将DataFrame写入Excel文件,其中列表中的项目被放入单独的单元格。

huangapple go评论56阅读模式
英文:

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

问题

考虑一个名为pivoted的数据框,其中一些数据的复制以数据框中的列表形式给出:

import pandas as pd

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')

可以将这个数据框写入Excel文件,如下所示:

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

希望的Excel文件中,如何改为将复制数据放在相邻单元格中?希望的Excel文件如下:

如何改为将复制数据放在相邻单元格中?

你可以使用以下代码将数据重新排列,然后将其写入Excel文件:

# 重新排列数据框
rearranged = pd.DataFrame(pivoted.stack().values.tolist(), index=pivoted.stack().index, columns=pivoted.stack().columns)

# 写入Excel文件
with pd.ExcelWriter('desired_output.xlsx') as writer:
    rearranged.to_excel(writer, sheet_name='Sheet1', index_label='Conc')

这将把复制数据放在相邻单元格中,并创建你所期望的Excel文件。

英文:

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')

将DataFrame写入Excel文件,其中列表中的项目被放入单独的单元格。

How can this instead be written where replicate data are given in side-by-side cells? Desired excel file:
将DataFrame写入Excel文件,其中列表中的项目被放入单独的单元格。

答案1

得分: 4

以下是代码部分的翻译:

首先,您需要以稍微不同的方式对数据进行转换,首先使用explode来展开"Data"列,然后使用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)
)

输出结果:

        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
英文:

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

答案2

得分: 2

除了@mozway的答案外,仅供格式化,您可以使用以下代码:

piv = (df.explode('Data').assign(col=lambda x: x.groupby(level=0).cumcount())
         .pivot(index='Conc', columns=['Compound', 'col'], values='Data')
         .rename_axis(None))
piv.columns = pd.Index([i if j == 0 else '' for i, j in piv.columns], name='Conc')
piv.to_excel('file.xlsx')

将DataFrame写入Excel文件,其中列表中的项目被放入单独的单元格。

英文:

Beside the @mozway's answer, just for formatting, you can use:

piv = (df.explode('Data').assign(col=lambda x: x.groupby(level=0).cumcount())
         .pivot(index='Conc', columns=['Compound', 'col'], values='Data')
         .rename_axis(None))
piv.columns = pd.Index([i if j == 0 else '' for i, j in piv.columns], name='Conc')
piv.to_excel('file.xlsx')

将DataFrame写入Excel文件,其中列表中的项目被放入单独的单元格。

huangapple
  • 本文由 发表于 2023年2月14日 21:42:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75448714.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定