gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 16:34:04 26 4
gpt4 key购买 nike

我是 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

最佳答案

您可以使用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))

关于python - 有没有更好的方法来索引数据帧?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37355592/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com