I have a list of ids that correspond to the row of a data frame. From that list of ids, I want to increment a value of another column that intersects with that id's row.
What I was thinking was something like this:
ids = [1,2,3,4]
for id in ids:
my_df.loc[my_df['id']] == id]['other_column'] += 1
But this doesn't work. How can I mutate the original df, my_df
?
You can use the nunique() function to count the number of unique values in a pandas DataFrame.
To increase your index with 1 you can simply modify the index like this, df. index += 1 . Note: df. index += 1 results in a IntXXIndex whereas df.
Pandas series is a One-dimensional ndarray with axis labels. The labels need not be unique but must be a hashable type. The object supports both integer- and label-based indexing and provides a host of methods for performing operations involving the index.
In order to iterate over rows, we apply a function itertuples() this function return a tuple for each row in the DataFrame. The first element of the tuple will be the row's corresponding index value, while the remaining values are the row values.
try this:
my_df.loc[my_df['id'].isin(ids), 'other_column'] += 1
Demo:
In [233]: ids=[0,2]
In [234]: df = pd.DataFrame(np.random.randint(0,3, (5, 3)), columns=list('abc'))
In [235]: df
Out[235]:
a b c
0 2 2 1
1 1 0 2
2 2 2 0
3 0 2 1
4 0 1 2
In [236]: df.loc[df.a.isin(ids), 'c'] += 100
In [237]: df
Out[237]:
a b c
0 2 2 101
1 1 0 2
2 2 2 100
3 0 2 101
4 0 1 102
the ids are unique, correct? If so, you can directly place the id
into the df
:
ids = [1,2,3,4]
for id in ids:
df.loc[id,'column_name'] = id+1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With