我正在尝试解析存储在 .csv 文件中名为“text”的列中的推文。我想使用正则表达式、TweetTokenizer 等,但这都要求文本采用字符串形式(据我所知)。
我看到了这篇文章:
Parsing a tweet inside a csv column in Python
但对我来说,代码对于查找主题标签来说过于具体。我确实想这样做,但是有人知道如何更普遍地将“文本”列中的文本转换为字符串以便我可以解析吗?
谢谢, 朋朋
最佳答案
读取 csv 文件时,文本列应作为字符串导入:
df = pd.read_csv('tweet.csv')
print(df)
输出:
user text
0 scotthamilton is upset that he can't update his Facebook by ...
1 mattycus @Kenichan I dived many times for the ball. Man...
2 ElleCTF my whole body feels itchy and like its on fire
3 Karoli @nationwideclass no, it's not behaving at all....
4 joy_wolf @Kwesidei not the whole crew
5 mybirch Need a hug
print(df.dtypes)
输出:
user object
text object
dtype: object
Pandas object
数据类型与 Python str
类型相同,用于文本。
如果确实需要将列类型转换为 str,可以使用以下命令:
df.text = df.text.astype(str)
关于python - 解析存储在 pandas 数据框列中的推文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55464976/