简体   繁体   English

如何用列名填充 dataframe 的真值?

[英]How to fill true values of a dataframe with column names?

I have a DataFrame with True and False values.我有一个带有 True 和 False 值的 DataFrame。

       A      B      C      D
0  False  True   True   False
1  False  False  True   False
2  True   True   False  False

I want to fill the true values with column names and false values with 0. How can I do that?我想用列名填充真值,用 0 填充假值。我该怎么做?

ie To get the result as即得到结果为

   A  B  C  D
0  0  B  C  0
1  0  0  C  0
2  A  B  0  0

First replace booelan to int and then use mask or where with inverting mask by ~ : 首先更换booelanint和再使用maskwhere通过反向掩码~

df = df.astype(int).mask(df, df.columns.to_series(), axis=1)
print (df)
   A  B  C  D
0  0  B  C  0
1  0  0  C  0
2  A  B  0  0

df = df.astype(int).where(~df, df.columns.to_series(), axis=1)
print (df)
   A  B  C  D
0  0  B  C  0
1  0  0  C  0
2  A  B  0  0

Thank you John Galt for improvement in new versions of pandas 0.21.x : 感谢John Galt对新版pandas 0.21.x

df = df.astype(int).mask(df, df.columns, axis=1)

numpy solution: numpy解决方案:

a = np.tile(df.columns, [len(df.index),1])
print (a)
[['A' 'B' 'C' 'D']
 ['A' 'B' 'C' 'D']
 ['A' 'B' 'C' 'D']]

df = pd.DataFrame(np.where(df.astype(int), a, 0), columns=df.columns, index = df.index)
print (df)
   A  B  C  D
0  0  B  C  0
1  0  0  C  0
2  A  B  0  0

pandas 1.5.2 pandas 1.5.2

df = df.mask(df.astype(bool), df.columns.to_series(), axis=1)

not astype(int) but astype(bool) or astype('bool')不是astype(int)而是astype(bool) or astype('bool')

Otherwise ValueError: Boolean array expected for the condition, not uint8否则ValueError:条件应为 Boolean 数组,而不是 uint8


can't remove .to_series()无法删除.to_series()

Otherwise ValueError: other must be the same shape as self when an ndarray否则ValueError: other must be the same shape as self when an ndarray

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

相关问题 如何根据条件从数据框列名称填充列值? - How to fill column values from dataframe column names based on condition? 通过将列名与字典匹配来填充 dataframe 中的值 - Fill values in dataframe by matching column names to dictionary 如何在另一个 dataframe 的特定列名上填写 dataframe - How can I fill a dataframe on specific column names of another dataframe 如何将 True 或 False 值添加到数据框列? - How to add True or False values to a dataframe column? 如何检查数据框B中第1列的值是否在数据框A中第1列和第2列的值之间。如果为true,则返回数据框A的第2列 - How to check if values of column 1 in dataframe B are between the values of column 1 and 2 in dataframe A. If true, return column 2 of dataframe A 使用另一个 pandas 数据框的列填充 na 值,但使用列索引,而不是名称 - Fill na values in one pandas dataframe's column using another, but using column indices, not names 根据值填写 Dataframe 列 - Fill Dataframe column based on values 如何使用 pandas 中的 dataframe 中的值作为列名,并将列名作为值在 dataframe 中使用 - how to use values from a dataframe in pandas for column names, and column names as values insdie the dataframe 如何从具有 True 值的列名称创建新列 - How to create a new column from columns names that have True values 在数据框中打印列名称和值 - Printing Column Names and Values in Dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM