hungrypython hungrypython - 5 months ago 8
Python Question

parsing CSV to pandas dataframes (one-to-many unmunge)

I have a csv file imported to a pandas dataframe. It probably came from a database export that combined a one-to-many parent and detail table. The format of the csv file is as follows:

header1, header2, header3, header4, header5, header6

sample1, property1,,,average1,average2
,,detail1,detail2,,
,,detail1,detail2,,
,,detail1,detail2,,

sample2, ...
,,detail1,detail2,,
,,detail1,detail2,,
...


(i.e.
line 0
is the
header
,
line 1
is
record 1
,
lines 2
through
n
are details, line n+1 is record 2 and so on...)

What is the best way to extricate (renormalize?) the details into separate
DataFrames
that can be referenced using values in the
sample#
records? The number of each subset of details are different for each sample.

I can use:

samplelist = df.header2[pd.notnull(df.header2)]


to get the starting index of each sample so that I can grab samplelist.index[0] to samplelist.index[1] and put it in a smaller dataframe. Detail records by themselves have no reference to which sample they came from, so that has to be inferred from the order of the csv file (notice that there is no intersection of filled/empty fields in my example).

Should I make a list of dataframes, a dict of dataframes, or a panel of dataframes?

Can I somehow create variables from the sample1 record fields and somehow attach them to each dataframe that has only detail records (like a collection of objects that have several scalar members and one dataframe each)?

Eventually I will create statistics on data from each detail record grouping and plot them against values in the sample records (e.g. sampletype, day or date, etc. vs. mystatistic). I will create intermediate Series to also be attached to the sample grouping like a kernel density estimation PDF or histogram.

Thanks.

Answer

You can use the fact that the first column seems to be empty unless it's a new sample record to .fillna(method='ffill') and then .groupby('header1') to get all the separate groups. On these, you can calculate statistics right away or store as separate DataFrame. High level sketch as follows:

df.header1 = df.header1.fillna(method='ffill')
for sample, data in df.groupby('header1'):
     print(sample) # access to sample name
     data = ... # process sample records