在 Pandas 数据帧上使用 split() 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45292833/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 04:05:12  来源:igfitidea点击:

Using split() function on a pandas dataframe

pythonpandassplit

提问by bugsyb

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:

我试图摆脱百分比符号。为了做到这一点,我决定对民主党和共和党专栏应用一个函数,并尝试按百分比符号 split()。下面的代码试图做到这一点:

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')

(“'Series' 对象没有属性 'split'”,你'发生在索引 Democrat/Lean > 民主')

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.

我不太清楚为什么会出现这个错误,因为我可以将其他函数应用到这个系列中。只是 split() 函数不起作用。

Any help would be appreciated!

任何帮助,将不胜感激!

回答by ksai

df[[ ]]returns a dataframe, so if you use df.apply()then it would be applied on pd.Series. And Seriesdoesn'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.

df[[ ]]返回一个数据帧,因此如果您使用df.apply()它,那么它将应用于pd.Series. 并且Series没有split()方法,但是如果您使用df[ ]并使用df.apply()那么您将能够实现您想要的。缺点是只能应用于一列。

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

回答by Henrique Coura

You could use the str.replacemethod on the desired columns

您可以在所需的列上使用str.replace方法

df["column"] = df["column"].str.replace("%", "")