让我有这样一个数据框:
test_df = pd.DataFrame(
0,
index = pd.IntervalIndex.from_breaks([100, 200, 300, 400]),
columns = ['A', 'B', 'C']
)
所以它会给我们这个:
A B C
(100, 200] 0 0 0
(200, 300] 0 0 0
(300, 400] 0 0 0
现在,假设我想更改给定间隔内的特定值,在我的数据框中的某些间隔内具有整数值。
我可以通过 .loc 获取特定给定整数值的间隔值,方法如下: test_df.loc[250]。这将给我系列。
但是!
第一种情况:
test_df.at[250, 'B']
------
ValueError: At based indexing on an non-integer index can only have non-integer indexers
为什么在这种情况下我不能使用整数值 250?它适用于 .loc 中的整数类型。
第二种情况:
test_df.loc[250, 'B']
------
'pandas._libs.interval.IntervalTree' object has no attribute 'get_value'
没有属性get_value?那么我怎样才能获得值(value)呢?我只能通过系列来完成,通过 .loc 属性获取它吗?
第三种情况:
test_df.at[250, 'B'] = 10
------
'pandas._libs.interval.IntervalTree' object has no attribute 'set_value'
主要问题
我应该如何在数据框中设置特定值?
请您参考如下方法:
注意 loc 的行为是 fixed并将出现在即将发布的 0.24.0 版本中:
In [1]: import pandas as pd; pd.__version__
Out[1]: '0.24.0.dev0+870.g7191af9b4'
In [2]: test_df = pd.DataFrame(
...: 0,
...: index = pd.IntervalIndex.from_breaks([100, 200, 300, 400]),
...: columns = ['A', 'B', 'C']
...: )
In [3]: test_df
Out[3]:
A B C
(100, 200] 0 0 0
(200, 300] 0 0 0
(300, 400] 0 0 0
In [4]: test_df.loc[250, 'B']
Out[4]: 0
In [5]: test_df.loc[250, 'B'] = 100
In [6]: test_df
Out[6]:
A B C
(100, 200] 0 0 0
(200, 300] 0 100 0
(300, 400] 0 0 0
