Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Writing a DataFrame to an excel file where items in a list are put into separate cells

Consider a dataframe like pivoted, where replicates of some data are given as lists in a dataframe:


d = {'Compound': ['A', 'A', 'A', 'B', 'B', 'B', 'C', 'C', 'C', 'C'],
     'Conc': [1, 0.5, 0.1, 1, 0.5, 0.1, 2, 1, 0.5, 0.1],
     'Data': [[100, 90, 80], [50, 40, 30], [10, 9.7, 8], 
              [20, 15, 10], [3, 4, 5, 6], [100, 110, 80],
              [30, 40, 50, 20], [10, 5, 9, 3], [2, 1, 2, 2], [1, 1, 0]]}

df = pd.DataFrame(data=d)
pivoted = df.pivot(index='Conc', columns='Compound', values='Data')

This df can be written to an excel file as such:

with pd.ExcelWriter('output.xlsx') as writer:
    pivoted.to_excel(writer, sheet_name='Sheet1', index_label='Conc')

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

How can this instead be written where replicate data are given in side-by-side cells? Desired excel file:
enter image description here

>Solution :

Then you need to pivot your data in a slightly different way, first explode the Data column, and deduplicate with groupby.cumcount:

(df.explode('Data')
   .assign(n=lambda d: d.groupby(level=0).cumcount())
   .pivot(index='Conc', columns=['Compound', 'n'], values='Data')
   .droplevel('n', axis=1).rename_axis(columns=None)
)

Output:

        A    A    A    B    B    B    B   C   C   C    C
Conc                                                        
0.1    10  9.7    8  100  110   80  NaN   1   1   0  NaN
0.5    50   40   30    3    4    5    6   2   1   2    2
1.0   100   90   80   20   15   10  NaN  10   5   9    3
2.0   NaN  NaN  NaN  NaN  NaN  NaN  NaN  30  40  50   20
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading