我有一个带有多个标题的数据框,其设置如下。我想将行和列合并为一行。有什么建议么?我能够降低一个标头,但不能降低更多。
代码
import pandas as pd
from pandas import ExcelWriter
df = pd.read_csv('Sample_Cookbook4.csv',header=[0,1],index_col=[0])
df = df.stack(dropna=False)
df.index = df.index.get_level_values(0) + df.index.get_level_values(1)
df = df.reset_index()
print(df)
当前数据框
Header1 | Header2 |
xxx1|xxx2|xxx3|xxx4||xxx1|xxx2|xxx3|xxx4||xxx1|xxx2|xxx3|xxx4 |
ColX|ColY |ColA|ColB|ColC|ColD||ColD|ColE|ColF|ColG||ColH|ColI|ColJ|ColDK|
1 | ds | 1 | |+1 |-1 | .......................................
2 | dh | ..........................................................
3 | ge | ..........................................................
4 | ew | ..........................................................
5 | er | ..........................................................
输出
ds-标头1-xx1-ColA:1
ds-标头1-xx2-ColB:“”
ds-标头1-xx2-ColB:-1