如何通过引用更新数据帧?

2024-04-23 15:48:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在处理一个CSV文件,它被加载到一个数据帧(python/pandas)中。原始数据帧是df_origen,每一行都有一个称为value的度量值,该度量值以1小时为间隔。你知道吗

我需要在一个新的数据帧中将每一行“调整大小”为4行,时间间隔为15分钟。有可能在数据df_origen中有跳转。你知道吗

我做到了。你知道吗

df_destiny = pd.DataFrame(pd.date_range(start, periods=96 * diff.days, freq='15Min'), columns=['from_time'])

之后,我迭代数据集df_origen,将值放入字段“value”中,但我按行过滤数据集df_destiny

tmp = df_destiny[df_destiny['date'] == row['date'] & df_destiny['to_time'] < row['to_time'] + datetime.timedelta(hours=1)

当我将行修改为变量tmp时,我注意到我正在更新为一个副本。你知道吗

有什么方法可以通过参考来做到这一点吗?你知道吗

我的意思是,我能修改数据集tmp并看到数据集df的变化吗?你知道吗

enter image description here


Tags: 文件csvto数据dfdate间隔time
1条回答
网友
1楼 · 发布于 2024-04-23 15:48:31

我通过添加index作为新列来解决这个问题

df_destiny['id'] = df_destiny .index

所以当我筛选行时:

test = df_destiny[df_destiny['date'] == row['date'] & df_destiny['to_time'] < row['to_time'] + datetime.timedelta(hours=1)]

我已经将数据帧test的原始索引放入了数据帧df_destiny。然后我可以更新

df_destiny.loc[test.iloc[0]['id'], 'value'] = int(value/4)

我不知道还有没有别的办法。。。你知道吗

相关问题 更多 >