[英]Merge and combine text of two first rows in specific columns in Dataframe
I have a DataFrame, df based on an imported csv-file, in order to be able to update data as new dataset become available.我有一个 DataFrame, df 基于导入的 csv 文件,以便能够在新数据集可用时更新数据。
Unnamed: 2 Unnamed: 3 Unnamed: 4 Unnamed: 5
1 NO1 NaN NO2 NaN
2 Up Down Up Down
3 0 0 0 0
4 0 0 0 0
In my dataset, the "NO1" is two merged cells with two sub-categories "Up" & "Down" - I want to combine the "NO1" with both "Up" and "Down" into headers with the text "NO1 Up" & "NO1 Down", etc.在我的数据集中,“NO1”是具有两个子类别“Up”和“Down”的两个合并单元格 - 我想将“NO1”与“Up”和“Down”组合成带有文本“NO1 Up”的标题" & "NO1 Down" 等
What I want to end up with is:我想要结束的是:
NO1 Up NO1 Down NO2 Up NO2 Down
0 0 0 0 0
1 0 0 0 0
2 0 0 0 0
3 0 0 0 0
You can TRY:你可以试试:
df.columns = (df.iloc[0].ffill() + ' ' + df.iloc[1]).values
df = df[2:]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.