Need code advice from a Python/pandas guru

Background:

I receive data from a remote sensor on a bi-weekly basis. The device stores the data (date/time and 8 parameters) in an .html format and my technician emails me the files from each device (10 devices). My company has a data management group that takes the data and gets into a intranet database for later analysis.

Unfortunately, the data management guy I send the files to has left the company. He had written a Python script scrape the html data and upload it to the main database. No one else in the data group uses Python, so the group manager sent me the script and now I get to figure out what he did.

My last encounter with Python was in version 2.3 day back in the early 2000's. However, going through the code, I have figured out generally what is happening. I have installed Python on my laptop, but I have already noticed that the overall environment can be different.

The original script was written for Python 2.7, but I installed Python 3.6. The script makes use of numpy and pandas, as well as BeautifulSoup for html parsing.

Current Issue:

In order to upload the sensor data into the host database, I need to create a csv that conforms to a specific format with proper column header names. We use an excel spreadsheet as a template, and I have been able to use the pandas commands to read the excel file and create a dataframe with the proper format.

In addition, via various functions and data manipulation, I have created a dataframe that contains the information I need, so ultimately I have a template dataframe that is of size [1,9], one row and 9 columns. The dataframe with the scraped data is of size [5130,5]. I am simply trying to get the data from the scraped dataframe (named stackedTemp) to the template dataframe (named dfEdd) using the following code:


Code:
dfEdd['#sys_loc_code'] = stackedTemp['location']
dfEdd['parameter_code'] = stackedTemp['param']
dfEdd['parameter_value'] = stackedTemp['value']
dfEdd['parameter_unit'] = stackedTemp['unit']
dfEdd['measurement_date'] = stackedTemp['Date Time']
dfEdd['measurement_method'] = 'Aqua Troll 600'

What I end up getting is the template dataframe only has one row of data. The data is correct, but the other 5129 rows of data don't come across. I'm thinking this is an indexing problem, but I don't understand the dataframe structure well enough to go much deeper at the moment. I need to get this solved as I will have data files piling up pretty soon. I can probably bruteforce and just make a csv from my temp data and cut and pase in Excel, but I'd rather do it pro grammatically if possible.

Any pointers appreciated,

Thanks!!
 

Ben_H

Ars Scholae Palatinae
1,059
Just to clarify, are you trying to take the entire column of entries for the 5 different headings from stackedTemp and move them to dfEdd?

In that case, you're 99% of the way there and you just need to do:

Code:
dfEdd['#sys_loc_code'] = stackedTemp['location'].values
dfEdd['parameter_code'] = stackedTemp['param'].values
dfEdd['parameter_value'] = stackedTemp['value'].values
dfEdd['parameter_unit'] = stackedTemp['unit'].values
dfEdd['measurement_date'] = stackedTemp['Date Time'].values
dfEdd['measurement_method'] = 'Aqua Troll 600'

That should work as you want if I'm reading your issue correctly. I tested the code using some python code I had lying around and it worked, though I was starting from an empty dataframe when I moved the columns over.

I ran into this problem when I was learning pandas and R too. Accessing the data in both isn't quite the same as it is in most languages or in standard python lists.