提取csv列中特殊字符之间的字符串

2024-09-30 01:36:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我想从转发中提取userhandles,即 “RT@用户名:xyzxyzxyz“到新列。我做了以下事情

df = pd.read_csv("string.csv")
for index,row in df.iterrows(): 
    df['Influencers'] = df['Tweet'].str.extract("\(@*?)\:")
df.to_csv('string3.csv', index=False)

它产生了以下错误:

^{pr2}$

样品DF:

df=pd.DataFrame({"Tweet": ["RT @saikatd: Are editors involved in the transfer of Income Tax officials?","RT @CLManojET: Can't allow L-G's fantasy of running a parallel administration"," Fairplay n equity 2 consumers 2 be ensured"]})

Tags: ofcsvindfforreadstringindex
2条回答

对不起,我解决了这个问题,但对于上述情况我无法实现其他条件:

df = pd.read_csv("string.csv")
for index,row in df.iterrows(): 
    if "RT @" in row["Tweet"]: 
        df['Influencers'] = "@"+df['Tweet'].str.extract("\@(.+?)\:", expand= False)
    else :
        df['Influencers'] = "Original"
df.to_csv('string3.csv', index=False)

它为else条件生成空白行。在

试试这个:

df = pd.read_csv("string.csv")
df['Influencers'] = df['Tweet'].str.extract("RT\s+(\@[^\:]*)", expand=False)

更新:

^{pr2}$

相关问题 更多 >

    热门问题