Home > other >  How to apply a python code on rows that are the same?
How to apply a python code on rows that are the same?

Time:06-21

I have an Excel file containing some data. The first row contains names, some of which are similar.

I want to create a loop in Python in order to apply an operation on each group of similar rows.

I tried this but it didn't help because it shows only one group of my data :

df.loc[df['name'] == 'sofia']

CodePudding user response:

Can you give us some example data and describe what you want to do with the rows?

Spontaneously it sounds like you should do something like:

def your_func(x):
   your operations
   return results of operations



df['col_to_upd'] = df.groupby('name')['col_to_upd'].apply(your_func)

CodePudding user response:

This page may be relevant for you to look up the Pandas functions. Like erikheld writes, it is different to give an exact solution to your problem, with no matrix provided, or example of the operation you want to perform on each group.

  • Related