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!
df['Code'] = df.apply(lambda x: generateCode(), axis=1)