[英]Pandas read_csv dtype=object column contains numbers
I have a DataFrame column with alphanumeric IDs - some numbers, some letters, some both. 我有一个带有字母数字ID的DataFrame列-一些数字,一些字母,以及两者兼有。 I am using read_csv to read the data and want to read all the values of this column as strings. 我正在使用read_csv读取数据,并想以字符串形式读取此列的所有值。 I can't change the values in the underlying data. 我无法更改基础数据中的值。
I have tried to set the dtype for the column as an object 我试图将列的dtype设置为对象
df = pd.read_csv(filename, dtype = {col: object})
I have also tried to use a converter to change all the values in the columns to strings. 我也尝试过使用转换器将列中的所有值更改为字符串。
df = pd.read_csv(filename, converters = {i: str for i in col})
However, I still end up with some non-string numbers (12345) and some string numbers ('12345') which mess up my aggregations. 但是,我最终仍然得到一些非字符串数字(12345)和一些字符串数字('12345'),这些数字使我的汇总混乱。
Any suggestions? 有什么建议么? Thanks! 谢谢!
您也可以尝试:
df['column'] = df['column'].apply(lambda x: str(x))
Use: 采用:
df = pd.read_csv(filename, dtype = {i: str for i in col})
The only difference from this and the first one is I do dtype
not converter
, it's basically a merge of the two. 与此唯一的区别是,我不是dtype
converter
,基本上是两者的合并。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.