简体   繁体   中英

Collecting ids in separate dataframe

I am stuck in a situation after grouping my data through ids. Now I want to collect them with full info with the same ids.

Current => I got this result after using group by id's. Expected=>every same id with all info as shown in the pic

输出应该像

dfs=pd.read_excel('tns1.xlsx')
grp = dfs.groupby('entity_id')
da  = grp.groups
for entity_id,show in grp:
  print(show)
  print(da)

Now I have expected output also but the problem is I am getting in extracting them correctly & then to write in excel using the below loop I am trying to extract but I do not think so that it is extracting properly besides it is running thousand of times as many rows are there

So you can collect the individual data to list and then write it to csv.

Data setup for illustration:

import pandas as pd
from io import StringIO
raw_data="""
entity_id,Name,Status,Date
2244,Abhi,Active,10-06-2021
2244,Abhi2,Blocked,10-06-2021
6666,other1,Blocked,10-06-2021
6666,other,Active,10-06-2021
"""
dfs=pd.read_csv(StringIO(raw_data))

append all the dfs in a list:

grp = dfs.groupby('entity_id')
da  = grp.groups
groups_as_list=[]

for entity_id,show in grp:
    groups_as_list.append(show)# append all dfs in a list

then write the list of dataframes to csv.

with open("my_results.csv","w") as f:# open file for write
    for each_df in groups_as_list:
            each_df.to_csv(f,index=False)
            f.write("\n") # write empty line to csv

so the result will look like the following:( you can tweak it further based on need) 在此处输入图片说明

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