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

Jon

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)
3kt

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]

編集
0

コメントを追加

0

関連記事

分類Dev

How to select result from dataframe according to specific pairs without iteration?

分類Dev

How to concatenate a specific column from a pandas.DataFrame()?

分類Dev

How to update a pandas dataframe, from multiple API calls?

分類Dev

How to update a pandas dataframe, from multiple API calls?

分類Dev

How to save pandas dataframe output from function to workspace?

分類Dev

Retrieve information from a specific row of a pandas dataframe

分類Dev

Pandas: combining results from function on subset of dataframe with the original dataframe

分類Dev

How to return result in series from R function?

分類Dev

How to pass schema to create a new Dataframe from existing Dataframe?

分類Dev

In pandas DataFrame, how can I store a specific value from a column into a variable, and then subsequently remove that value from the column?

分類Dev

Function to subset dataframe on optional arguments

分類Dev

Pandas DataFrame apply function to multiple columns and output multiple columns

分類Dev

Merge multiple dataframe pandas

分類Dev

Python - Pandas - Apply specific function to a given Level - Multi Index DataFrame

分類Dev

How to use a specific list of bins for multiple histograms from DataFrame, when using plotly+cufflinks?

分類Dev

How to update a pandas dataframe with sets, from another dataframe

分類Dev

How to create a Pandas DataFrame from a list of OrderedDicts?

分類Dev

How to convert from Pandas' DatetimeIndex to DataFrame in PySpark?

分類Dev

How to stay with a percent of data from a pandas DataFrame?

分類Dev

how to read multiple CSV files from folder into pandas with dataframe name as file name

分類Dev

Sum of specific rows in a dataframe (Pandas)

分類Dev

How to pass list of custom functions to pandas.Dataframe.aggregate

分類Dev

How to pass a dataframe column as an argument in a function using piping?

分類Dev

Pandas DataFrame - How to retrieve specific combinations of MultiIndex levels

分類Dev

How to put specific dictionary values in dataframe columns (pandas)

分類Dev

How to create specific DataFrame based on other df in Pandas?

分類Dev

How do I reduce a spark dataframe from kafka and collect the result?

分類Dev

Remore rows from pandas dataframe with logics based on multiple column

分類Dev

append multiple sheets from excel into pandas dataframe - sort problems

Related 関連記事

  1. 1

    How to select result from dataframe according to specific pairs without iteration?

  2. 2

    How to concatenate a specific column from a pandas.DataFrame()?

  3. 3

    How to update a pandas dataframe, from multiple API calls?

  4. 4

    How to update a pandas dataframe, from multiple API calls?

  5. 5

    How to save pandas dataframe output from function to workspace?

  6. 6

    Retrieve information from a specific row of a pandas dataframe

  7. 7

    Pandas: combining results from function on subset of dataframe with the original dataframe

  8. 8

    How to return result in series from R function?

  9. 9

    How to pass schema to create a new Dataframe from existing Dataframe?

  10. 10

    In pandas DataFrame, how can I store a specific value from a column into a variable, and then subsequently remove that value from the column?

  11. 11

    Function to subset dataframe on optional arguments

  12. 12

    Pandas DataFrame apply function to multiple columns and output multiple columns

  13. 13

    Merge multiple dataframe pandas

  14. 14

    Python - Pandas - Apply specific function to a given Level - Multi Index DataFrame

  15. 15

    How to use a specific list of bins for multiple histograms from DataFrame, when using plotly+cufflinks?

  16. 16

    How to update a pandas dataframe with sets, from another dataframe

  17. 17

    How to create a Pandas DataFrame from a list of OrderedDicts?

  18. 18

    How to convert from Pandas' DatetimeIndex to DataFrame in PySpark?

  19. 19

    How to stay with a percent of data from a pandas DataFrame?

  20. 20

    how to read multiple CSV files from folder into pandas with dataframe name as file name

  21. 21

    Sum of specific rows in a dataframe (Pandas)

  22. 22

    How to pass list of custom functions to pandas.Dataframe.aggregate

  23. 23

    How to pass a dataframe column as an argument in a function using piping?

  24. 24

    Pandas DataFrame - How to retrieve specific combinations of MultiIndex levels

  25. 25

    How to put specific dictionary values in dataframe columns (pandas)

  26. 26

    How to create specific DataFrame based on other df in Pandas?

  27. 27

    How do I reduce a spark dataframe from kafka and collect the result?

  28. 28

    Remore rows from pandas dataframe with logics based on multiple column

  29. 29

    append multiple sheets from excel into pandas dataframe - sort problems

ホットタグ

アーカイブ