简体   繁体   中英

Creating a new Dataframe using existing data in a column

I am trying to create a new dataframe based on the data shown in the below Dataframe link. Basically I need to create 6 new columns based on the value of "Keyword Type" Which gives me each article per row and all the corresponding keyword type information. So the columns would be Article ID, Sport, Competition, Context, etc... and the first row would be Article 1's corresponding info. I need it per article so I can join it to another dataframe's article column and bring this info in. Is there an efficient way to do this? Click here to view Dataframe

Current Structure:

Article ID  | Keyword Type | Keyword Value  
Article 1   | Sport        | Football  
Article 1   | Team         | Manchester United  
Article 1   | Language     | English
Article 1   | Context      | News

Expected Output:

Article ID | Sport    | Team              | Language  | Context  
Article 1  | Football | Manchester United | English   | News

Do the following:

res = pd.pivot_table(df, columns="Keyword Type", index="Article ID", aggfunc=lambda x:x)
res = res.droplevel(0, axis="columns")

The result is:

           Context Language     Sport               Team
Article ID                                              
Article 1     News  English  Football  Manchester United

A combination of set_index and unstack could get you your desired output:

df.set_index(['Article ID','Keyword Type'])
  .unstack()
  .droplevel(0,axis=1)
  .rename_axis(None,axis=1)


            Context     Language     Sport         Team
Article ID              
Article 1   News        English     Football    Manchester United

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