chinmaykelkar
chinmaykelkar

Reputation: 111

how to replace values in columns by new values in pandas

I have this code:

def chgFormat(x):
    newFormat = 0
    if x[-1] == 'K':
        if len(x)==1:newFormat=1000
        else:newFormat = float(x[:-1])*1000
    elif x[-1] == 'M':
        newFormat = float(x[:-1]) * 100000
    elif x[-1] == 'B':
        newFormat = float(x[:-1]) * 1000000
    elif x[-1] == 'H':
        newFormat = float(x[:-1]) * 100
    elif x[-1] == 'h':
        newFormat = float(x[:-1]) * 100
    else:
        newFormat = float(x)
    return newFormat


frame=pd.read_csv(folderpath+"/StormEvents_details-ftp_v1.0_d1951_c20160223.csv",index_col=None,encoding = "ISO-8859-1",header=0,low_memory=False)
frame['DAMAGE_PROPERTY'].fillna('0K',inplace=True)
frame['DAMAGE_PROPERTY'].apply(chgFormat)

This code converts data entries like 2K, 3K, 3H, 3B into 2000.0, 3000.0, 300.0 etc.

What I want is this code should replace column entries by the values calculated by vales in written function. If I do print(frame['DAMAGE_PROPERTY']) after the written code, I still see the original values.

Upvotes: 0

Views: 73

Answers (1)

Shijo
Shijo

Reputation: 9711

You may try this

frame['DAMAGE_PROPERTY'] = frame['DAMAGE_PROPERTY'].apply(chgFormat, axis=1)

Upvotes: 1

Related Questions