gpt4 book ai didi

excel - 将 excel 数据分组到 pandas 数据框

转载 作者:行者123 更新时间:2023-12-04 20:24:25 25 4
gpt4 key购买 nike

我的excel表格里有这样的数据,

enter image description here

当尝试读取excel时

read_df_from_excel = pd.read_excel('output.xlsx', sheetname='Sheet1')

我看到 7 行,test1 和 test2 只添加到 2 行,其他行有 NaN,如下所示:

enter image description here

有什么方法可以将分组的项目从 excel 读取到分组的 pandas 数据框中吗?

read_df_from_excel.to_dict(orient='r')

[{'test': 'test1', 'segment': 's1', 'c1': 23, 'c2': 7, 'c3': 78, 'c4': 1231},
{'test': nan, 'segment': 's2', 'c1': 32, 'c2': 123, 'c3': 5, 'c4': 745},
{'test': nan, 'segment': 's3', 'c1': 123, 'c2': 13, 'c3': 1, 'c4': 13},
{'test': nan, 'segment': 's4', 'c1': 7, 'c2': 123, 'c3': 5, 'c4': 12},
{'test': 'test2', 'segment': 's1', 'c1': 456, 'c2': 5, 'c3': 41, 'c4': 22},
{'test': nan, 'segment': 's2', 'c1': 31, 'c2': 1, 'c3': 213, 'c4': 8},
{'test': nan, 'segment': 's3', 'c1': 13, 'c2': 1, 'c3': 4, 'c4': 323},
{'test': nan, 'segment': 's4', 'c1': 13, 'c2': 2, 'c3': 23, 'c4': 23}]

感谢您的帮助!

最佳答案

不确定是否有读取合并单元格的方法,但像这样的简单处理步骤可以解决这个问题

read_df_from_excel['test'] = read_df_from_excel['test'].fillna(method='ffill')

关于excel - 将 excel 数据分组到 pandas 数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63583204/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com