如何更新 pandas DataFrame 中单行的 DatetimeIndex 值?
How to update the value of DatetimeIndex of a single row in a pandas DataFrame?
在 python pandas DataFrame 中,我想在一行中更新索引的值(最好就地更新,因为 DataFrame 非常大)。
索引为 DatetimeIndex,DataFrame 可能包含多个列。
例如:
In [1]: import pandas as pd
In [2]: pd.DataFrame({'DATA': [1,2,3]},
index=[pd.Timestamp(2011,10,01,00,00,00),
pd.Timestamp(2011,10,01,02,00,00),
pd.Timestamp(2011,10,01,03,00,00)])
Out[5]:
DATA
2011-10-01 00:00:00 1
2011-10-01 02:00:00 2
2011-10-01 03:00:00 3
期望的输出是:
DATA
2011-10-01 01:00:00 1 <---- Index changed !!!
2011-10-01 02:00:00 2
2011-10-01 03:00:00 3
对于大型数据帧,是否有一种简单(且便宜)的方法来做到这一点?
假设样本的位置已知(例如它是需要更改的第 n 行)!
一种可能的解决方案 Series.replace
, but first need convert Index.to_series
:
df.index = df.index
.to_series()
.replace({pd.Timestamp('2011-10-01'): pd.Timestamp('2011-10-01 01:00:00')})
print (df)
DATA
2011-10-01 01:00:00 1
2011-10-01 02:00:00 2
2011-10-01 03:00:00 3
Index.where
的另一种解决方案(0.19.0
中的新解决方案):
df.index = df.index.where(df.index != pd.Timestamp('2011-10-01'),
[pd.Timestamp('2011-10-01 01:00:00')])
print (df)
DATA
2011-10-01 01:00:00 1
2011-10-01 02:00:00 2
2011-10-01 03:00:00 3
通过 drop
, last sort_index
添加新行并删除旧行的解决方案:
df.loc[pd.Timestamp('2011-10-01 01:00:00')] = df.loc['2011-10-01 00:00:00', 'DATA']
df.drop(pd.Timestamp('2011-10-01 00:00:00'), inplace=True)
df.sort_index(inplace=True)
print (df)
DATA
2011-10-01 01:00:00 1
2011-10-01 02:00:00 2
2011-10-01 03:00:00 3
另一个 如果需要按值而不是按位置替换:
df.index.set_value(df.index, pd.Timestamp(2011,10,1,0,0,0), pd.Timestamp(2011,10,1,1,0,0))
print (df)
DATA
2011-10-01 01:00:00 1
2011-10-01 02:00:00 2
2011-10-01 03:00:00 3
最后一个解决方案,将 index
从 转换为 numpy array
:
i = 0
df.index.values[i] = pd.Timestamp('2011-10-01 01:00:00')
print (df)
DATA
2011-10-01 01:00:00 1
2011-10-01 02:00:00 2
2011-10-01 03:00:00 3
如果您已经知道要操作的索引,那么快速的方法是直接查找,然后您可以在 Index.set_value
:
的帮助下相应地设置它的值
df.index.set_value(df.index, df.index[0], pd.Timestamp(2011,10,1,1,0,0))
# <-index-> <-row num-> <---value to be inserted--->
这是一个就地操作,所以你不需要将结果赋值给它自己。
在 python pandas DataFrame 中,我想在一行中更新索引的值(最好就地更新,因为 DataFrame 非常大)。
索引为 DatetimeIndex,DataFrame 可能包含多个列。
例如:
In [1]: import pandas as pd
In [2]: pd.DataFrame({'DATA': [1,2,3]},
index=[pd.Timestamp(2011,10,01,00,00,00),
pd.Timestamp(2011,10,01,02,00,00),
pd.Timestamp(2011,10,01,03,00,00)])
Out[5]:
DATA
2011-10-01 00:00:00 1
2011-10-01 02:00:00 2
2011-10-01 03:00:00 3
期望的输出是:
DATA
2011-10-01 01:00:00 1 <---- Index changed !!!
2011-10-01 02:00:00 2
2011-10-01 03:00:00 3
对于大型数据帧,是否有一种简单(且便宜)的方法来做到这一点?
假设样本的位置已知(例如它是需要更改的第 n 行)!
一种可能的解决方案 Series.replace
, but first need convert Index.to_series
:
df.index = df.index
.to_series()
.replace({pd.Timestamp('2011-10-01'): pd.Timestamp('2011-10-01 01:00:00')})
print (df)
DATA
2011-10-01 01:00:00 1
2011-10-01 02:00:00 2
2011-10-01 03:00:00 3
Index.where
的另一种解决方案(0.19.0
中的新解决方案):
df.index = df.index.where(df.index != pd.Timestamp('2011-10-01'),
[pd.Timestamp('2011-10-01 01:00:00')])
print (df)
DATA
2011-10-01 01:00:00 1
2011-10-01 02:00:00 2
2011-10-01 03:00:00 3
通过 drop
, last sort_index
添加新行并删除旧行的解决方案:
df.loc[pd.Timestamp('2011-10-01 01:00:00')] = df.loc['2011-10-01 00:00:00', 'DATA']
df.drop(pd.Timestamp('2011-10-01 00:00:00'), inplace=True)
df.sort_index(inplace=True)
print (df)
DATA
2011-10-01 01:00:00 1
2011-10-01 02:00:00 2
2011-10-01 03:00:00 3
另一个
df.index.set_value(df.index, pd.Timestamp(2011,10,1,0,0,0), pd.Timestamp(2011,10,1,1,0,0))
print (df)
DATA
2011-10-01 01:00:00 1
2011-10-01 02:00:00 2
2011-10-01 03:00:00 3
最后一个解决方案,将 index
从 numpy array
:
i = 0
df.index.values[i] = pd.Timestamp('2011-10-01 01:00:00')
print (df)
DATA
2011-10-01 01:00:00 1
2011-10-01 02:00:00 2
2011-10-01 03:00:00 3
如果您已经知道要操作的索引,那么快速的方法是直接查找,然后您可以在 Index.set_value
:
df.index.set_value(df.index, df.index[0], pd.Timestamp(2011,10,1,1,0,0))
# <-index-> <-row num-> <---value to be inserted--->
这是一个就地操作,所以你不需要将结果赋值给它自己。