0

I have an example dataframe as given below:

df = 
index                Value     Name         
2019-01-01 07:53:53  10   S_1.M_01
2019-01-01 08:33:52  20   S_1.M_02
2019-01-01 09:13:52  30   S_1.M_03
2019-01-01 09:53:52  40   S_1.M_01
2019-01-01 10:33:52  50   S_1.M_02
2019-01-01 11:13:53  60   S_1.M_03                                                                     
...  ...       ...
2019-09-08 15:38:52  100  8157_S2

Now I want to extract to those columns with name Name and create outputs. How to extract those rows with same names?

My code:

df_grp = iv_df['element'].unique

This did not yield any result.

I want to achieve something like this below different outputs

Output1:

index                Value     Name         
2019-01-01 07:53:53  10   S_1.M_01
2019-01-01 09:53:52  40   S_1.M_01

Output2:

index                Value     Name         
2019-01-01 08:33:52  20   S_1.M_02
2019-01-01 10:33:52  50   S_1.M_02

Output3:

index                Value     Name         
2019-01-01 09:13:52  30   S_1.M_03
2019-01-01 11:13:53  60   S_1.M_03    

How to achieve this?

1 Answer 1

1

I think there may be code missing from your question, or it isn't clear or I am not fully understanding it. But you can try something like this, where you are iterating through the unique values and creating new dfs.

dfs = []
for val in df.Name.unique():
    dfs.append(df.loc[df.Name == val, :])

This will give you a list, dfs, populated with smaller dfs that are just excerpts of your larger df.

Sign up to request clarification or add additional context in comments.

8 Comments

This could create a 26 dataframes. Is there way to extract different outputs? SO I dont have to create 26 dfs
Instead of appending them to a list, you could just create a temporary df and run whatever operation you need to on the subset.
Not sure I understand what you mean by 'extract different outputs'
What you suggested in your previous comment is what I wanted. Thanks a ton.
Yes, you could iterate through them again or just before you append to the list and do something like df['new_column'] = None. But it would be better to do that on the original data frame, so that you are only creating a new column once, not 26 times
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.