简体   繁体   English

如何将日期列拆分为 pandas 中单独的日、月、年列

[英]How to split a date column into separate day , month ,year column in pandas

I have a dataset df :我有一个数据集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
...

Here is df.columns :这是df.columns

Index(['Dewptm', 'Fog', 'Humidity', 'Pressurem', 'Rain', 'Tempm', 'Wspdm',
       'Rainfall'],
      dtype='object')

How could I split datetime_utc column into the year, month and day column?如何将datetime_utc列拆分为年、月和日列?

I tried:我试过:

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)

Error:错误:

KeyError: 'datetime_utc' KeyError: 'datetime_utc'

Also

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)

I am getting error:我收到错误:

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. KeyError: "['datetime_utc'] not found in axis" 我面临的问题是列datetime_utc是我数据集中的默认索引列,请建议我一种方法。

The problem is that datetime_utc is in your index instead a column, so you have to access your index to be able to make your new columns:问题是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  

If you want datetime_utc as a column you have to reset your index and then you can access the datetime methods with dt.month , dt.year and dt.day like following:如果您希望datetime_utc作为列,您必须重置索引,然后您可以使用dt.monthdt.yeardt.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  

Note if your index is not in datetime type yet, use the following before you try to extract year, month and day:请注意,如果您的索引还不是datetime时间类型,请在尝试提取年、月和日之前使用以下内容:

df.index = pd.to_datetime(df.index)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用python将日期列拆分为日/月/年列 - How to split a date column into day/month/year columns using python 熊猫:如何计算日期栏中的年份月份? - Pandas: how to take the year month of a date column? Pandas 年和日列到月列 - Pandas Year and Day Column to Month column 更改 pandas 列的日期格式(月-日-年到日-月-年) - Change date format of pandas column (month-day-year to day-month-year) 如何将年(2位),月和日的3个单独列合并为单个日期列 - How to combine 3 separate columns of year(2 digit) ,month and day into single date column 如何从 Pandas Dataframe 中的期间代码列(组合年、日期和月份)创建单独的字段? - How to create separate fields from Period code column(combined year, date and month) in Pandas Dataframe? Python-将月,日,年组合成日期列 - Python - Combining Month, Day, Year into Date Column 将带有月份和年份的列添加到 pandas 中的新日期 - Add column with month and year to a new date in pandas 如何为 Pandas 中的 2 天数据从年、日、小时和分钟列(无月列)创建日期时间 object? - How to create a datetime object from Year, Day, Hour and Minute columns (without month column) for 2 day data in Pandas? 使用Python更改“日期中的年份”列,而不更改月份和日期 - Change the Year in Date column without changing the month and day using Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM