ahmad
ahmad

Reputation: 1

How to change values in a data frame at different indexes

For example, I have a dataframe of size 100 and i have an array of 20 values stored in x

df = pd.DataFrame({'value': np.arange(100)})

How can I change values at index 70 to 90 with the array x

Upvotes: 0

Views: 189

Answers (2)

yatu
yatu

Reputation: 88236

Just index the dataframe on that position, at is enough for single value indexing:

df.at[70, 'value'] = 90

For indices 70 to 90:

df.loc[70:90, 'value'] = 90

Upvotes: 1

Christian Sloper
Christian Sloper

Reputation: 7510

You can do it with .iloc:

df.iloc[70:90,0] = x

Here 70:90 is the row number (starts at 0), and the 0 is the column index

You can also use .loc if your index is numerated (like yours):

70-90 is actually 21 numbers, so i will assume for this example that you actually want to change where the indices go from 70-89 inclusive.

df.loc[70:89,"value"] = x

Upvotes: 0

Related Questions