Mig*_*lez 2 python split dataframe pandas
我有一个名为 tweetscrypto 的数据框,我试图从“text”列中删除以字符“@”开头的所有单词,并将结果收集到新列“clean_text”中。其余的单词应该保持完全相同:
tweetscrypto['clean_text'] = tweetscrypto['text'].apply(filter(lambda x:x[0]!='@', x.split()))
Run Code Online (Sandbox Code Playgroud)
它似乎不起作用。有人可以帮忙吗?
提前致谢
在这种情况下,出于可读性的目的,定义一个方法可能比使用 lambda 更好。
def clean_text(X):
X = X.split()
X_new = [x for x in X if not x.startswith("@")
return ' '.join(X_new)
tweetscrypto['clean_text'] = tweetscrypto['text'].apply(clean_text)
Run Code Online (Sandbox Code Playgroud)
请str.replace以以下字符串开头@
样本数据
text
0 News via @livemint: @RBI bars banks from links
1 Newsfeed from @oayments_source: How Africa
2 is that bitcoin? not my thing
tweetscrypto['clean_text']=tweetscrypto['text'].str.replace('(\@\w+.*?)',"")
Run Code Online (Sandbox Code Playgroud)
尽管如此,仍可以捕获@而无需逃脱,如所指出的@baxx
tweetscrypto['clean_text']=tweetscrypto['text'].str.replace('(@\w+.*?)',"")
clean_text
0 News via : bars banks from links
1 Newsfeed from : How Africa
2 is that bitcoin? not my thing
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1991 次 |
| 最近记录: |