Py学习  »  Python

在Python中使用str.lower时如何防止NaN?

mf17 • 3 年前 • 1225 次点击  

我想把一列转换成小写。问题是,在某些情况下,列中的字符串只包含数字。在我的现实生活中,这是由于数据输入不当造成的。我不想将这些值转换为NaN,而是希望保持数字字符串的原样。实现这一目标的最佳方法是什么?

下面是我当前的代码和输出

import pandas as pd

df = pd.DataFrame({'col':['G5051', 'G5052', 5053, 'G5054']})

df['col'].str.lower()

电流输出

enter image description here

期望输出

enter image description here

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/132221
 
1225 次点击  
文章 [ 3 ]  |  最新文章 3 年前