简体   繁体   English

如何根据两个或多个其他变量创建pandas dataframe变量/列?

[英]How to create pandas dataframe variable/column based on two or more other variables?

I have a pandas dataframe, eg: 我有一个pandas数据帧,例如:

Col1 Col2
A     1 
B     2
C     3

I understand how to create a Col3 based on say the value of Col2: 我理解如何基于Col2的值来创建Col3:

df['Col3'] = (df['Col2'] <= 1).astype(int)

But ... How about if the new column was based on two variables, as in (pseudocode here): 但是......如果新列基于两个变量,如(伪代码):

if Col2=1 and Col3=1 then Col4='X'
else if Col2=1 and Col3=2 then Col4='Y'
else Col4='Z'

how would that be achieved? 怎么会实现? many thanks 非常感谢

You can try double numpy.where : 你可以尝试双numpy.where

df['Col4'] = np.where((df['Col2'] == 1) & (df['Col3'] == 1), 'X', 
             np.where((df['Col2'] == 1) & (df['Col3'] == 2), 'Y', 'Z'))

Sample: 样品:

import pandas as pd

df = pd.DataFrame({'Col2': {0: 1, 1: 1, 2: 3}, 
                   'Col1': {0: 'A', 1: 'B', 2: 'C'}, 
                   'Col3': {0: 1, 1: 2, 2: 4}})
print (df)

  Col1  Col2  Col3
0    A     1     1
1    B     1     2
2    C     3     4

df['Col4'] = np.where( (df['Col2'] == 1) & (df['Col3'] == 1), 'X', 
             np.where((df['Col2'] == 1) & (df['Col3'] == 2), 'Y', 'Z'))

print (df)
  Col1  Col2  Col3 Col4
0    A     1     1    X
1    B     1     2    Y
2    C     3     4    Z

Another solution with loc and fillna for fill NaN all other values: 使用locfillna另一个解决方案,用于填充NaN所有其他值:

df.loc[ (df['Col2'] == 1) & (df['Col3'] == 1) , 'Col4'] =  'X'
df.loc[ (df['Col2'] == 1) & (df['Col3'] == 2) , 'Col4'] =  'Y'
df['Col4'] = df['Col4'].fillna('Z')

print (df)
  Col1  Col2  Col3 Col4
0    A     1     1    X
1    B     1     2    Y
2    C     3     4    Z

You can initialize the column with your final else value (eg Z ) and then check each condition: 您可以使用最终的else值(例如Z )初始化列,然后检查每个条件:

df['Col4'] = 'Z'
df.loc[(df.Col1 == 1) & (df.Col3 == 1), 'Col4'] = 'X'
df.loc[(df.Col2 == 1) & (df.Col3 == 2), 'Col4'] = 'Y'

暂无
暂无

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

相关问题 Pandas DataFrame 基于其他两列创建新的 csv 列 - Pandas DataFrame create new csv column based on two other columns 如何基于一个或多个 OTHER 列的条件子字符串搜索在 Pandas 数据框中创建一列 - How to create a column in a Pandas dataframe based on a conditional substring search of one or more OTHER columns 大熊猫:根据其他数据框创建数据框列 - Pandas: Create dataframe column based on other dataframe 如何根据 pandas dataframe 中其他列中的子字符串创建新列? - How to create new column based on substrings in other column in a pandas dataframe? 如何基于保存日期的其他两个列创建一个 Pandas DataFrame 列? - How to create a pandas DataFrame column based on two other columns that holds dates? 如何基于布尔表达式和其他两个列的关系在pandas数据框中创建列 - How to create column in pandas dataframe based on boolean expression and relationship of two other columns 如何根据 Pandas 数据框中的其他行创建新列? - How create a new column based on other rows in pandas dataframe? 如何在其他两列上创建熊猫数据框列循环? - How to create pandas dataframe column loop on two other columns? 基于数据框的其他列创建一个新的熊猫数据框列 - Create a new pandas dataframe column based on other column of the dataframe Pandas:根据 DataFrame 中的其他列在 DataFrame 中创建新列 - Pandas: Create new column in DataFrame based on other column in DataFrame
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM