pandas - Faster method for changing row entries? -


i have pandas dataframe follows:

in [55]: df.head() out[55]:            country  energy supply  energy supply per capita  % renewable 0     afghanistan   3.210000e+08                      10.0    78.669280 1         albania   1.020000e+08                      35.0   100.000000 2         algeria1   1.959000e+09                      51.0     0.551010 3  american samoa            nan                       nan     0.641026 4         andorra   9.000000e+06                     121.0    88.695650 

and suppose want remove every numeric character each entries in df['country'] wrote following code:

in [15]: c in energy['country']:    ....:     c = ''.join([i in c if not i.isdigit()])    ....:      

and when call df.head(), output same i.e no changes @ all. far know method assigns new value variable c doesn't make changes in dataframe(am right?)

so tried new code:

in [51]: k = 0  in [52]: c in df['country']:    ....:     df.loc[k, "country"] = ''.join([i in c if not i.isdigit()])    ....:     k += 1    ....:      

and worked. surely know slow method(2nd one),is there faster method available?

you can utilize pandas built-in string operation, str.replace()

df['country'] = df['country'].str.replace('\d','') 

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -