python - 如果 Pandas 数据框字符串列缺少值,如何小写它?

标签 python string pandas missing-data

以下代码不起作用。

import pandas as pd
import numpy as np
df=pd.DataFrame(['ONE','Two', np.nan],columns=['x']) 
xLower = df["x"].map(lambda x: x.lower())

我应该如何调整它以获得 xLower = ['one','two',np.nan] ? 效率很重要,因为真实的数据框很大。

最佳答案

使用 Pandas vectorized string methods ;如文档中所述:

these methods exclude missing/NA values automatically

.str.lower() 是第一个例子;

>>> df['x'].str.lower()
0    one
1    two
2    NaN
Name: x, dtype: object

关于python - 如果 Pandas 数据框字符串列缺少值,如何小写它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22245171/

相关文章:

python - Seaborn Factorplot 的对数网格线

python - 为什么不将 if __name__ == '__main__' 放在模块的开头?

ruby - 在 Ruby 中用 ASCII 字符替换所有非 ASCII 字符的最简单方法是什么?

JavaScript 替换字符串

objective-c - Objective-C 中的字符串解析

python - Pandas:按两个参数分组并按第三个参数排序

Python3 用 Pandas 读取 Html 表

python - 使用Python 3.4和Pillow以及方法转换: AttributeError: Image

Python 使用累加器和任意 lambda 函数减少?

python - 将函数参数打包到字典中——与 **kwargs 相反