我想更改一个数据帧列的数据类型(从datetime64更改为object)。
首先,我创建数据帧:
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
它有两个列:一个是datetime64,另一个是int64 dtype:
>>> df.dtypes
dates datetime64[ns]
values int64
dtype: object
在pandas文档中,我发现了如何将series转换为任何数据类型。看起来我需要的是:
>>> 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
但当我将这个系列指定为dataframe列时,我又得到了一个datetime64数据类型。
>>> df['dates'] = df['dates'].astype(object)
>>> df.dtypes
dates datetime64[ns]
values int64
dtype: object
拜托,帮帮忙。如何将数据帧的列转换为对象dtype?谢谢。
如果您真的想从datetime64[ns]的数据类型更改为对象,您可以运行这样的操作:
df['dates'] = df['dates'].apply(lambda x: str(x))
print df.types # Can verify to see that dates prints out as an object
如果要将Date
列的object
类型转换为datetime64[ns]dtype代码>,则以下代码将起作用:
df['Date']=pd.to_datetime(df['Date'])
这就是你想要的吗?
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