Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using split() function on a pandas dataframe

I have the following dataframe:

enter image description here

I'm trying to get rid of the percentage signs. In order to do this I decided to apply a function to the Democrat and Republican column and try to split() by the percentage sign. The following code tries to do that:

gallup_2012[['Democrat/Lean Democratic', 'Republican/Lean 
Republican']].apply(lambda x: x.split('%')[0])

However, when I try to do this, I get the following error:

("'Series' object has no attribute 'split'", u'occurred at index Democrat/Lean > Democratic')

I'm not quite sure why this error occurs, as I can apply other functions to this series. It's just that the split() function doesn't work.

Any help would be appreciated!

like image 837
bugsyb Avatar asked Jul 25 '17 02:07

bugsyb


2 Answers

df[[ ]] returns a dataframe, so if you use df.apply() then it would be applied on pd.Series. And Series doesn't have split() method, But if you use df[ ] and use df.apply() then you would be able to achieve what you want. The drawback is only that you can apply only on one column.

gallup_2012['Democrat/Lean Democratic'].apply(lambda x: x.split('%')[0])
like image 190
ksai Avatar answered Sep 20 '22 22:09

ksai


You could use the str.replace method on the desired columns

df["column"] = df["column"].str.replace("%", "")
like image 20
Henrique Coura Avatar answered Sep 20 '22 22:09

Henrique Coura