简体   繁体   English

Pandas基于两个现有变量创建一个新变量

[英]Pandas creating a new variable based on two existing variables

I have the following code I think is highly inefficient. 我认为以下代码效率很低。 Is there a better way to do this type common recoding in pandas? 有没有更好的方法在熊猫中进行这种类型的常见重新编码?

df['F'] = 0
df['F'][(df['B'] >=3) & (df['C'] >=4.35)] = 1
df['F'][(df['B'] >=3) & (df['C'] < 4.35)] = 2
df['F'][(df['B'] < 3) & (df['C'] >=4.35)] = 3
df['F'][(df['B'] < 3) & (df['C'] < 4.35)] = 4

Use numpy.select and cache boolean masks to variables for better performance: 使用numpy.select并将布尔掩码缓存到变量以获得更好的性能:

m1 = df['B'] >= 3
m2 = df['C'] >= 4.35
m3 = df['C'] < 4.35
m4 = df['B'] < 3

df['F'] = np.select([m1 & m2, m1 & m3, m4 & m2, m4 & m3], [1,2,3,4], default=0)

In your specific case, you can make use of the fact that booleans are actually integers (False == 0, True == 1) and use simple arithmetic: 在您的特定情况下,您可以利用布尔实际上是整数(False == 0,True == 1)并使用简单算术的事实:

df['F'] = 1 + (df['C'] < 4.35) + 2 * (df['B'] < 3)

Note that this will ignore any NaN's in your B and C columns, these will be assigned as being above your limit. 请注意,这将忽略BC列中的任何NaN,这些将被指定为高于您的限制。

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

相关问题 基于两列创建新变量作为索引一列作为新变量名称python pandas或R. - Creating new variables based on two columns as index one column as new variable names python pandas or R 大熊猫:在循环中创建现有变量的滞后变量 - pandas: creating lagged variables of existing variable in a loop 基于 pandas 中的现有列创建新列 - Creating new column based on existing column in pandas 根据 if 和现有列在 pandas 中创建新列 - creating new column in pandas based on if and existing column 根据不同索引的值减去两个变量,在 Pandas 中创建新列 - Creating New Columns in Pandas based on subtracting two variables based on value from different indexes Python pandas 和 numpy:根据现有变量的多个条件为新变量分配数值 - Python pandas and numpy: assign numerical values to new variable based on multiple conditions for existing variables 基于两个参数创建新的 Pandas DataFrame - Creating new Pandas DataFrame based on two parameters Python / Pandas - 基于几个变量和if / elif / else函数创建新变量 - Python/Pandas - creating new variable based on several variables and if/elif/else function 根据其他两个变量的值创建一个变量 - Creating a variable based on the values of two other variables 无法根据现有变量创建新变量 - Trouble creating new variable based off of existing variable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM