I have a csv -file structured like this:
Last Name;First Name;Start Date;End Date;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Example;Eva;;;1.1.2021;15.6.2021;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Here is some random information.;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-------;Header;------- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Index;Date;Time;Reading
0;10.4.2021;19:12:10;0,1;;;;;;
1;10.4.2021;19:07:14;;;;;;;;
2;10.4.2021;19:05:34;0,1;;;;;;
3;10.4.2021;19:05:32;0,1;;;;;;
4;10.4.2021;19:05:32;0,1;;;;;;
5;10.4.2021;19:05:31;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-------;Header;------- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Index;Date;Time;Reading
6;12.4.2021;19:12:10;0,1;;;;;;
7;12.4.2021;19:07:14;;;;;;;;
8;12.4.2021;19:05:34;0,1;;;;;;
9;12.4.2021;19:05:32;0,1;;;;;;
10;12.4.2021;19:05:32;0,1;;;;;;
11;12.4.2021;19:05:31;;;;;;;;
My goal is to get a clean dict out of the file with only the information I need. Let's say I want only name, dates and readings structured like this:
{
'last_name': 'Example',
'first_name': 'Eva',
'measurements': [{'date:': 'some_date', 'reading': 'some_reading'}]
}
How can I iterate through the columns and only get the ones I need? There are a lot of columns where the Reading field is nan. In my example this happens in the second and the last row, in both sections.
This is what I have tried so far to get the data I want from the csv:
df = (pd.read_csv(file,
sep='\s+',
skiprows=6,
index_col=0,
dtype='unicode'
)
)
df = pd.DataFrame(df, columns=['Reading', 'Date', 'Time'])
print(df.keys())
print(len(df))
test_list = df.values.tolist()
print(test_list)
print(len(test_list))
This gives me result:
Index(['Reading', 'Date', 'Time'], dtype='object')
13520
[[nan, nan, nan], [nan, nan, nan], ...]
13520
So the list where I want to have the values is just a list of list of nan's.