日期后更改2列长数据帧内容的Python方法
设备“MOR4”的“顶部”和“底部”温度/湿度传感器在8月10日和11日之间错位 在“长格式”数据集中,什么是最符合python规范的纠正方法 数据结构: 数据样本日期后更改2列长数据帧内容的Python方法,python,pandas,Python,Pandas,设备“MOR4”的“顶部”和“底部”温度/湿度传感器在8月10日和11日之间错位 在“长格式”数据集中,什么是最符合python规范的纠正方法 数据结构: 数据样本 data.head() bottom_temperature bottom_humidity top_temperature top_humidity external_temperature published_at external_humidity short_id weight 0
data.head()
bottom_temperature bottom_humidity top_temperature top_humidity external_temperature published_at external_humidity short_id weight
0 34.48 44.81 33.56 47.62 17.88 2017-10-07 23:11:27 17.88 MOR1 NaN
1 34.89 42.89 33.89 43.86 18.06 2017-10-09 03:16:05 18.06 MOR5 NaN
2 34.87 41.90 33.81 42.88 18.19 2017-10-09 03:31:41 18.19 MOR5 NaN
3 34.79 43.05 33.93 44.68 18.00 2017-10-09 03:00:37 18.00 MOR20 NaN
4 34.92 42.53 34.04 44.68 18.19 2017-10-09 03:47:11 18.19 MOR6 NaN
df.dtypes
bottom_temperature float64
bottom_humidity float64
top_temperature float64
top_humidity float64
external_temperature float64
published_at datetime64[ns]
external_humidity float64
short_id object
weight float64
dtype: object
用垂直线标识开关的图形:
# MOR4 - bottom and top sensors switched on aug 10 and switched back on 11th
fig, axarr = plt.subplots()
fig.autofmt_xdate()
plt.plot(df.loc[df['short_id'] == 'MOR4']['published_at'], df.loc[df['short_id'] == 'MOR4']['bottom_temperature'], label = "Bottom Temperature C")
plt.plot(df.loc[df['short_id'] == 'MOR4']['published_at'], df.loc[df['short_id'] == 'MOR4']['top_temperature'], label = "Top Temperature")
plt.plot(df.loc[df['short_id'] == 'MOR4']['published_at'], df.loc[df['short_id'] == 'MOR4']['bottom_humidity'], label = "Bottom Humidity %")
plt.plot(df.loc[df['short_id'] == 'MOR4']['published_at'], df.loc[df['short_id'] == 'MOR4']['top_humidity'], label = "Top Humidity %")
plt.plot(df.loc[df['short_id'] == 'MOR4']['published_at'], df.loc[df['short_id'] == 'MOR4']['weight'], label = "Weight kg")
#add vertical line
plt.axvline(datetime.datetime(2017, 8, 10, 13, 10))
#add vertical line
plt.axvline(datetime.datetime(2017, 8, 11, 14, 10))
#specify date
axarr.set_xlim([datetime.date(2017, 8, 10), datetime.date(2017, 8, 12)])
#add title, legend
#plt.title('MOR1, Noticed on Aug 23')
axarr.legend(loc ='best',prop={'size': 6})
plt.show()
问题:
在数据框中,如何在指定日期之间切换“底部湿度”、“底部温度”和“顶部湿度”、“底部湿度”的值(第一个日期:2017-8-10,13:10。第二个日期:2017-8-11,14:10
换言之:
在两条垂直线之间,绿色线实际上是深蓝色线,反之亦然,浅蓝色线和红色线也是如此,并且希望在两个确定的日期之间的数据框中更改这两条线。这里有两种方法
df = pd.DataFrame({'top': [5,6,3,4,5, 2,2,1,3,1, 7,6,5],
'bottom':[2,2,1,3,1, 5,6,3,4,5, 1,2,1],
'other': [1,2,3,4,5,6,7,8,9,10,11,12,13]})
1) 如果top总是大于。。。然后使用最大/最小值:
df['new_top'] = df[['top', 'bottom']].max(axis=1)
df['new_bottom'] = df[['top', 'bottom']].min(axis=1)
2) (非常脏)手动识别点并构建列:
df['new_top2'] = pd.concat([ df.iloc[:4]['top'], df.iloc[4:10]['bottom'], df.iloc[10:]['top'] ])
df['new_bottom2'] = pd.concat([ df.iloc[:4]['bottom'], df.iloc[4:10]['top'], df.iloc[10:]['bottom'] ])
基于您提供的有限信息,以及您没有提供任何您尝试过的东西,很难给您一个好的答案…您可以使用布尔掩码获取相关行:
m = (df['published_at'] >= '2017-8-10 13:10') & (df['published_at'] <= '2017-8-11 14:10') & (df['short_id'] == 'MOR4')
如果先将时间戳设置为索引,则会使事情变得更简单:
data = data.set_index('published_at')
然后您可以像这样更改有问题的段:
data.loc['2017-8-10 13:10':'2017-8-11 14:10','bottom_humidity'] = \
data.loc['2017-8-10 13:10':'2017-8-11 14:10','top_humidity'].values
data.loc[snafu,['bottom_temperature','top_temperature'] = \
data.loc[snafu,['top_temperature','bottom_temperature']].values
如果愿意,可以为此定义时间片并多次使用:
snafu = slice('2017-8-10 13:10','2017-8-11 14:10')
data.loc[snafu,'bottom_humidity'] = data.top_humidity
data.loc[snafu,'bottom_temperature'] = data.top_temperature
或者像这样交换值:
data.loc['2017-8-10 13:10':'2017-8-11 14:10','bottom_humidity'] = \
data.loc['2017-8-10 13:10':'2017-8-11 14:10','top_humidity'].values
data.loc[snafu,['bottom_temperature','top_temperature'] = \
data.loc[snafu,['top_temperature','bottom_temperature']].values
谢谢你指出这一点,我编辑了这篇文章。你试过什么吗?在这里,仅仅是转储数据和需求是非常不受欢迎的。它似乎仍然不清楚。传感器是“两次”什么?为什么要交换这些值?从图表中不清楚为什么任何东西都需要与其他东西交换;例如,“底部湿度”似乎在两条垂直线上都有一致的值。很抱歉,不清楚,这一天传感器放错了位置,所以“顶部传感器”被放在“底部”位置。这适用于整个数据集,而不仅仅是df['short_id']=='MOR5'Hmm的子集,得到以下错误:TypeError:无法比较['2017-8-10 13:10']带有块值。我想这可能是因为没有确切的日期。谢谢你的帮助。我刚刚在中添加了数据类型和绘图代码OP@Evan哎哟,设置数据时没有匹配的索引…添加
。值应该可以工作(请参阅更新的答案)。第一个想法就是这样“error:ValueError:cannot reindex from a duplicate axis”正在尝试时间片可能原始索引有重复项。Try:data.index=data.published\u在