5

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!

1
  • Just Add df['Code'] = df.apply(lambda x: generateCode(), axis=1) Commented Oct 16, 2020 at 8:44

2 Answers 2

4

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
Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

Actually I´ve been making some prints and I think the problems was that you didnt specify a column so it returned a whole row instead of just a series with each row value. df.Brand.apply(lambda x: generateCode()), this also worked by the way

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.