将我的函数数据列添加到 python 中的原始数据中

add my function data column into original data in python

我正在尝试将我的函数值添加到我的数据集列中。我有八列,它们是:

'DATE','Max_R','Total_R','Avg_R','MAX_T','TOTAL_T','AVG_T'

然后我将 DATE 列分为三列,分别为日、月和年。这是我在 python:

中的代码
import pandas as pd
import numpy as np

df=pd.read_csv('moving_average_calculation.csv', sep=',')
#df = pd.DataFrame(columns=['DATE','Max_R','Total_R','Avg_R','MAX_T','TOTAL_T','AVG_T'])
df = pd.DataFrame(pd.date_range('1-Jan-08', periods=2558),columns=['DATE'])


def f(df):
    df = df.copy()
    df['Day'] = pd.DatetimeIndex(df['DATE']).day
    df['Month'] =pd.DatetimeIndex(df['DATE']).month
    df['Year'] = pd.DatetimeIndex(df['DATE']).year
    return df

print(f(df).head(10))

现在我想要获得包含这些列的列:

'Day','Month','Year','Max_R','Total_R','Avg_R','MAX_T','TOTAL_T','AVG_T'

我该怎么做?谢谢。

你的问题有点不清楚,因为你定义了两次 df (评论中的+1),但如果我理解正确的话(也就是说:你已经在 .csv 中有 'DATE'文件)这可能有帮助:

df = pd.read_csv('moving_average_calculation.csv', sep=',')

df['Day'] = pd.DatetimeIndex(df['DATE']).day
df['Month'] = pd.DatetimeIndex(df['DATE']).month
df['Year'] = pd.DatetimeIndex(df['DATE']).year

df.drop('DATE', axis=1, inplace=True)

df = df[['Day','Month','Year','Max_R','Total_R','Avg_R','MAX_T','TOTAL_T','AVG_T']]