我正在使用 .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...
我想要做的是将文本中的所有字母更改为小写。因此,例如,“The new pictures of NASA show...”将变为“the new pictures of nasa show...”,但“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 - 处理文本 : change all letters to lowercase in a CSV file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43686568/