Alex-droid AD
Alex-droid AD

Reputation: 645

Set particular value in DataFrame with interval index

Let I have a dataframe like this one:

test_df = pd.DataFrame(
        0, 
        index = pd.IntervalIndex.from_breaks([100, 200, 300, 400]),
        columns = ['A', 'B', 'C']
)

So it will give us this:

            A   B   C
(100, 200]  0   0   0
(200, 300]  0   0   0
(300, 400]  0   0   0

Now, suppose, I want to change particular value in given interval, having integer value in some of intervals I have in my dataframe.

I can get values of interval by .loc for particular given integer value by this weay: test_df.loc[250]. This will give me series.

But!

The first case:

test_df.at[250, 'B']
------
ValueError: At based indexing on an non-integer index can only have non-integer indexers

Why can't I user integer value 250 in this case? It works great with integer type in .loc.


The second case:

test_df.loc[250, 'B']
------
'pandas._libs.interval.IntervalTree' object has no attribute 'get_value'

Has no attribute get_value? So how can I get value then? Can I do it only through series, by getting it via .loc property?


The third case:

test_df.at[250, 'B'] = 10
------
'pandas._libs.interval.IntervalTree' object has no attribute 'set_value'

Main question
How should I set particular value then in dataframe?

Upvotes: 1

Views: 2268

Answers (3)

ALollz
ALollz

Reputation: 59519

.loc currently works if you provide it with a list for the second set of labels:

test_df.loc[250, ['B']]
#B    0
#Name: (200, 300], dtype: int64

test_df.loc[250:400, ['B', 'C']]
#            B  C
#(200, 300]  0  0
#(300, 400]  0  0

test_df.loc[250, ['B']] = 10
print(test_df)
#            A   B  C
#(100, 200]  0   0  0
#(200, 300]  0  10  0
#(300, 400]  0   0  0

Upvotes: 2

root
root

Reputation: 33783

Note that the behavior with loc has been fixed and will be present in the upcoming 0.24.0 release:

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

Upvotes: 2

Khalil Al Hooti
Khalil Al Hooti

Reputation: 4506

test_df.loc[250].at['B'] = whatever

but make sure whatever is the same type as column data type

Upvotes: 1

Related Questions