简体   繁体   English

如何使用其他数据框列的值转换数据框的列值

[英]How to transform a column value of a dataframe with values of another dataframe columns

How to add a column "main_category" to orig_diff which will indicate under which main category does the sub-category belong to. 如何在orig_diff中添加列“ main_category”,该列将指示子类别属于哪个主要类别。 For instance orig_df with value "Movie" must have the "main_category" as "Entertainment" and "Maths" as "Education". 例如,值“电影”的orig_df必须将“ main_category”作为“娱乐”,将“ Maths”作为“教育”。

import pandas as pd
import numpy as np

orig_df = pd.DataFrame({"sub_cat" : ["Movie", "Science", "Maths", "Music", "Songs", "Dance", "English", "Maths", "Songs"], "Student": ["Stud1", "Stud2", "Stud3", "Stud4", "Stud5", "Stud6", "Stud7", "Sud8", "Stud9"]})
sub_df = pd.DataFrame({"Education": [0,1,1,0,0,0,1], "Entertainment": [1,0,0,1,1,1,0]}, index=["Movie", "Science", "Maths", "Music", "Songs", "Dance", "English"])
print(orig_df)
print(sub_df)

One way is to create a dictionary from sub_df by iterating rows. 一种方法是通过迭代行从sub_df创建字典。

Then use dictionary as map on orig_df['sub_cat'] : 然后使用字典作为orig_df['sub_cat']上的地图:

d = {idx: next(k for k in sub_df if row[k]==1)
     for idx, row in sub_df.iterrows()}

orig_df['main_category'] = orig_df['sub_cat'].map(d)

print(orig_df)

  Student  sub_cat  main_category
0   Stud1    Movie  Entertainment
1   Stud2  Science      Education
2   Stud3    Maths      Education
3   Stud4    Music  Entertainment
4   Stud5    Songs  Entertainment
5   Stud6    Dance  Entertainment
6   Stud7  English      Education
7    Sud8    Maths      Education
8   Stud9    Songs  Entertainment

Note this assumes that each sub_cat only maps to one of "Education" or "Entertainment." 请注意,这假定每个sub_cat仅映射到“教育”或“娱乐”之一。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将数据帧的列值转换为具有不同索引的另一个数据帧? - How to transform column values of a dataframe to another dataframe with different indexes? DataFrame将列值转换为新列 - DataFrame transform column values to new columns Pandas数据框-将列值转换为单个列 - Pandas dataframe - transform column values into individual columns Pandas 将 dataframe 的列重命名为另一个 dataframe 的值,如果两个 Z6A8064B53C47945557755705 列的值匹配 - Pandas rename column of dataframe to value of another dataframe if values of two dataframe columns match 如果其他两个列在Pandas中具有匹配的值,如何用另一个数据框的值填充空列的值? - How to fill empty column values with another dataframe's value if two other columns have matching values in Pandas? 如何根据来自 onw dataframe 的两列的值与另一个 dataframe 的列的值和名称进行比较来获取正确的值 - how to pick up correct value based on values from tow columns of onw dataframe comparing with value and name of the column of another dataframe 如何转换 dataframe 列 - How to transform dataframe columns 如何转换列的值以扩展数据框? - How to transform the column‘s values to spread the Dataframe? 如何将 dataframe 中的列设置为从另一列获得的值? - How to set the columns in dataframe to the values obtained from another column? 如何将 MultiIndex 转换为 dataframe,其中一个索引作为行,第二个索引作为列,另一列作为值 - How do I transform a MultiIndex to a dataframe with one index as rows, second index as columns and another columns as values
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM