Nils
Nils

Reputation: 81

Pandas Dataframe add column with lambda function

I am trying to add a column to my Pandas Dataframe with a random generated code of two uppercase letters. For the random code generation i wrote a function, but when i add it to the dataframe it gives me only NaN as result. Any idea how to fix this?

import pandas as pd
import random
import string

def generateCode():
    return random.choice(string.ascii_uppercase) + random.choice(string.ascii_uppercase)

cars = {'Brand': ['Honda Civic','Toyota Corolla','Ford Focus','Audi A4'],
        'Price': [22000,25000,27000,35000]
        }
df = pd.DataFrame(cars, columns = ['Brand', 'Price'])

df['Code'] = df.apply(lambda x: generateCode())

print(df)

gives the output:

            Brand  Price Code
0     Honda Civic  22000  NaN
1  Toyota Corolla  25000  NaN
2      Ford Focus  27000  NaN
3         Audi A4  35000  NaN

Appreciate any help!

Upvotes: 5

Views: 6707

Answers (2)

jezrael
jezrael

Reputation: 862406

Use Series.apply for processing per each value:

df['Code'] = df['Brand'].apply(lambda x: generateCode())

print(df)
            Brand  Price Code
0     Honda Civic  22000   AY
1  Toyota Corolla  25000   EN
2      Ford Focus  27000   VN
3         Audi A4  35000   ZZ

Or DataFrame.apply with axis=1 for processing per rows:

df['Code'] = df.apply(lambda x: generateCode(), axis=1)

print(df)
            Brand  Price Code
0     Honda Civic  22000   AY
1  Toyota Corolla  25000   EN
2      Ford Focus  27000   VN
3         Audi A4  35000   ZZ

Upvotes: 4

luso97
luso97

Reputation: 39

Don´t ask me why, but this actually worked:

import pandas as pd
import random
import string

def generateCode():
    print(random.choice(string.ascii_uppercase) + random.choice(string.ascii_uppercase))
    return random.choice(string.ascii_uppercase) + random.choice(string.ascii_uppercase)

cars = {'Brand': ['Honda Civic','Toyota Corolla','Ford Focus','Audi A4'],
        'Price': [22000,25000,27000,35000]
        }
df = pd.DataFrame(cars, columns = ['Brand', 'Price'])

df['Code'] = df.Brand.map(lambda x: generateCode())

print(df)

If someone can explain why this worked, I would be grateful

Upvotes: 0

Related Questions