我有以下数据框:
data={'a1':['X1',2,3,4,5],'Unnamed: 02':['Y1',5,6,7,8],'b1':['X2',5,3,7,9],'Unnamed: 05':['Y2',5,8,9,3],'c1':['X3',4,5,7,5],'Unnamed: 07':['Y3',5,8,9,3],'d1':['P',2,4,5,7],'Unnamed: 09':['M',8,4,6,7]}
df=pd.DataFrame(data)
df.columns=df.columns.to_series().mask(lambda x: x.str.startswith('Unnamed')).ffill()
df
我想做的几件事:
- 更改包含的行(x1,x2&x3)仅为'x(反之亦然,y1,y2,y3 in'y)将
- 现有列标头与包含x,y,p,m的行组合在一起,
结果应该看起来像:
- 更改行将(x1,x2&x3)包含在'x中(反之亦然,y1,y2,y3 in'y)
- 将现有的列标头与包含X,Y,P,M-的行组合'm'完全替换了'd1'。
I have the following dataframe:
data={'a1':['X1',2,3,4,5],'Unnamed: 02':['Y1',5,6,7,8],'b1':['X2',5,3,7,9],'Unnamed: 05':['Y2',5,8,9,3],'c1':['X3',4,5,7,5],'Unnamed: 07':['Y3',5,8,9,3],'d1':['P',2,4,5,7],'Unnamed: 09':['M',8,4,6,7]}
df=pd.DataFrame(data)
df.columns=df.columns.to_series().mask(lambda x: x.str.startswith('Unnamed')).ffill()
df

There are a few things which I would like to do:
- Change the rows containing (X1, X2 & X3) into just 'X (vice versa for Y1,Y2,Y3 into 'Y)
- Combine the existing column header with the row containing X,Y,P,M
The outcome should look like:
- Change the rows containing (X1, X2 & X3) into just 'X (vice versa for Y1,Y2,Y3 into 'Y)

- Combine the existing column header with the row containing X,Y,P,M - Also note that the 'P' and 'M' completely replaces 'd1' respectively.

发布评论
评论(3)
尝试一下。
Try this.
另外,您可以执行这样的操作:
它是一个更通用的解决方案,因为它使用每个行零条目的长度作为条件,而不是实际值“ P”和“ M”。因此,它适用于每个字符串。
Alternatively, you can do something like this:
It's a more general solution as it uses the length of each row-zero entry as a condition, not the actual values 'P' and 'M'. Thus, it holds for each single character string.
输出:
Output: