How to split a date column into separate day , month ,year column in pandas(如何在 pandas 中将日期列拆分为单独的日、月、年列)
                            本文介绍了如何在 pandas 中将日期列拆分为单独的日、月、年列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
                        
                        问题描述
我有一个数据集df:
               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’]
我面临的问题是列datetime_utc是我的数据集中的默认索引列,请给我建议一种方法。
推荐答案
问题在于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.month、dt.year和dt.day访问DateTime方法,如下所示:
# 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)
                        这篇关于如何在 pandas 中将日期列拆分为单独的日、月、年列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
				 沃梦达教程
				
			本文标题为:如何在 pandas 中将日期列拆分为单独的日、月、年列
				
        
 
            
        基础教程推荐
             猜你喜欢
        
	     - PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01
 - 包装空间模型 2022-01-01
 - PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01
 - 在Python中从Azure BLOB存储中读取文件 2022-01-01
 - Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
 - 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
 - 修改列表中的数据帧不起作用 2022-01-01
 - 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01
 - 求两个直方图的卷积 2022-01-01
 - 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
 
    	
    	
    	
    	
    	
    	
    	
    	
				
				
				
				