Python 如何更改DataFrame中一列的dtype?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19448153/
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-08-19 13:46:05  来源:igfitidea点击:

How to change dtype of one column in DataFrame?

pythonpandasdataframe

提问by ghostishev

I want to change dtype of one data frame column (from datetime64 to object).

我想更改一个数据框列的 dtype(从 datetime64 到 object)。

First of all, I create data frame:

首先,我创建数据框:

Python 2.6.8 (unknown, Jan 26 2013, 14:35:25) 
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pandas as pd
>>> values = pd.Series(i for i in range(5))
>>> dates = pd.date_range('20130101',periods=5)
>>> df = pd.DataFrame({'values': values, 'dates': dates})
>>> df
/usr/local/lib/python2.6/dist-packages/pandas/core/config.py:570: DeprecationWarning: height has been deprecated.

  warnings.warn(d.msg, DeprecationWarning)
                dates  values
0 2013-01-01 00:00:00       0
1 2013-01-02 00:00:00       1
2 2013-01-03 00:00:00       2
3 2013-01-04 00:00:00       3
4 2013-01-05 00:00:00       4

It have two columns: one is datetime64 and other one is int64 dtype:

它有两列:一列是 datetime64,另一列是 int64 dtype:

>>> df.dtypes
dates     datetime64[ns]
values             int64
dtype: object

In pandas documentation I found how to convert series to any dtypes. It looks like what I need:

在 Pandas 文档中,我找到了如何将系列转换为任何 dtypes。它看起来像我需要的:

>>> df['dates'].astype(object)
0    2013-01-01 00:00:00
1    2013-01-02 00:00:00
2    2013-01-03 00:00:00
3    2013-01-04 00:00:00
4    2013-01-05 00:00:00
Name: dates, dtype: object

But when I assign this series as dataframe column, I got a datetime64 dtype again.

但是当我将这个系列指定为数据框列时,我又得到了一个 datetime64 dtype。

>>> df['dates'] = df['dates'].astype(object)
>>> df.dtypes
dates     datetime64[ns]
values             int64
dtype: object

Please, help. How to convert data frame's column to object dtype? Thanks.

请帮忙。如何将数据框的列转换为对象数据类型?谢谢。

回答by Jeff

Is this what you are after?

这是你追求的吗?

In [9]: pd.pivot_table(data=df,rows='columns',cols='rows',values='values',margins=True).T
Out[9]: 
columns  2013-01-01 00:00:00  2013-01-02 00:00:00  2013-01-03 00:00:00  2013-01-04 00:00:00  2013-01-05 00:00:00       All
rows                                                                                                                      
a                          0                  NaN                    2                    3                  NaN  1.666667
b                        NaN                    1                  NaN                  NaN                    4  2.500000
All                        0                    1                    2                    3                    4  2.000000

回答by Will

If you really want to change from datatype of datetime64[ns] to object, you could run something like this:

如果您真的想将 datetime64[ns] 的数据类型更改为 object,您可以运行如下代码:

df['dates'] = df['dates'].apply(lambda x: str(x))
print df.types # Can verify to see that dates prints out as an object

回答by Shihe Zhang

Not proficient with lambda usage. In some simple case, df['dates'].astype(str)would work also.

不精通 lambda 用法。在一些简单的情况下,df['dates'].astype(str)也可以工作。

Note:it doesn't work when there are NaN in a column.

注意:当列中有 NaN 时它不起作用。

Not solution to OP, but others may find help in this question. Almost duplicate, but it's mostly talk about convert to numeric.

不是 OP 的解决方案,但其他人可能会在此问题中找到帮助。几乎重复,但主要是谈论转换为数字。

回答by Darshan Chheda

If you want to convert Datecolumn which is objecttype to datetime64[ns] dtype;, then following code will work:

如果要将类型为 的Date列转换objectdatetime64[ns] dtype;,则以下代码将起作用:

df['Date']=pd.to_datetime(df['Date'])