简体   繁体   中英

How to pass multiple arguments from a pandas dataframe to a function and return the result to the datframe at specific locations in the dataframe

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)

There is no need to use apply .

You can simply use loc and a mask .

mask = df['column3'] == "orange"
df.loc[mask, "column2"] = func(df.loc[mask].column1, df.loc[mask].column2)

This is simpler and faster than apply.

Using pd.DataFrame.apply , you can define a function which is applied to each row sequentially. Note that the row is passed to your function as a series object and may be unpacked into component fields via the syntax row['col_name'] .

As this method is just a thinly veiled loop, you are advised, where possible, to use a vectorised solution where possible.

def func(row):
    x = row['column1']
    y = row['column2']
    if row['column3'] == 'orange':
        return x * 2.0
    else:
        return y

df['column2'] = df.apply(func, axis=1)

print(df)

   index  column1  column2 column3
0      0        2      5.0   apple
1      1        4      3.0   apple
2      2        6     12.0  orange
3      3        8      6.0   apple
4      4       10     20.0  orange

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM