Lets say I have a the following pandas data frame with the following columnar structure and the dataframe is titled df
index column1 column2 column3
0 2 5 apple
1 4 3 apple
2 6 1 orange
3 8 6 apple
4 10 5 orange
I would like to search the dataframe such that it will recognize every row where df['column3'] == orange
and extract the value of df['column1']
and df['column2']
in that row and insert it into the below function and then change the existing value of df[column2']
by the output of the function.
def func(x, y):
return x * 2.0
Thus far I have implemented the following, which works, but I suspect it is not the most pythonic way of doing this, and probably does not have the most efficient execution speed. Any advice would be appreciated.
for i in range(len(df.index)):
if df.loc[i, 'column3'] == 'orange':
df.loc[i, 'column2'] = func(df.column1, df.column2)
Nest your condition in an apply:
In [26]: df
Out[26]:
column1 column2 column3
index
0 2 5 apple
1 4 3 apple
2 6 1 orange
3 8 6 apple
4 10 5 orange
In [27]: df['column2'] = df.apply(lambda x: func(x['column1'], x['column2']) \
if x['column3'] == 'orange' else x['column2'], axis=1)
In [28]: df
Out[28]:
column1 column2 column3
index
0 2 5.0 apple
1 4 3.0 apple
2 6 12.0 orange
3 8 6.0 apple
4 10 20.0 orange
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加