【发布时间】:2020-12-14 09:39:08
【问题描述】:
我在excel表格中有这样的数据,
当尝试读取 excel 时
read_df_from_excel = pd.read_excel('output.xlsx', sheetname='Sheet1')
我看到 7 行,test1 和 test2 仅添加到 2 行,其他行具有 NaN,如下所示:
有什么方法可以将 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.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}]
标签: excel pandas dataframe pandas-groupby openpyxl