gpt4 book ai didi

python - 使用其索引对 Pandas Dataframe 列进行操作

转载 作者:太空宇宙 更新时间:2023-11-04 02:53:41 27 4
gpt4 key购买 nike

这应该比较容易。我有一个 Pandas 数据框(日期):

    A   B   C
1/8/2017 1/11/2017 1/20/2017 1/25/2017
1/9/2017 1/11/2017 1/20/2017 1/25/2017
1/10/2017 1/11/2017 1/20/2017 1/25/2017
1/11/2017 1/20/2017 1/25/2017 1/31/2017
1/12/2017 1/20/2017 1/25/2017 1/31/2017
1/13/2017 1/20/2017 1/25/2017 1/31/2017

我想了解 Dates.index 和 Dates 之间的区别。输出将是这样的:

    A   B   C
1/8/2017 3 12 17
1/9/2017 2 11 16
1/10/2017 1 10 15
1/11/2017 9 14 20
1/12/2017 8 13 19
1/13/2017 7 12 18

当然,我试过这个:

Dates - Dates.index

但是我收到了这个可爱的 TypeError:

TypeError: Could not operate DatetimeIndex...with block values ufunc subtract cannot use operands with types dtype('<M8[ns]') and dtype('O')

相反,我编写了一个逐列循环的循环,但这看起来很愚蠢。任何人都可以建议一个 pythonic 方法来做到这一点吗?

编辑

In [1]: import pandas as pd
import numpy as np
import datetime
dates = pd.date_range('20170108',periods=6)
df = pd.DataFrame(np.empty([len(dates),3]),index=dates,columns=list('ABC'))
df['A'].loc[0:3] = datetime.date(2017, 1, 11)
df['B'].loc[0:3] = datetime.date(2017, 1, 20)
df['C'].loc[0:3] = datetime.date(2017, 1, 25)
df['A'].loc[3:6] = datetime.date(2017, 1, 20)
df['B'].loc[3:6] = datetime.date(2017, 1, 25)
df['C'].loc[3:6] = datetime.date(2017, 1, 31)

In [2]: print(df)
A B C
2017-01-08 2017-01-11 2017-01-20 2017-01-25
2017-01-09 2017-01-11 2017-01-20 2017-01-25
2017-01-10 2017-01-11 2017-01-20 2017-01-25
2017-01-11 2017-01-20 2017-01-25 2017-01-31
2017-01-12 2017-01-20 2017-01-25 2017-01-31
2017-01-13 2017-01-20 2017-01-25 2017-01-31

In [3]: df = df.sub(df.index.to_series(),axis=0)

ValueError: operands could not be broadcast together with shapes (18,) (6,)

最佳答案

您需要先转换所有列 to_datetime然后使用 sub :

#if dtypes of all columns are datetime, omit it
date_cols = list('ABC')
for col in df.columns:
df[col] = pd.to_datetime(df[col])

df = df.sub(df.index.to_series(),axis=0)
print (df)
A B C
2017-01-08 3 days 12 days 17 days
2017-01-09 2 days 11 days 16 days
2017-01-10 1 days 10 days 15 days
2017-01-11 9 days 14 days 20 days
2017-01-12 8 days 13 days 19 days
2017-01-13 7 days 12 days 18 days

你需要dtypes datetime64:

dates = pd.date_range('20170108',periods=6)
df = pd.DataFrame(index=dates)
df.loc[0:3, 'A'] = pd.Timestamp(2017, 1, 11)
df.loc[0:3, 'B'] = pd.Timestamp(2017, 1, 20)
df.loc[0:3, 'C'] = pd.Timestamp(2017, 1, 25)
df.loc[3:6, 'A'] = pd.Timestamp(2017, 1, 20)
df.loc[3:6, 'B'] = pd.Timestamp(2017, 1, 25)
df.loc[3:6, 'C'] = pd.Timestamp(2017, 1, 31)
print (df)
A B C
2017-01-08 2017-01-11 2017-01-20 2017-01-25
2017-01-09 2017-01-11 2017-01-20 2017-01-25
2017-01-10 2017-01-11 2017-01-20 2017-01-25
2017-01-11 2017-01-20 2017-01-25 2017-01-31
2017-01-12 2017-01-20 2017-01-25 2017-01-31
2017-01-13 2017-01-20 2017-01-25 2017-01-31

print (df.dtypes)
A datetime64[ns]
B datetime64[ns]
C datetime64[ns]
dtype: object

df = df.sub(df.index.to_series(),axis=0)
print (df)
A B C
2017-01-08 3 days 12 days 17 days
2017-01-09 2 days 11 days 16 days
2017-01-10 1 days 10 days 15 days
2017-01-11 9 days 14 days 20 days
2017-01-12 8 days 13 days 19 days
2017-01-13 7 days 12 days 18 days

关于python - 使用其索引对 Pandas Dataframe 列进行操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43096821/

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