我正在处理.txt数据集,并将其作为csv文件读取。

data = pd.read_csv('train.txt', delimiter='\t', header=None, names=['category', 'text'], dtype=str)
print data.head()


它打印:

0  MUSIC  Today at the recording studio, John...
1  POLITICS  The tensions inside the government have...
2  NEWS  The new pictures of NASA show...


我要做的是将所有字母从文本更改为小写。因此,例如,“ NASA节目的新图片...”将变为“ NASA节目的新图片...”,但“ NEWS”仍大写为“ NEWS”。

有什么建议吗?

最佳答案

您可以应用lambda来为您完成此操作:

data = pd.read_csv('train.txt', delimiter='\t', header=None, names=['category', 'text'], dtype=str).apply(lambda x: x.astype(str).str.lower())


使用示例数据,您将看到:

>>> import pandas as pd
>>> data = pd.read_csv('train.txt', delimiter='\t', header=None, names=['category', 'text'], dtype=str).apply(lambda x: x.astype(str).str.lower())
>>> data.head()
   category                                        text
0     music      today at the recording studio, john...
1  politics  the tensions inside the government have...
2      news            the new pictures of nasa show...

关于python - 处理文本:将CSV文件中的所有字母更改为小写,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43686568/

10-11 23:09
查看更多