简体   繁体   English

根据 pandas 上另一列值的值创建一列

[英]Create a column based on a value from another columns values on pandas

I'm new with python and pandas and I'm struggling with a problem我是 python 和 pandas 的新手,我遇到了一个问题

Here is a dataset这是一个数据集

data = {'col1': ['a','b','a','c'], 'col2': [None,None,'a',None], 'col3': [None,'a',None,'b'], 'col4': ['a',None,'b',None], 'col5': ['b','c','c',None]}
df = pd.DataFrame(data)

I need to create 3 columns based on the unique values of col1 to col4 and whenever the col1 or col2 or col3 or col4 have a value equals to the header of the new columns it should return 1 otherwise it should return 0我需要根据 col1 到 col4 的唯一值创建 3 列,每当 col1 或 col2 或 col3 或 col4 的值等于新列的 header 时,它应该返回 1,否则它应该返回 0

need a output like this需要这样的 output

dataset output example:数据集 output 示例:

data = {'col1': ['a','b','a','c'], 'col2': [None,None,'a',None], 'col3': [None,'a',None,'b'], 'col4': ['a',None,'b',None], 'col5': ['b','c','c',None], 'a':[1,1,1,0],'b':[0,1,1,1],'c':[0,1,1,1]}
df = pd.DataFrame(data)

I was able to create a new colum and set it to 1 using the code below我能够使用下面的代码创建一个新列并将其设置为 1

df['a'] = 0
df['a'] = (df['col1'] == 'a').astype(int)

but it works only with the first column, I would have to repeat it for all columns.但它只适用于第一列,我必须对所有列重复它。

Is there a way to make it happens for all columns at once?有没有办法让它同时发生在所有列上?

Check with pd.get_dummies and groupby检查pd.get_dummiesgroupby

df = pd.concat([df,
                pd.get_dummies(df,prefix='',prefix_sep='').groupby(level=0,axis=1).max()],
                axis=1)
Out[377]: 
  col1  col2  col3  col4  col5  a  b  c
0    a  None  None     a     b  1  1  0
1    b  None     a  None     c  1  1  1
2    a     a  None     b     c  1  1  1
3    c  None     b  None  None  0  1  1
pd.concat([df, pd.get_dummies(df.stack().droplevel(1)).groupby(level=0).max()], axis=1)

result:结果:

   col1 col2    col3    col4    col5    a   b   c
0   a   None    None    a       b       1   1   0
1   b   None    a       None    c       1   1   1
2   a   a       None    b       c       1   1   1
3   c   None    b       None    None    0   1   1

暂无
暂无

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

相关问题 Pandas:如果来自第三列的字符串值,则根据另一列的值创建列 - Pandas : Create columns based on values of another column if string value from 3rd column 熊猫:创建条件列,并基于另一个df.groupby中2列的值返回值 - Pandas: create a conditional column and return a value based on the values of 2 columns in another df.groupby Pandas 使用其他列的值创建新列,根据列值选择 - Pandas create new column with values from other columns, selected based on column value 根据pandas中的另一个列值有条件地填充列值 - Conditionally fill column values based on another columns value in pandas Pandas/Python:根据另一列中的值将列的值存储到列表中 - Pandas/Python: Store values of columns into list based on value in another column 根据 Pandas 的另一列的值从相邻列中获取立即值 - Get the immediate values from the adjacent columns based on the value of another column with Pandas 如何根据 pandas 中的条件用另外两列的值替换部分列值 - How to replace a part of column value with values from another two columns based on a condition in pandas 根据另一列中的文本在 pandas 中创建列 - Create columns in pandas based on text from another column 基于分类列和来自另一列的值创建列 - Create columns based on a categorical column and values from another column 熊猫-根据其他2列的值创建一列 - Pandas - Create a column based on values from 2 other columns
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM