简体   繁体   English

根据Pandas中的特定条件将值从一列复制到同一行中的其他列

[英]Copy values from one column to other columns in same row based on specific criteria in Pandas

I have a data frame like below: 我有一个如下data frame

import pandas as pd

df = pd.DataFrame({'ID':['M001','M002','M003','M004','M005'],
                      'X001':[0,0,1,0,0],
                      'X002':[0,0,1,1,0],
                      'X003':[0,0,1,0,1],
                      'X004':[1,0,1,0,0],
                      'X005':[1,0,1,1,0]})

print(df)

And it looks like this: 它看起来像这样:

     ID  X001  X002  X003  X004  X005
0  M001     0     0     0     1     1
1  M002     0     0     0     0     0
2  M003     1     1     1     1     1
3  M004     0     1     0     0     1
4  M005     0     0     1     0     0

What I want to do is to copy the value in the ID column into the other columns based where the value is 1 as shown below. 我想做的就是将ID列中的值复制到其他列中,其中值是1 ,如下所示。

     ID  X001  X002  X003  X004  X005
0  M001     0     0     0  M001  M001
1  M002     0     0     0     0     0
2  M003  M003  M003  M003  M003  M003
3  M004     0  M004     0     0  M004
4  M005     0     0  M005     0     0

What would be the easiest and fastest way to do so on a ~2000 x ~2000 data frame ? 在〜2000 x〜2000 data frame上,最简单,最快的方法是什么?

Here is a way, replacing 1 with a null value, Transposing, using fillna , and transposing back: 这是一种使用空值替换1的方法,即使用fillna转置并向后转置:

df.T.replace(1,pd.np.nan).fillna(df['ID']).T

     ID  X001  X002  X003  X004  X005
0  M001     0     0     0  M001  M001
1  M002     0     0     0     0     0
2  M003  M003  M003  M003  M003  M003
3  M004     0  M004     0     0  M004
4  M005     0     0  M005     0     0

I might use where , for example: 我可能使用where ,例如:

In [218]: df.where(df != 1, df.ID, axis=0)
Out[218]: 
     ID  X001  X002  X003  X004  X005
0  M001     0     0     0  M001  M001
1  M002     0     0     0     0     0
2  M003  M003  M003  M003  M003  M003
3  M004     0  M004     0     0  M004
4  M005     0     0  M005     0     0

There's an np.where equivalent of this which, like usual, is slightly faster but I find it harder to read. 有一个np.where等效项,与往常一样,速度稍快,但我觉得很难阅读。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 熊猫根据同一行中其他列的值创建新列 - pandas creates new columns based on the values of the other columns in the same row 根据其他列的值创建新列/在 Pandas 中按行应用多列的 function - Create new column based on values from other columns / apply a function of multiple columns, row-wise in Pandas 如何根据同一行中其他列的值返回一列的值(在值阈值内)? - How do I return values of one column (within a value threshold) based on the values of other columns in the same row? 根据 pandas 中的其他列值合并具有相同列值的行 - Merge row with a same column value based on other column values in pandas 如何根据 pandas 中两个不同列的条件将值从一列复制到另一列? - how to copy values from one column into another column based on conditions of two different columns in pandas? Pandas - Select 来自特定列的行值基于其他列的值 - Pandas - Select row value from specific column based on value from other columns pandas dataframe根据相应行的其他列更新列值 - pandas dataframe update column values based on other columns of the corresponding row 熊猫:当其他两列是唯一对时,从一列复制值 - Pandas: Copy values from one column when two other columns are unique pairs 根据列值将值从一个数据帧映射到其他数据帧中的新列 - Pandas - Map values from one dataframe to new columns in other based on column values - Pandas Pandas:根据其他列的值创建一个新列(按行) - Pandas: Create a new column based on values from other columns (Row wise)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM