python - 处理文本 : change all letters to lowercase in a CSV file

标签 python csv

我正在使用 .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/

相关文章:

python - 根据单独数组中的标签对 numpy 数组值求和

python - 使用外部命令多重处理数千个文件

python - 从 (xarray) dask.array 到 numpy 数组的转换非常慢

python - 如何在 Groovy 中设置对象的属性

sql - Postgres PL/pgSQL 函数结果到文件,文件名作为参数

java - 以特定模式读取 csv 文件并存储在 map 或二维数组中

python - 在 wtforms 中调用验证时键入错误

Python 循环协助

php - 无法使用 PHP 将 CSV 导入 MySQL

mysql - 我怎么能忽略分号;在 & 当我从 .csv 文件创建 Hive 表时