pandas dtype 从对象到字符串的转换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21841402/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 21:42:27  来源:igfitidea点击:

pandas dtype conversion from object to string

pythonpandas

提问by uday

I have a csv file that has a few columns which are numbers and few that are string. When I try myDF.dtypesit shows me all the string columns as object.

我有一个 csv 文件,其中有几列是数字,几列是字符串。当我尝试时,myDF.dtypes它将所有字符串列显示为object.

  1. Someone asked a related question before hereabout why this is done. Is it possible to recast the dtypefrom object to string?

  2. Also, in general, is there any easy way to recast the dtypefrom int64and float64to int32and float32and save on the size of the data (in memory / on disk)?

  1. 之前有人在这里问过一个相关的问题,关于为什么要这样做。是否可以将dtypefrom 对象重新转换为字符串?

  2. 另外,在一般情况下,没有任何简单的方法来重铸dtypeint64float64int32float32和保存(/在存储器中在磁盘上)上的数据的大小?

采纳答案by Jeff

All strings are represented as variable-length (which is what objectdtype is holding). You can do series.astype('S32')if you want; but it will be recast if you then store it in a DataFrame or do much with it. This is for simplicity.

所有字符串都表示为可变长度(这是objectdtype 所持有的)。series.astype('S32')如果你愿意,你可以这样做;但是如果您将其存储在 DataFrame 中或使用它做很多事情,它将被重铸。这是为了简单起见。

Certain serialization formats, e.g. HDFStorestores the strings as fixed-length strings on disk though.

某些序列化格式,例如,HDFStore将字符串作为固定长度的字符串存储在磁盘上。

You can series.astype(int32)if you would like and it will store as the new type.

series.astype(int32)如果你愿意,你可以,它会存储为新类型。

回答by Anshul Bisht

df = your dataframe object with values
print('dtype in object form :')
print(df.dtypes[df.columns[0]])    // output: dtype('O')
print('\ndtype in string')
print(str(df.dtypes[df.columns[0]]))    // output: 'object'