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
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.