Python 替换时间序列中的列值
我想在预设的timeseries数据帧中替换序列中的一组值,例如,序列a中的3个值将替换3个时间戳值。但是,replace或.set\u value命令似乎不起作用。在这种情况下,时间戳已经以索引形式存在。时间戳示例系列如下所示:Python 替换时间序列中的列值,python,replace,timestamp,Python,Replace,Timestamp,我想在预设的timeseries数据帧中替换序列中的一组值,例如,序列a中的3个值将替换3个时间戳值。但是,replace或.set\u value命令似乎不起作用。在这种情况下,时间戳已经以索引形式存在。时间戳示例系列如下所示: > timestamp Coloum_1 > 01/01/2010 00:00 21.17 > 01/01/2010 00:15 19.67 > 01/01/2010 00:30 17.95 在python中有点新。感谢
> timestamp Coloum_1
> 01/01/2010 00:00 21.17
> 01/01/2010 00:15 19.67
> 01/01/2010 00:30 17.95
在python中有点新。感谢您的支持。谢谢
更新
输出可能看起来像底部的输出。这里,时间戳和Column1数据位于一个单独的excel文件中,Python在其中读取并提取以进行分析。时间戳已经以索引形式存在,只有column1值需要替换为单独的序列值。但是,它似乎并没有用时间戳序列替换序列值
In [42]: df4 = pd.DataFrame({'Timestamp': ['2010-01-01 00:00:00', '2010-01-01 00:15:00', '2010-01-01 00:30:00'], 'Column_1':[21.17, 19.17, 17.95]})
df4 = df4.set_index('Timestamp')
df4.head()
Out[42]:
Timestamp Column_1
2010-01-01 00:00:00 21.17
2010-01-01 00:15:00 19.17
2010-01-01 00:30:00 17.95
In [43]:a = pd.Series([50.0,60.0,70.0])
df4.replace("'2010-01-01 00:00:00', periods=3, freq='15 min'", 'Column_1',a.all)
df4.head()
a = pd.Series([50.0,60.0,70.0])
df4.replace("'2010-01-01 00:00:00', periods=3, freq='15 min'", 'Column_1',a.all)
df4.head()
Out[43]:
Timestamp Column_1
2010-01-01 00:00:00 21.17
2010-01-01 00:15:00 19.17
2010-01-01 00:30:00 17.95
但是如果我只使用一个时间戳,它将替换相应的值
In [50]:
df4.set_value('2010-01-01 00:00:00', 'Column_1', 50);
df4.head()
Out[50]:
Timestamp Column_1
2010-01-01 00:00:00 50.00
2010-01-01 00:15:00 19.17
2010-01-01 00:30:00 17.95
试试这个:
a = pd.Series([15.0,16.0,17.0])
#0 15.0
#1 16.0
#2 17.0
# dtype: float64
a = pd.date_range('1/1/2010', periods=3, freq='900S')
#DatetimeIndex(['2010-01-01 00:00:00', '2010-01-01 00:15:00',
# '2010-01-01 00:30:00'],
# dtype='datetime64[ns]', freq='900S')
print a
# DatetimeIndex(['2010-01-01 00:00:00', '2010-01-01 00:15:00',
# '2010-01-01 00:30:00'],
# dtype='datetime64[ns]', freq='900S')
df = pd.DataFrame(a, columns=['Column1'] )
# or df = pd.DataFrame(pd.date_range('1/1/2010', periods=3, freq='900S'), columns=['Column1'] )
# Column1
# 0 2010-01-01 00:00:00
# 1 2010-01-01 00:15:00
# 2 2010-01-01 00:30:00
s = pd.Series(range(3), name='Column1')
#0 0
#1 1
#2 2
#Name: Column1, dtype: int64
编辑:
已解决
In [68]:
df = pd.DataFrame({'Timestamp': ['2010-01-01 00:00:00', '2010-01-01 00:15:00', '2010-01-01 00:30:00', '2010-01-01 00:45:00'],
'Column_1':[21.17, 19.17, 17.95, 25.0]})
df = df.set_index('Timestamp')
df.head()
Out[68]:
Timestamp Column_1
2010-01-01 00:00:00 21.17
2010-01-01 00:15:00 19.17
2010-01-01 00:30:00 17.95
2010-01-01 00:45:00 25.00
4 rows × 1 columns
In [69]:
df.loc ['2010-01-01 00:00:00':'2010-01-01 00:15:00', 'Column_1'] = [50,60]
df.head()
Out[69]:
Timestamp Column_1
2010-01-01 00:00:00 50.00
2010-01-01 00:15:00 60.00
2010-01-01 00:30:00 17.95
2010-01-01 00:45:00 25.00
嗨,梅林,谢谢。只需添加一个单独的部分来更详细地解释问题和输出。你能看看这个吗你想得太多了。。请参见编辑。你不需要创建熊猫对象来处理熊猫。@Merlin:哈哈……我有超过20000个信号,与时间戳的间隔为15分钟。我需要将一些定义的值对应替换为某个时间戳,我认为覆盖这些值的最简单方法是与时间戳匹配。@Merlin:Solved。谢谢…对这个问题想得太多了………>df.loc['timestamp start':timestamp stop','Column']=[X,Y]如果我下面的答案有帮助,也许你应该投上一票,或者标记为正确。
df4['Column_1'] = [50.0,60.0,70.0]
In [68]:
df = pd.DataFrame({'Timestamp': ['2010-01-01 00:00:00', '2010-01-01 00:15:00', '2010-01-01 00:30:00', '2010-01-01 00:45:00'],
'Column_1':[21.17, 19.17, 17.95, 25.0]})
df = df.set_index('Timestamp')
df.head()
Out[68]:
Timestamp Column_1
2010-01-01 00:00:00 21.17
2010-01-01 00:15:00 19.17
2010-01-01 00:30:00 17.95
2010-01-01 00:45:00 25.00
4 rows × 1 columns
In [69]:
df.loc ['2010-01-01 00:00:00':'2010-01-01 00:15:00', 'Column_1'] = [50,60]
df.head()
Out[69]:
Timestamp Column_1
2010-01-01 00:00:00 50.00
2010-01-01 00:15:00 60.00
2010-01-01 00:30:00 17.95
2010-01-01 00:45:00 25.00