基于另一列的值,我想用for循环填充新列。遗憾的是没有得到我需要的结果;
profit = []
# For each row in the column,
for row in df3['Result']:
# if value is;
if row == 'H':
# Append a Profit/Loss
profit.append(df3['column value H'])
# else, if value is,
elif row == 'D':
# Append a Profit/Loss
profit.append(df3['column value D'])
# otherwise,
else:
# Append a Profit/Loss
profit.append(df3['column value A'])
df3['profit'] = profit
最佳答案
我认为您需要加倍numpy.where
:
df3['profit'] = np.where(df3['Result'] == 'H', df3['column value H'],
np.where(df3['Result'] == 'D', df3['column value D'], df3['column value A']))
样品:
df3 = pd.DataFrame({'Result':['H','D','E'],
'column value H':[4,5,6],
'column value D':[7,8,9],
'column value A':[1,3,5]})
print (df3)
Result column value A column value D column value H
0 H 1 7 4
1 D 3 8 5
2 E 5 9 6
df3['profit'] = np.where(df3['Result'] == 'H', df3['column value H'],
np.where(df3['Result'] == 'D', df3['column value D'], df3['column value A']))
print (df3)
Result column value A column value D column value H profit
0 H 1 7 4 4
1 D 3 8 5 8
2 E 5 9 6 5
时间:
In [198]: %timeit (jez(df3))
100 loops, best of 3: 7.59 ms per loop
In [199]: %timeit (wwii(df4))
1 loop, best of 3: 1.49 s per loop
In [200]: %timeit (wwii1(df5))
1 loop, best of 3: 4.48 s per loop
测试代码:
df3 = pd.DataFrame({'Result':['H','D','E'],
'column value H':[4,5,6],
'column value D':[7,8,9],
'column value A':[1,3,5]})
print (df3)
df3 = pd.concat([df3]*10000).reset_index(drop=True)
df4 = df3.copy()
df5 = df3.copy()
def jez(df3):
df3['profit'] = np.where(df3['Result'] == 'H', df3['column value H'],
np.where(df3['Result'] == 'D', df3['column value D'], df3['column value A']))
return (df3)
def foo(series):
# d maps Result column values to DataFrame/Series column names
d = {'H':'column value H', 'D':'column value D'}
try:
return series[d[series['Result']]]
except KeyError as e:
return series['column value A']
def wwii(df3):
df3['Profit'] = df3.apply(foo, axis = 1)
return df3
def wwii1(df3):
profit = []
for row in df3.iterrows():
series = row[1]
if series.Result == 'H':
# Append a Profit/Loss
profit.append(series['column value H'])
# else, if value is,
elif series.Result == 'D':
# Append a Profit/Loss
profit.append(series['column value D'])
# otherwise,
else:
# Append a Profit/Loss
profit.append(series['column value A'])
df3['profit'] = profit
return df3
print (jez(df3))
print (wwii(df4))
print (wwii1(df5))
关于python - 试图用for循环填充数据框中的新列,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40636572/