【发布时间】:2022-01-01 04:40:44
【问题描述】:
这就是我使用 pandas 读取和创建数据框的方式
def get_sheet_data(sheet_name='SomeName'):
df = pd.read_excel(f'{full_q_name}',
sheet_name=sheet_name,
header=[0,1],
index_col=0)#.fillna(method='ffill')
df = df.swapaxes(axis1="index", axis2="columns")
return df.set_index('Product Code')
我似乎无法将前两行添加到标题中,我试过了:
python:pandas - How to combine first two rows of pandas dataframe to dataframe header?
https://stackoverflow.com/questions/59837241/combine-first-row-and-header-with-pandas
我在每一点都失败了。我认为是因为多索引,不一定是轴交换?但是使用:
https://pandas.pydata.org/docs/reference/api/pandas.MultiIndex.html
现在有点让我头疼。请帮我将这两行添加到标题中?
df.columns 的输出量很大,所以我把它削减了很多:
Index(['Product Code','Product Narrative\nHigh-level service description','Product Name','Huawei Product ID','Type','Bill Cycle Alignment',nan,'Stackable',nan,
并以:
结尾nan], dtype='object')
【问题讨论】:
-
请发布 df.columns 的输出,并尝试复制粘贴数据框至少几行而不是发布图片?
-
我希望它足够达斯巴巴
标签: python-3.x pandas dataframe multi-index