大熊猫是否可以将两个(或三个...)头行与统一的excel-cell融合为一个? 例如表格:
| Report for ..... | Income | Ordered |
|-----------------|--------------------|---------------------|--------------|---------------|-----------|-------------------------------|
| Brend | Art of supplier | Contract | pcs | income price | pcs | rub (prize for selling) |
| Elena Chezelle | Y0060 | 0400-6752 Agent | 85 | 245,00 | 226 | 785,00 |
| Amour Bridal | ALWE-1199-WHITE | 0400-6752 Agent | 47 | 56,00 | 163 | 857,00 |
进入:
| Brend | Art of supplier | Contract | Income pcs | Income price | Ordered pcs | Ordered rub (prize for selling) |
|----------------|--------------------|---------------------|-----------------|---------------------------|-------------|----------------------------------------|
| Elena Chezelle | Y0060 | 0400-6752 Agent | 85 | 245,00 | 226 | 785,00 |
| Amour Bridal | ALWE-1199-WHITE | 0400-6752 Agent | 47 | 56,00 | 163 | 857,00 |
直到现在使用循环使用代码完成
: file = 'static/ExportToEXCELOPENXML - 2020-05-11T180206.635.xlsx'
df = pd.read_excel(file)
df_list = []
for x in df.columns:
if 'Unnamed' in x or 'Report' in x:
df_list.append('')
else:
df_list.append(x + ' ')
i = 0
while i < len(df_list):
if df_list[i] == '':
df_list[i] = df_list[i - 1]
i += 1
print(df_list)
df.columns = df_list + df.iloc[0]
df_new = df.iloc[1:].reset_index(drop=True)
df_new.to_excel('static/test.xlsx', index=None)
有可能只用熊猫(没有周期)吗?
答案 0 :(得分:2)
当您阅读Excel时,可以给两行添加为标题
df = pd.read_excel('static/ExportToEXCELOPENXML - 2020-05-11T180206.635.xlsx', header=[0,1])
如果要使用分隔符将它们连接在一起,则可以在阅读后进行操作
df.columns = df.columns.map('_'.join)