python - How to create a new `DataFrame` with values obtained from another? -
i have dataframe df1:
|company name|contact| representatives | | xyz-corp |adam | mark, john, stacy | i want create new dataframe df2 looks this:
| company name | contact | | xyz-corp | adam | | xyz-corp | mark | | xyz-corp | john | | xyz-corp | stacy | how do this? want split values representatives column , add new rows.
i can use double loop, 1 iterate through rows , iterate through representative names , add new dataframe. there better way this?
df.set_index('company name', inplace=true) df = pd.concat([df.contact, df.loc[:,'representatives'].str.split(',', expand=true).stack().reset_index(-1, drop=true)]).reset_index() df.rename(columns={0: 'contact'}, inplace=true) company name contact 0 xyz-corp adam 1 xyz-corp mark 2 xyz-corp john 3 xyz-corp stacy
Comments
Post a Comment