Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将熊猫中的日期列拆分为单独的日、月、年列_Python_Pandas_Datetime_Dataframe_Multiple Columns - Fatal编程技术网

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

Python 如何将熊猫中的日期列拆分为单独的日、月、年列,python,pandas,datetime,dataframe,multiple-columns,Python,Pandas,Datetime,Dataframe,Multiple Columns,我有数据集,df.head(4): 下面是df.columns: Index(['Dewptm', 'Fog', 'Humidity', 'Pressurem', 'Rain', 'Tempm', 'Wspdm', 'Rainfall'], dtype='object') 如何将datetime\u utc列拆分为年、月和日列 我试过: df["day"] = df['datetime_utc'].map(lambda x: x.day) df["month"] =

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

下面是
df.columns

Index(['Dewptm', 'Fog', 'Humidity', 'Pressurem', 'Rain', 'Tempm', 'Wspdm',
       'Rainfall'],
      dtype='object')
如何将
datetime\u 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\u utc'

我得到一个错误:

KeyError:“['datetime\u utc']未在axis中找到” 我面临的问题是,
datetime\u utc
列是我的数据集中的默认索引列,请建议一种方法


问题是
datetime\u 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\u 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)

看起来是索引而不是列
# 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  
df.index = pd.to_datetime(df.index)