Pandas:使用基于不同DF的自定义列创建DF

时间:2018-06-08 20:04:44

标签: python pandas dataframe merge

我有两个数据帧,需要更改第二个以适应我的第一个格式,或者我可以创建一个新的数据帧。

第一个数据帧就是......

stores_df = pd.DataFrame({'Stores': ['Kroger', 'United'], 'City':['Dallas','Austin']})

    Stores  City
0   Kroger  Dallas
1   United  Austin

第二种格式是这样的(这是给我的方式,我不能改变它)......

apples_df = pd.DataFrame({'Color': ['Red', 'Green', 'Yellow','Red', 'Green', 'Yellow'], 'Store':['Kroger','Kroger','Kroger','United','United','United'], 'Ripe': ['20','17', '14', '23', '18', '0']})

    Color   Stores  Ripe
0   Red     Kroger  20
1   Green   Kroger  17
2   Yellow  Kroger  14
3   Red     United  23
4   Green   United  18
5   Yellow  United  0

我想要的数据帧是两者的组合,除了颜色值+列标题'成熟'成为目标的列标题,并将计数添加到第一个数据帧的存储行中的每一个。像这样......

    Stores  City    Red Ripe    Green Ripe    Yellow Ripe
0   Kroger  Dallas  20          17            14
1   United  Austin  23          18            0

我对合并/加入有点熟悉,并且与数据透视表搞混,足以让我意识到我需要一些帮助。感谢。

2 个答案:

答案 0 :(得分:4)

IIUC,<div class="outer-container"> <div class="row"> <div class="col-md-4" style="text-align: center;"> <div data-collaborate="projects" class="decisionTreeBox collabProjects" style="font-size: x-large;"> Collaborate on Projects</div> </div> <div class="col-md-4" style="text-align: center;"> <div data-collaborate="files" class="decisionTreeBox collabFiles" style="font-size: x-large;"> Collaborate on Files</div> </div> <div class="col-md-4" style="text-align: center;"> <div data-collaborate="socially" class="decisionTreeBox collabSocially" style="font-size: x-large;"> Collaborate Socially</div> </div> </div> <hr /> <div class="container" style="padding: 0px;"> <div class="row"> <a href="/TrainingResourceCenter/O365Training/Pages/OneDrive.aspx"> <div class="col-md-4 margin-bottom-20" style="text-align: center;"> <div data-decision="files" id="decisionTreeOneDrive"> <h3 id="innerBoxHeadings"><img src="/TrainingResourceCenter/O365Training/PublishingImages/onedrive-logo.png" style="width: 65px; height: 65px; padding-bottom: 5px; padding-right: 10px; vertical-align: middle;" />OneDrive</h3> </div> </div> </a> <!-- <a href="/TrainingResourceCenter/O365Training/Pages/O365.aspx"><div class="col-md-4 margin-bottom-20" style="text-align: center;"> <div data-decision="projects" id="decisionTreeProject"><h3 id="innerBoxHeadings"><img src="/TrainingResourceCenter/O365Training/PublishingImages/Project.png" style="padding-bottom: 5px; padding-right: 10px; vertical-align: middle;"/>Project</h3></div> </div></a> --> <a href="/TrainingResourceCenter/O365Training/Pages/SharePointOnline.aspx"> <div class="col-md-4 margin-bottom-20" style="text-align: center;"> <div data-decision="files" id="decisionTreeSharePoint"> <h3 id="innerBoxHeadings"><img src="/TrainingResourceCenter/O365Training/PublishingImages/SharePointDecisionTree.png" style="padding-bottom: 5px; padding-right: 10px; vertical-align: middle;" />SharePoint</h3> </div> </div> </a> <a href="/TrainingResourceCenter/O365Training/Pages/Teams.aspx"> <div class="col-md-4 margin-bottom-20" style="text-align: center;"> <div data-decision="socially files" id="decisionTreeTeams"> <h3 id="innerBoxHeadings"><img src="/TrainingResourceCenter/O365Training/PublishingImages/TeamsDecisionTree.png" style="padding-bottom: 5px; padding-right: 10px; vertical-align: middle;" />Teams</h3> </div> </div> </a> <a href="/TrainingResourceCenter/O365Training/Pages/Planner.aspx"> <div class="col-md-4 margin-bottom-20" style="text-align: center;"> <div data-decision="projects" id="decisionTreePlanner"> <h3 id="innerBoxHeadings"><img src="/TrainingResourceCenter/O365Training/PublishingImages/PlannerDecisionTree.png" style="padding-bottom: 5px; padding-right: 10px; vertical-align: middle;" />Planner</h3> </div> </div> </a> <a href="/TrainingResourceCenter/O365Training/Pages/Yammer.aspx"> <div class="col-md-4 margin-bottom-20" style="text-align: center;"> <div data-decision="socially" id="decisionTreeYammer"> <h3 id="innerBoxHeadings"><img src="/TrainingResourceCenter/O365Training/PublishingImages/YammerDecisionTree.png" style="padding-bottom: 5px; padding-right: 10px; vertical-align: middle;" />Yammer</h3> </div> </div> </a> </div> </div> </div> <script> </script>两个df,按merge

进行广泛重组
unstack

答案 1 :(得分:2)

pivot_table + merge

以下几个步骤:透视apples_df,然后与stores_df合并。

apples_pvt = apples_df.pivot_table(index='Store', columns='Color',
                                   values='Ripe', aggfunc='sum')

res = stores_df.merge(apples_pvt.add_suffix(' Ripe'), left_on='Stores', right_index=True)

print(res)

   Stores    City Green Ripe Red Ripe Yellow Ripe
0  Kroger  Dallas         17       20          14
1  United  Austin         18       23           0