将 if 语句与 apply in python 相结合
Combine if statement with apply in python
python 的新手。我正在尝试找出基于其他列创建列的最佳方法。理想情况下,代码应该是这样的。
df['new'] = np.where(df['Country'] == 'CA', df['x'], df['y'])
我认为这行不通,因为它认为我正在调用整个专栏。我尝试用 apply 做同样的事情,但语法有问题。
df['my_col'] = df.apply(
lambda row:
if row.country == 'CA':
row.my_col == row.x
else:
row.my_col == row.y
我觉得一定有更简单的方法。
这可能也适合你
data = {
'Country' : ['CA', 'NY', 'NC', 'CA'],
'x' : ['x_column', 'x_column', 'x_column', 'x_column'],
'y' : ['y_column', 'y_column', 'y_column', 'y_column']
}
df = pd.DataFrame(data)
condition_list = [df['Country'] == 'CA']
choice_list = [df['x']]
df['new'] = np.select(condition_list, choice_list, df['y'])
df
你的 np.where() 看起来不错,所以我会仔细检查你的列是否正确标记。
这三种方法(np.where
、apply
、mask
)中的任何一种似乎都有效:
df['where'] = np.where(df.country=='CA', df.x, df.y)
df['apply'] = df.apply(lambda row: row.x if row.country == 'CA' else row.y, axis=1)
mask = df.country=='CA'
df.loc[mask, 'mask'] = df.loc[mask, 'x']
df.loc[~mask, 'mask'] = df.loc[~mask, 'y']
完整测试代码:
import pandas as pd
import numpy as np
df = pd.DataFrame({'country':['CA','US','CA','UK','CA'], 'x':[1,2,3,4,5], 'y':[6,7,8,9,10]})
print(df)
df['where'] = np.where(df.country=='CA', df.x, df.y)
df['apply'] = df.apply(lambda row: row.x if row.country == 'CA' else row.y, axis=1)
mask = df.country=='CA'
df.loc[mask, 'mask'] = df.loc[mask, 'x']
df.loc[~mask, 'mask'] = df.loc[~mask, 'y']
print(df)
输入:
country x y
0 CA 1 6
1 US 2 7
2 CA 3 8
3 UK 4 9
4 CA 5 10
输出
country x y where apply mask
0 CA 1 6 1 1 1.0
1 US 2 7 7 7 7.0
2 CA 3 8 3 3 3.0
3 UK 4 9 9 9 9.0
4 CA 5 10 5 5 5.0
python 的新手。我正在尝试找出基于其他列创建列的最佳方法。理想情况下,代码应该是这样的。
df['new'] = np.where(df['Country'] == 'CA', df['x'], df['y'])
我认为这行不通,因为它认为我正在调用整个专栏。我尝试用 apply 做同样的事情,但语法有问题。
df['my_col'] = df.apply(
lambda row:
if row.country == 'CA':
row.my_col == row.x
else:
row.my_col == row.y
我觉得一定有更简单的方法。
这可能也适合你
data = {
'Country' : ['CA', 'NY', 'NC', 'CA'],
'x' : ['x_column', 'x_column', 'x_column', 'x_column'],
'y' : ['y_column', 'y_column', 'y_column', 'y_column']
}
df = pd.DataFrame(data)
condition_list = [df['Country'] == 'CA']
choice_list = [df['x']]
df['new'] = np.select(condition_list, choice_list, df['y'])
df
你的 np.where() 看起来不错,所以我会仔细检查你的列是否正确标记。
这三种方法(np.where
、apply
、mask
)中的任何一种似乎都有效:
df['where'] = np.where(df.country=='CA', df.x, df.y)
df['apply'] = df.apply(lambda row: row.x if row.country == 'CA' else row.y, axis=1)
mask = df.country=='CA'
df.loc[mask, 'mask'] = df.loc[mask, 'x']
df.loc[~mask, 'mask'] = df.loc[~mask, 'y']
完整测试代码:
import pandas as pd
import numpy as np
df = pd.DataFrame({'country':['CA','US','CA','UK','CA'], 'x':[1,2,3,4,5], 'y':[6,7,8,9,10]})
print(df)
df['where'] = np.where(df.country=='CA', df.x, df.y)
df['apply'] = df.apply(lambda row: row.x if row.country == 'CA' else row.y, axis=1)
mask = df.country=='CA'
df.loc[mask, 'mask'] = df.loc[mask, 'x']
df.loc[~mask, 'mask'] = df.loc[~mask, 'y']
print(df)
输入:
country x y
0 CA 1 6
1 US 2 7
2 CA 3 8
3 UK 4 9
4 CA 5 10
输出
country x y where apply mask
0 CA 1 6 1 1 1.0
1 US 2 7 7 7 7.0
2 CA 3 8 3 3 3.0
3 UK 4 9 9 9 9.0
4 CA 5 10 5 5 5.0