Pandas apply function

Viewed 162

I'm trying to apply a function to a pandas dataframe, the function I wanna apply is to create a new column with 'abc' as a value. But the output is not what i'm expecting. Here's the code with the input and the output:

import pandas as pd

df = pd.read_csv("test.csv")
print(df)

# Initial Dataframe
#      name  age
# 0    alex   25
# 1  audrey   22

def add_one(df):
    return df + 1

def create_col(df):
    df["new_col"] = 'abc'


df["age_1_year"] = df["age"].apply(add_one)
df["my_col"] = df.apply(create_col)
print(df)

#      name  age  age_1_year my_col
# 0    alex   25          26    NaN
# 1  audrey   22          23    NaN

I was expecting to have 'abc' in my column "my_col" but I got "NaN". Thanks in advance

4 Answers

With pandas you want to avoid apply as much as possible and instead use vectorized operations on the entire Series or DataFrame. When possible your method signatures should accept a Series, manipulate that Series and then return a Series you can assign back, or accept the DataFrame, manipulate the DataFrame and return the modified DataFrame.

So if you wanted to create a function to add one to a Series you would do:

def add_one(s: pd.Series):
    return s+1

df['age_one_year'] = add_one(df['age'])
#     name  age  age_one_year
#0    alex   25            26
#1  audrey   22            23

And if you wanted a function that creates a static value you could pass and return the DataFrame:

def add_static_column(df: pd.DataFrame, col_name, static_val):
    df[col_name] = static_val
    return df

df = add_static_column(df, 'new_col', 'abc')
#     name  age  age_one_year new_col
#0    alex   25            26     abc
#1  audrey   22            23     abc

Can you not do this:

df["age_1_year"] = df["age"] +1

df["my_col"] = 'abc'

Does something like this help?


import pandas as pd


d = {'age': [25, 22]}

def add_one(x):
    return x + 1

def create_col(row):
    row['my_col'] = 'abc'
    return row

df = pd.DataFrame(d, columns=['age'])
df["age_1_year"] = df["age"].apply(add_one)
df = df.apply(create_col, axis=1)

print(df)

   age  age_1_year my_col
0   25          26    abc
1   22          23    abc
import pandas as pd

df = pd.read_csv("test.csv")
print(df)

# Initial Dataframe
#      name  age
# 0    alex   25
# 1  audrey   22

def add_one(df):
    return df + 1

def create_col(df):
    return 'abc'


df["age_1_year"] = df["age"].apply(add_one)
df["my_col"] = df.apply(lambda x: create_col(x), axis=1)
##can also do
##df["my_col"] = df.pipe(create_col)
Related