I am importing an excel worksheet using pandas and trying to remove any instance where there is a duplicate area measurement for a given Frame. The sheet I'm playing with looks vaguely like the table below wherein there are n number of files, a measured area from each frame of an individual file, and the Frame Number that corresponds to each area measurement.
| Filename.0 | Area.0 | Frame.0 | Filename.1 | Area.1 | Frame.1 | ... | Filename.n | Area.n | Filename.n |
|---|---|---|---|---|---|---|---|---|---|
| Exp327_Date_File_0 | 600 | 1 | Exp327_Date_File_1 | 830 | 1 | ... | Exp327_Date_File_n | 700 | 1 |
| Exp327_Date_File_0 | 270 | 2 | Exp327_Date_File_1 | 730 | 1 | ... | Exp327_Date_File_n | 600 | 2 |
| Exp327_Date_File_0 | 230 | 3 | Exp327_Date_File_1 | 630 | 2 | ... | Exp327_Date_File_n | 500 | 3 |
| Exp327_Date_File_0 | 200 | 4 | Exp327_Date_File_1 | 530 | 3 | ... | Exp327_Date_File_n | 400 | 4 |
| NaN | NaN | NaN | Exp327_Date_File1 | 430 | 4 | ... | NaN | NaN | NaN |
If I manually go through the excel worksheet and concatenate the filenames into just 3 unique columns containing my entire dataset like so:
| Filename | Area | Frame |
|---|---|---|
| Exp327_Date_File_0 | 600 | 1 |
| Exp327_Date_File_0 | 270 | 2 |
| etc... | etc... | etc... |
| Exp327_Date_File_n | 530 | 4 |
I have been able to successfully use pandas to remove the duplicates using the following:
df_1 = df.groupby(['Filename', 'Frame Number']).agg('Area': 'sum')
However, manually concatenating everything into this format isn't feasible when I have hundreds of File replicates and I will then have to separate everything back out into multiple column-sets (similar to how the data is presented in Table 1). How do I either (1) use pandas to create a new Dataframe with every 3 columns stacked on top of each other which I can then group and aggregate before breaking back up into individual sets of columns based on Filename or (2) loop through the multiple filenames and aggregate any Frames with multiple Areas? I have tried option 2:
(row, col) = df.shape #shape of the data frame the excel file was read into
for count in range(0,round(col/3)): #iterate through the data
aggregation_functions = {'Area.'+str(count):'sum'} #add Areas together
df_2.groupby(['Filename.'+str(count), 'Frame Number.'+str(count)]).agg(aggregation_functions)
However, this just returns the same DataFrame without any of the Areas summed together. Any help would be appreciated and please let me know if my question is unclear