python - 如何在 Pandas 中将日期列拆分为单独的日、月、年列

标签 python pandas datetime dataframe multiple-columns

我有数据集 df.head(4) :

               Dewptm   Fog  Humidity   Pressurem     Tempm      Wspdm  Rainfall
datetime_utc                            
1996-11-01    11.666667 0.0  52.916667  -2659.666667  22.333333  2.466667   0
1996-11-02    10.458333 0.0  48.625000  1009.833333   22.916667  8.028571   0
1996-11-03    12.041667 0.0  55.958333  1010.500000   21.791667  4.804545   0
1996-11-04    10.222222 0.0  48.055556  1011.333333   22.722222  1.964706   0

这是 df.columns :
Index(['Dewptm', 'Fog', 'Humidity', 'Pressurem', 'Rain', 'Tempm', 'Wspdm',
       'Rainfall'],
      dtype='object')

如何将 datetime_utc 列拆分为年、月和日列?

我试过了:
df["day"] = df['datetime_utc'].map(lambda x: x.day)
df["month"] = df['datetime_utc'].map(lambda x: x.month)
df["year"] = df['datetime_utc'].map(lambda x: x.year)

错误:

KeyError: 'datetime_utc'




pd.concat([df.drop('datetime_utc', axis = 1), 
          (df.datetime_utc.str.split("-).str[:3].apply(pd.Series)
          .rename(columns={0:'year', 1:'month', 2:'day'}))], axis = 1)

我收到错误:

KeyError: "['datetime_utc'] not found in axis" The problem I am facing is the column datetime_utc is the default index column in my dataset, Please suggest me an approach.

最佳答案

问题是 datetime_utc 在您的索引中而不是列中,因此您必须访问索引才能创建新列:

df['day'] = df.index.day
df['month'] = df.index.month
df['year'] = df.index.year

print(df)
                 Dewptm  Fog   Humidity    Pressurem      Tempm     Wspdm  \
datetime_utc                                                                
1996-11-01    11.666667  0.0  52.916667 -2659.666667  22.333333  2.466667   
1996-11-02    10.458333  0.0  48.625000  1009.833333  22.916667  8.028571   
1996-11-03    12.041667  0.0  55.958333  1010.500000  21.791667  4.804545   
1996-11-04    10.222222  0.0  48.055556  1011.333333  22.722222  1.964706   

              Rainfall  day  month  year  
datetime_utc                              
1996-11-01           0    1     11  1996  
1996-11-02           0    2     11  1996  
1996-11-03           0    3     11  1996  
1996-11-04           0    4     11  1996  
如果您想要 datetime_utc 作为列,您必须重置索引,然后您可以使用 dt.monthdt.yeardt.day 访问日期时间方法,如下所示:
# Reset our index so datetime_utc becomes a column
df.reset_index(inplace=True)

# Create new columns
df['day'] = df['datetime_utc'].dt.day
df['month'] = df['datetime_utc'].dt.month
df['year'] = df['datetime_utc'].dt.year

print(df)
  datetime_utc     Dewptm  Fog   Humidity    Pressurem      Tempm     Wspdm  \
0   1996-11-01  11.666667  0.0  52.916667 -2659.666667  22.333333  2.466667   
1   1996-11-02  10.458333  0.0  48.625000  1009.833333  22.916667  8.028571   
2   1996-11-03  12.041667  0.0  55.958333  1010.500000  21.791667  4.804545   
3   1996-11-04  10.222222  0.0  48.055556  1011.333333  22.722222  1.964706   

   Rainfall  day  month  year  
0         0    1     11  1996  
1         0    2     11  1996  
2         0    3     11  1996  
3         0    4     11  1996  
注意 如果您的索引不在 datetime 类型中,请在尝试提取年月日之前使用以下内容:
df.index = pd.to_datetime(df.index)

关于python - 如何在 Pandas 中将日期列拆分为单独的日、月、年列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55776571/

相关文章:

python - 循环遍历 pandas DataFrame 时出现意外结果

python - 如何保持时间戳的毫秒分量,即使它的值为零?

python - 将 pandas.Series 中的时间戳转换为 datetime.datetime

c++ - ctime 和 time_t 的数学运算

javascript - 我没有在 javascript 0000-00-00 00 :00:00 中获得准确的 mysql 默认时间格式

python - 当发送到 Pyramid/Cornice 应用程序时,Backbone.js HTTP PUT 请求失败并出现 404 错误

python - pandas 使用 groupby 填充

python - 如何执行两个列表的逐元素乘法?

python - 如何从 Raw_Input 字符串值创建循环

python - 为什么我会收到此内存错误?