user10214452
user10214452

Reputation:

How to set a value of a panda dataframe between two indices?

I would like to set a value to a panda dataframe based on the values of another column. In a nutshell, for example, if I wanted to set indices of a column my_column of a pandas dataframe pd where another column, my_interesting_column is between 10 and 30, I would like to do something like:

start_index=pd.find_closest_index_where_pd["my_interesting_column"].is_closest_to(10)
end_index=pd.find_closest_index_where_pd["my_interesting_column"].is_closest_to(30)
pd["my_column"].between(star_index, end_index)= some_value

As a simple illustration, suppose I have the following dataframe

df = pd.DataFrame(np.arange(10, 20), columns=list('A'))
df["B"]=np.nan
>>> df
    A   B
0  10 NaN
1  11 NaN
2  12 NaN
3  13 NaN
4  14 NaN
5  15 NaN
6  16 NaN
7  17 NaN
8  18 NaN
9  19 NaN

How can I do something like

df.where(df["A"].is_between(13,16))= 5

So that the end results looks like

>>> df
    A   B
0  10 NaN
1  11 NaN
2  12 NaN
3  13 5
4  14 5
5  15 5
6  16 5
7  17 NaN
8  18 NaN
9  19 NaN

Upvotes: 0

Views: 2072

Answers (1)

Atul Shanbhag
Atul Shanbhag

Reputation: 636

pd.loc[start_idx:end_idx, 'my_column'] = some_value

I think this is what you are looking for

df.loc[(df['A'] >= 13) & (df['A'] <= 16), 'B'] = 5 

Upvotes: 2

Related Questions