简体   繁体   中英

Extracting unique list of strings from Python Dataframe column

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM