Apply function over rows in pandas df

foobarbaz

In a pandas df, I have a column ['name'] with various Operating System classifications such as 'Windows 7', 'Windows 10', 'Linux', 'Mobile iOS 9.1', 'OS X 10.12', etc. That are strings.

I am hoping to use this function to create a new column ['type'] that will be a more generalized version:

def name_group(row):
    if 'Windows' in row:
        name = 'Microsoft Windows'
    elif 'iOS' in row:
        name = 'Apple iOS'
    elif 'OS X' in row:
        name ='Apple Macintosh'
    elif 'Macintosh' in row:
        name = 'Apple Macintosh'
    elif 'Linux' in row:
        name = 'GNU/Linux'
    else:
        name = 'Other'
    return name

It works correctly when I test the function by passing in a single string variable, but for some reason when I apply the function to the df like this, it only returns "other" for each row.

new_df['type'] = new_df.apply(name_group, axis=1)

Any thoughts on what could be causing this?

jezrael

You need pass column name with Series.apply:

new_df['type'] = new_df['name'].apply(name_group)

But if want use DataFrame.apply then need lambda function and pass name of column too:

new_df['type'] = new_df.apply(lambda x: name_group(x['name']), axis=1)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Apply function over relative rows in Pandas

From Dev

Iterate over rows in a pandas dataframe and apply a lambda function

From Dev

Apply a share function on row pandas DF

From Dev

Pandas: Iterating over sorted rows in a df, implementing a counter

From Dev

pandas apply function to multiple columns and multiple rows

From Dev

Apply a match and replace function over series of rows in a dataframe in order

From Dev

pandas apply function that returns multiple values to rows in pandas dataframe

From Dev

Using a shift() function within an apply function to compare rows in a Pandas Dataframe

From Dev

Duplicate rows in pandas DF

From Dev

Pandas df unstack rows

From Dev

Pandas: Apply function over each pair of columns under constraints

From Dev

python pandas: groupby apply function looks at prior rows

From Dev

Apply function to pandas dataframe row using values in other rows

From Dev

Loop over multiple df and apply factor labels

From Dev

R data.table apply function with multiple column input over all rows and get reasonable output

From Dev

What is a proper idiom in pandas for creating a dataframes from the output of a apply function on a df?

From Dev

Replace rows in a Pandas df with rows from another df

From Dev

Apply function with args in pandas

From Dev

Alternative to apply function in pandas

From Java

How to apply regex over all the rows of a dataset?

From Dev

How to iterate over rows using apply to a dataframe?

From Dev

Iterate over groups of rows in Pandas

From Dev

pandas iteration over rows as dict

From Dev

Apply function on the rows of a matrix in R

From Dev

Apply function on multiple rows of matrix

From Dev

python pandas: passing in dataframe to df.apply

From Dev

Creating a loop program to apply values to pandas df

From Dev

pandas str split and apply, create multiindex df

From Dev

python pandas: passing in dataframe to df.apply

Related Related

  1. 1

    Apply function over relative rows in Pandas

  2. 2

    Iterate over rows in a pandas dataframe and apply a lambda function

  3. 3

    Apply a share function on row pandas DF

  4. 4

    Pandas: Iterating over sorted rows in a df, implementing a counter

  5. 5

    pandas apply function to multiple columns and multiple rows

  6. 6

    Apply a match and replace function over series of rows in a dataframe in order

  7. 7

    pandas apply function that returns multiple values to rows in pandas dataframe

  8. 8

    Using a shift() function within an apply function to compare rows in a Pandas Dataframe

  9. 9

    Duplicate rows in pandas DF

  10. 10

    Pandas df unstack rows

  11. 11

    Pandas: Apply function over each pair of columns under constraints

  12. 12

    python pandas: groupby apply function looks at prior rows

  13. 13

    Apply function to pandas dataframe row using values in other rows

  14. 14

    Loop over multiple df and apply factor labels

  15. 15

    R data.table apply function with multiple column input over all rows and get reasonable output

  16. 16

    What is a proper idiom in pandas for creating a dataframes from the output of a apply function on a df?

  17. 17

    Replace rows in a Pandas df with rows from another df

  18. 18

    Apply function with args in pandas

  19. 19

    Alternative to apply function in pandas

  20. 20

    How to apply regex over all the rows of a dataset?

  21. 21

    How to iterate over rows using apply to a dataframe?

  22. 22

    Iterate over groups of rows in Pandas

  23. 23

    pandas iteration over rows as dict

  24. 24

    Apply function on the rows of a matrix in R

  25. 25

    Apply function on multiple rows of matrix

  26. 26

    python pandas: passing in dataframe to df.apply

  27. 27

    Creating a loop program to apply values to pandas df

  28. 28

    pandas str split and apply, create multiindex df

  29. 29

    python pandas: passing in dataframe to df.apply

HotTag

Archive