有没有更好的方法来索引数据帧?

时间:2016-05-20 20:47:14

标签: python pandas dataframe

我是Python和数据框架的新手。我想知道是否有更好的方法来做这样的事情:

df['Datetime'] = df.index
df.reset_index(inplace=True, drop=True)
df['id'] = df.index
df.index = df['Datetime']
df.drop('Datetime', axis=1, inplace=True)

我需要一个ID /索引列,以便我可以在数学公式中使用它(sin(2*pi*id)sin(2*pi*datetime)效果好。)

是否有更容易(Pythonic)的方式来生成它而不必移动列?

更新

print(df.index)
print(df['id'])

输出:

DatetimeIndex(['2013-01-03', '2013-01-04', '2013-01-07', '2013-01-08',
               '2013-01-09', '2013-01-10', '2013-01-11', '2013-01-14',
               '2013-01-15', '2013-01-16', 
               ...
               '2014-01-20', '2014-01-21', '2014-01-22', '2014-01-23',
               '2014-01-24', '2014-01-27', '2014-01-28', '2014-01-29',
               '2014-01-30', '2014-01-31'],
              dtype='datetime64[ns]', length=282, freq=None, tz='UTC')
2013-01-03 00:00:00+00:00      0
2013-01-04 00:00:00+00:00      1
2013-01-07 00:00:00+00:00      2
2013-01-08 00:00:00+00:00      3
2013-01-09 00:00:00+00:00      4
2013-01-10 00:00:00+00:00      5
2013-01-11 00:00:00+00:00      6
2013-01-14 00:00:00+00:00      7

1 个答案:

答案 0 :(得分:1)

您可以使用pd.DataFrame重新构建所需的DataFrame。

import numpy as np
import pandas as pd

df = pd.DataFrame(index=pd.date_range('2009-1-1', periods=4))
df['Datetime'] = df.index
df.reset_index(inplace=True, drop=True)
df['id'] = df.index
df.index = df['Datetime']
df.drop('Datetime', axis=1, inplace=True)

相当于

df = pd.DataFrame(index=pd.date_range('2009-1-1', periods=4))
df = pd.DataFrame({'id': np.arange(len(df))}, index=df.index)
#             id
# 2009-01-01   0
# 2009-01-02   1
# 2009-01-03   2
# 2009-01-04   3

或者,作为A L points out,您只需使用

即可
df['id'] = np.arange(len(df))