我的DataFrame的索引是日期时间,每10分钟一行。我想根据索引的小时值设置列的值。到目前为止,这是我的代码:
import pandas as pd
df = pd.read_csv('Data.csv', sep='\t');
df['DateTime'] = pd.to_datetime(df['DateTime'])
df.set_index('DateTime', inplace=True)
rng = df.index
day_index = rng[(rng.hour >= 8) & (rng.hour < 22)]
night_index = rng[(rng.hour > 8 ) & (rng.hour <= 22)]
到目前为止一切顺利:以下给出了DF的预期部分:
df.loc[day_index].head()
LAeq
DateTime
2018-01-16 08:00:00 50.0
2018-01-16 08:10:00 49.4
2018-01-16 08:20:00 49.2
2018-01-16 08:30:00 48.9
2018-01-16 08:40:00 48.1
和
df.loc[night_index].head()
LAeq
DateTime
2018-01-15 23:10:00 42.8
2018-01-15 23:20:00 41.9
2018-01-15 23:30:00 43.5
2018-01-15 23:40:00 45.6
2018-01-15 23:50:00 45.7
我不应该能够设置这些日夜时段的列值,例如:
df.loc[day_index, 'Limit'] = 55
df.loc[night_index, 'Limit'] = 45
两者都给AttributeError: 'bool' object has no attribute 'any'
设置这些的最简单方法是什么?我正在使用可怕的非Pythonic(警告这段代码可能会伤到你的眼睛):
df['Hour'] = df.index.hour
for i in range(len(df.index)):
h = int(df.ix[i]['Hour'])
if (8 <= h < 22):
df.ix[i, 'Limit'] = 55
else:
df.ix[i,'Limit'] = 45
哪个很丑,但确实有效。
答案 0 :(得分:0)
您需要删除过滤并返回布尔掩码:
day_mask = (rng.hour >= 8) & (rng.hour < 22)
night_mask = (rng.hour > 8 ) & (rng.hour <= 22)
df[day_mask].head()
df[night_mask ].head()
#same as
#df.loc[night_mask ].head()
df.loc[day_mask, 'Limit'] = 55
df.loc[night_mask , 'Limit'] = 45
或更好:
df['limit'] = np.where(day_mask, 55, 45)