Reputation: 39
I want to replace a set of values from a series in pre-set timeseries dataframe e.g. 3 values from series a will replace 3 timestamp values. However, replace or .set_value command don't seems to be working.In this cases, timestamp is already in indexed form. Timestamp example series are below:
> timestamp Coloum_1
> 01/01/2010 00:00 21.17
> 01/01/2010 00:15 19.67
> 01/01/2010 00:30 17.95
a = pd.Series([15.0,16.0,17.0]) df.set_value("index=pd.DatetimeIndex(start='2010/01/1 00::00', periods=3, freq='900 sec')", 'Column_1', 'a')
Kinda new in python. Appreciate any support. Thanks.
The output may look like the bottom one. Here the timestamp and Column1 data are in a separate excel file where the Python read it and extract for analysis. Timestamp are already in indexed form and only the column1 values needed to replace by a separate series value. However, it doesn't seems to replace the series values with a timestamp sequence.
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
But if I use only a single timestamp it is replacing the corresponding value.
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
Upvotes: 1
Views: 563
Reputation: 39
Solved
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
Upvotes: 2
Reputation: 25639
Try this:
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
Edit:
df4['Column_1'] = [50.0,60.0,70.0]
Upvotes: 0