Python在双索引

时间:2017-09-14 12:42:56

标签: python pandas dataframe dayofweek dummy-variable

我有一个带有双索引(日,时间)的数据框,如果索引日在正确的日期,我想创建新的列'星期一','星期二','星期三'等等于1。

我的原始数据框:

                       Visitor  
Date       Time                                                              
2017-09-11 4:45           0         
           5:00           1        
           5:15          26       
....
2017-09-12 4:45           0       
           5:00           1         
           5:15          26     
....

我想拥有什么:

                       Visitor      Monday    Tuesday
Date       Time                                                              
2017-09-11 4:45           0           1          0
           5:00           1           1          0
           5:15          26           1          0
....
2017-09-12 4:45           0           0          1
           5:00           1           0          1
           5:15          26           0          1
....

以下是我的尝试:

df['Monday'] = (df.index.get_level_values(0).weekday() == 0)

但是我收到一条错误,说“'Int64Index'对象不可调用”。

提前致谢!

2 个答案:

答案 0 :(得分:3)

您需要从

中删除()
df['Monday'] = (df.index.get_level_values(0).weekday == 0).astype(int)

print (df)
                 Visitor  Monday
Date       Time                 
2017-09-11 4:45        0       1
           5:00        1       1
           5:15       26       1
2017-09-12 4:45        0       0
           5:00        1       0
           5:15       26       0
names = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']

for i, x in enumerate(names):
    df[x] = (df.index.get_level_values(0).weekday == i).astype(int)
print (df)
                 Visitor  Monday  Tuesday  Wednesday  Thursday  Friday  \
Date       Time                                                          
2017-09-11 4:45        0       1        0          0         0       0   
           5:00        1       1        0          0         0       0   
           5:15       26       1        0          0         0       0   
2017-09-12 4:45        0       0        1          0         0       0   
           5:00        1       0        1          0         0       0   
           5:15       26       0        1          0         0       0   

                 Saturday  Sunday  
Date       Time                    
2017-09-11 4:45         0       0  
           5:00         0       0  
           5:15         0       0  
2017-09-12 4:45         0       0  
           5:00         0       0  
           5:15         0       0  

另一个解决方案是改进了另一个原始answer - 需要DatetimeIndex.weekday_name get_dummies,然后set_index原始索引,如有必要,请添加reindex以添加missig名称:

names = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']

df1 = df.join(pd.get_dummies(df.index.get_level_values(0).weekday_name)
                .set_index(df.index).reindex(columns=names, fill_value=0))
print (df1)
                 Visitor  Monday  Tuesday  Wednesday  Thursday  Friday  \
Date       Time                                                          
2017-09-11 4:45        0       1        0          0         0       0   
           5:00        1       1        0          0         0       0   
           5:15       26       1        0          0         0       0   
2017-09-12 4:45        0       0        1          0         0       0   
           5:00        1       0        1          0         0       0   
           5:15       26       0        1          0         0       0   

                 Saturday  Sunday  
Date       Time                    
2017-09-11 4:45         0       0  
           5:00         0       0  
           5:15         0       0  
2017-09-12 4:45         0       0  
           5:00         0       0  
           5:15         0       0  

答案 1 :(得分:1)

get_dummiesweekday_name

一起使用
In [293]: df.join(pd.get_dummies(df.index.get_level_values(0).weekday_name)
                    .set_index(df.index))
Out[293]:
                 Visitor  Monday  Tuesday
Date       Time
2017-09-11 4:45        0       1        0
           5:00        1       1        0
           5:15       26       1        0
2017-09-12 4:45        0       0        1
           5:00        1       0        1
           5:15       26       0        1

In [272]: pd.get_dummies(df.index.get_level_values(0).weekday_name)
Out[272]:
   Monday  Tuesday
0       1        0
1       1        0
2       1        0
3       0        1
4       0        1
5       0        1

详细

In [286]: df.index.get_level_values(0).weekday_name
Out[286]: Index([u'Monday', u'Monday', u'Monday', u'Tuesday', u'Tuesday', u'Tuesday'], dtype='object', name=u'Date')