Raheel
Raheel

Reputation: 9024

Pandas df.apply does not modify DataFrame

I am just starting pandas so please forgive if this is something stupid.

I am trying to apply a function to a column but its not working and i don't see any errors also.

 capitalizer = lambda x: x.upper()
    for df in pd.read_csv(downloaded_file, chunksize=2, compression='gzip', low_memory=False):
        df['level1'].apply(capitalizer)
        print df
        exit(1)

This print shows the level1 column values same as the original csv not doing upper. Am i missing something here ?

Thanks

Upvotes: 8

Views: 5691

Answers (2)

cs95
cs95

Reputation: 402263

apply is not an inplace function - it does not modify values in the original object, so you need to assign it back:

df['level1'] = df['level1'].apply(capitalizer)

Alternatively, you can use str.upper, it should be much faster.

df['level1'] = df['level1'].str.upper()

Upvotes: 22

gB08
gB08

Reputation: 182

df['level1'] = map(lambda x: x.upper(), df['level1'])

you can use above code to make your column uppercase

Upvotes: 2

Related Questions