Pandas 如何从示例dataframe的“创建时间”列中获取小时数,并将其作为另一个dataframe进行计数

Pandas 如何从示例dataframe的“创建时间”列中获取小时数,并将其作为另一个dataframe进行计数,pandas,pandas-groupby,Pandas,Pandas Groupby,具有以下列的示例数据帧(df): id created_time faid 0 21 2019-06-17 07:06:45 FF1854155 1 54 2019-04-12 08:06:03 FF30232 2 88 2019-04-20 05:36:03 FF1855531251 3 154 2019-04-26 07:0

具有以下列的示例数据帧(df):

     id     created_time     faid                                            
0    21 2019-06-17 07:06:45  FF1854155    
1    54 2019-04-12 08:06:03  FF30232     
2    88 2019-04-20 05:36:03  FF1855531251     
3   154 2019-04-26 07:09:22  FF8145292   
4   218 2019-07-25 13:20:51  FF0143154   
5   219 2019-04-30 18:50:24  FF04211 
6   235 2019-04-30 20:37:37  FF0671380   
7   266 2019-05-02 08:38:56  FF08070   
8   268 2019-05-02 11:08:21  FF591087
我可以知道如何实现新的数据帧吗

hour  count
 07      2
 08      2
  .      .
  .      .  

尝试从创建的时间计算小时数

分组计时

df['hour'] = pd.to_datetime(df['created_time']).dt.hour
res = df.groupby(['hour'],as_index=False)['faid'].count().rename(columns={"faid":"count"})

我试过了,但运气不好……有什么变化吗?df['hour']=pd.to_datetime(df['created_time'].dt.hour res=df.groupby(['hour'],as_index=False)['farmer_id'].count()res.rename(columns={“faid”:“count”},inplace=True)只需记住需要重命名count()中使用的列。如果使用farmer_id重命名farmer_id,如果faid,则重命名faid
df['count']=df.groupby(['hour'],as_index=False)['faid'].transform(pd.Series.count)
trythis@Codenewbie您需要在其他数据帧中保存添加的所需输出
hour  count
 07      2
 08      2