提问人:user7289 提问时间:11/12/2013 最后编辑:wjandreauser7289 更新时间:9/25/2023 访问量:817755
如何创建一个新列,其中的值是根据现有列选择的?
How do I create a new column where the values are selected based on existing columns?
问:
如何将列添加到以下数据帧中,以便 if 和 else?color
color='green'
Set == 'Z'
color='red'
Type Set
1 A Z
2 B Z
3 B X
4 C Y
答:
如果您只有两个选项可供选择,请使用 np.where
:
df['color'] = np.where(df['Set']=='Z', 'green', 'red')
例如
import pandas as pd
import numpy as np
df = pd.DataFrame({'Type':list('ABBC'), 'Set':list('ZZXY')})
df['color'] = np.where(df['Set']=='Z', 'green', 'red')
print(df)
收益 率
Set Type color
0 Z A green
1 Z B green
2 X B red
3 Y C red
如果您有两个以上的条件,请使用 np.select
。例如,如果你想color
yellow
什么时候(df['Set'] == 'Z') & (df['Type'] == 'A')
- 否则,当
blue
(df['Set'] == 'Z') & (df['Type'] == 'B')
- 否则,当
purple
(df['Type'] == 'B')
- 否则
black
然后使用
df = pd.DataFrame({'Type':list('ABBC'), 'Set':list('ZZXY')})
conditions = [
(df['Set'] == 'Z') & (df['Type'] == 'A'),
(df['Set'] == 'Z') & (df['Type'] == 'B'),
(df['Type'] == 'B')]
choices = ['yellow', 'blue', 'purple']
df['color'] = np.select(conditions, choices, default='black')
print(df)
这会产生
Set Type color
0 Z A yellow
1 Z B blue
2 X B purple
3 Y C black
实现这一目标的另一种方式是
df['color'] = df.Set.map( lambda x: 'red' if x == 'Z' else 'green')
列表推导是有条件地创建另一列的另一种方法。如果在列中处理对象 dtypes(如示例中所示),则列表推导式通常优于大多数其他方法。
示例列表推导式:
df['color'] = ['red' if x == 'Z' else 'green' for x in df['Set']]
%timeit 测试:
import pandas as pd
import numpy as np
df = pd.DataFrame({'Type':list('ABBC'), 'Set':list('ZZXY')})
%timeit df['color'] = ['red' if x == 'Z' else 'green' for x in df['Set']]
%timeit df['color'] = np.where(df['Set']=='Z', 'green', 'red')
%timeit df['color'] = df.Set.map( lambda x: 'red' if x == 'Z' else 'green')
1000 loops, best of 3: 239 µs per loop
1000 loops, best of 3: 523 µs per loop
1000 loops, best of 3: 263 µs per loop
评论
pd.DataFrame({'Type':list('ABBC')*100000, 'Set':list('ZZXY')*100000})
numpy.where
map
numpy.where
df['color'] = ['red' if (x['Set'] == 'Z') & (x['Type'] == 'B') else 'green' for x in df]
df['color_type'] = np.where(df['Set']=='Z', 'green', df['Type'])
.iterrows()
以下方法比此处定时的方法慢,但我们可以根据多列的内容计算额外的列,并且可以为额外的列计算两个以上的值。
仅使用“Set”列的简单示例:
def set_color(row):
if row["Set"] == "Z":
return "red"
else:
return "green"
df = df.assign(color=df.apply(set_color, axis=1))
print(df)
Set Type color
0 Z A red
1 Z B red
2 X B green
3 Y C green
考虑更多颜色和更多列的示例:
def set_color(row):
if row["Set"] == "Z":
return "red"
elif row["Type"] == "C":
return "blue"
else:
return "green"
df = df.assign(color=df.apply(set_color, axis=1))
print(df)
Set Type color
0 Z A red
1 Z B red
2 X B green
3 Y C blue
编辑(21/06/2019):使用plydata
也可以使用 plydata 来做这种事情(这似乎比使用 and 更慢)。assign
apply
from plydata import define, if_else
简单:if_else
df = define(df, color=if_else('Set=="Z"', '"red"', '"green"'))
print(df)
Set Type color
0 Z A red
1 Z B red
2 X B green
3 Y C green
嵌 套:if_else
df = define(df, color=if_else(
'Set=="Z"',
'"red"',
if_else('Type=="C"', '"green"', '"blue"')))
print(df)
Set Type color
0 Z A red
1 Z B red
2 X B blue
3 Y C green
这是给这只猫剥皮的另一种方法,使用字典将新值映射到列表中的键上:
def map_values(row, values_dict):
return values_dict[row]
values_dict = {'A': 1, 'B': 2, 'C': 3, 'D': 4}
df = pd.DataFrame({'INDICATOR': ['A', 'B', 'C', 'D'], 'VALUE': [10, 9, 8, 7]})
df['NEW_VALUE'] = df['INDICATOR'].apply(map_values, args = (values_dict,))
它是什么样子的:
df
Out[2]:
INDICATOR VALUE NEW_VALUE
0 A 10 1
1 B 9 2
2 C 8 3
3 D 7 4
当您要创建许多 -type 语句(即要替换许多唯一值)时,此方法可能非常强大。ifelse
当然,你总是可以这样做的:
df['NEW_VALUE'] = df['INDICATOR'].map(values_dict)
但是这种方法的速度是我的机器上从上面的方法的三倍多。apply
您也可以使用以下方法执行此操作:dict.get
df['NEW_VALUE'] = [values_dict.get(v, None) for v in df['INDICATOR']]
评论
.apply()
.map()
您可以简单地使用强大的方法,并根据需要使用一个或多个条件(使用 pandas=1.0.5 进行测试)。.loc
代码摘要:
df=pd.DataFrame(dict(Type='A B B C'.split(), Set='Z Z X Y'.split()))
df['Color'] = "red"
df.loc[(df['Set']=="Z"), 'Color'] = "green"
#practice!
df.loc[(df['Set']=="Z")&(df['Type']=="B")|(df['Type']=="C"), 'Color'] = "purple"
解释:
df=pd.DataFrame(dict(Type='A B B C'.split(), Set='Z Z X Y'.split()))
# df so far:
Type Set
0 A Z
1 B Z
2 B X
3 C Y
添加“颜色”列并将所有值设置为“红色”
df['Color'] = "red"
应用您的单一条件:
df.loc[(df['Set']=="Z"), 'Color'] = "green"
# df:
Type Set Color
0 A Z green
1 B Z green
2 B X red
3 C Y red
或多个条件(如果需要):
df.loc[(df['Set']=="Z")&(df['Type']=="B")|(df['Type']=="C"), 'Color'] = "purple"
您可以在此处阅读有关 Pandas 逻辑运算符和条件选择的信息:Pandas 中布尔索引的逻辑运算符
一个带有方法的衬里如下:.apply()
df['color'] = df['Set'].apply(lambda set_: 'green' if set_=='Z' else 'red')
之后,数据框如下所示:df
>>> print(df)
Type Set color
0 A Z green
1 B Z green
2 B X red
3 C Y red
如果您正在处理海量数据,最好采用记忆方法:
# First create a dictionary of manually stored values
color_dict = {'Z':'red'}
# Second, build a dictionary of "other" values
color_dict_other = {x:'green' for x in df['Set'].unique() if x not in color_dict.keys()}
# Next, merge the two
color_dict.update(color_dict_other)
# Finally, map it to your column
df['color'] = df['Set'].map(color_dict)
当您有许多重复值时,此方法将最快。我的一般经验法则是记住以下情况:data_size
> 10**4
& n_distinct
< data_size/4
例如,在案例中记住 10,000 行,其中 2,500 个或更少的非重复值。
评论
random.choices()
df['color'] = 'green'
df['color'] = df['color'].where(df['Set']=='Z', other='red')
# Replace values where the condition is False
或
df['color'] = 'red'
df['color'] = df['color'].mask(df['Set']=='Z', other='green')
# Replace values where the condition is True
或者,您可以将该方法与 lambda 函数一起使用:transform
df['color'] = df['Set'].transform(lambda x: 'green' if x == 'Z' else 'red')
输出:
Type Set color
1 A Z green
2 B Z green
3 B X red
4 C Y red
与@chai的性能比较:
import pandas as pd
import numpy as np
df = pd.DataFrame({'Type':list('ABBC')*1000000, 'Set':list('ZZXY')*1000000})
%timeit df['color1'] = 'red'; df['color1'].where(df['Set']=='Z','green')
%timeit df['color2'] = ['red' if x == 'Z' else 'green' for x in df['Set']]
%timeit df['color3'] = np.where(df['Set']=='Z', 'red', 'green')
%timeit df['color4'] = df.Set.map(lambda x: 'red' if x == 'Z' else 'green')
397 ms ± 101 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
976 ms ± 241 ms per loop
673 ms ± 139 ms per loop
796 ms ± 182 ms per loop
如果您只有 2 个选择,请使用np.where()
df = pd.DataFrame({'A':range(3)})
df['B'] = np.where(df.A>2, 'yes', 'no')
如果您有超过 2 个选择,也许可以工作
输入apply()
arr = pd.DataFrame({'A':list('abc'), 'B':range(3), 'C':range(3,6), 'D':range(6, 9)})
和 arr 是
A B C D
0 a 0 3 6
1 b 1 4 7
2 c 2 5 8
如果希望列 Eif arr.A =='a' then arr.B elif arr.A=='b' then arr.C elif arr.A == 'c' then arr.D else something_else
arr['E'] = arr.apply(lambda x: x['B'] if x['A']=='a' else(x['C'] if x['A']=='b' else(x['D'] if x['A']=='c' else 1234)), axis=1)
最后 arr 是
A B C D E
0 a 0 3 6 0
1 b 1 4 7 4
2 c 2 5 8 8
pyjanitor 的 case_when 函数是一个包装器,为多种条件提供了可链接/方便的形式:pd.Series.mask
对于单个条件:
df.case_when(
df.col1 == "Z", # condition
"green", # value if True
"red", # value if False
column_name = "color"
)
Type Set color
1 A Z green
2 B Z green
3 B X red
4 C Y red
对于多种情况:
df.case_when(
df.Set.eq('Z') & df.Type.eq('A'), 'yellow', # condition, result
df.Set.eq('Z') & df.Type.eq('B'), 'blue', # condition, result
df.Type.eq('B'), 'purple', # condition, result
'black', # default if none of the conditions evaluate to True
column_name = 'color'
)
Type Set color
1 A Z yellow
2 B Z blue
3 B X purple
4 C Y black
更多例子可以在这里找到
使用以下方法的不那么冗长的方法:np.select
a = np.array([['A','Z'],['B','Z'],['B','X'],['C','Y']])
df = pd.DataFrame(a,columns=['Type','Set'])
conditions = [
df['Set'] == 'Z'
]
outputs = [
'Green'
]
# conditions Z is Green, Red Otherwise.
res = np.select(conditions, outputs, 'Red')
res
array(['Green', 'Green', 'Red', 'Red'], dtype='<U5')
df.insert(2, 'new_column',res)
df
Type Set new_column
0 A Z Green
1 B Z Green
2 B X Red
3 C Y Red
df.to_numpy()
array([['A', 'Z', 'Green'],
['B', 'Z', 'Green'],
['B', 'X', 'Red'],
['C', 'Y', 'Red']], dtype=object)
%%timeit conditions = [df['Set'] == 'Z']
outputs = ['Green']
np.select(conditions, outputs, 'Red')
134 µs ± 9.71 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
df2 = pd.DataFrame({'Type':list('ABBC')*1000000, 'Set':list('ZZXY')*1000000})
%%timeit conditions = [df2['Set'] == 'Z']
outputs = ['Green']
np.select(conditions, outputs, 'Red')
188 ms ± 26.5 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
这里有一个简单的单行字,当你有一个或多个条件时,你可以使用:
df['color'] = np.select(condlist=[df['Set']=="Z", df['Set']=="Y"], choicelist=["green", "yellow"], default="red")
简单易行!
在此处查看更多信息:https://numpy.org/doc/stable/reference/generated/numpy.select.html
评论