Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas: how to increment a column's cell value based on a list of ids

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?

like image 752
TheRealFakeNews Avatar asked Sep 12 '16 21:09

TheRealFakeNews


People also ask

How do you count unique occurrences in pandas?

You can use the nunique() function to count the number of unique values in a pandas DataFrame.

How do you increase the index of a data frame?

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.

Can you index a series in pandas?

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.

How do pandas iterate over cells?

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.


2 Answers

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
like image 133
MaxU - stop WAR against UA Avatar answered Oct 14 '22 02:10

MaxU - stop WAR against UA


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
like image 3
NickBraunagel Avatar answered Oct 14 '22 01:10

NickBraunagel