[英]Adding observations having same column and create one unique row for each observation
I am having a dataset as shown below: 我有一个数据集,如下所示:
batsman batting_team 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018
0 A Ashish Reddy Deccan Chargers 0 0 0 0 35 0 0 0 0 0 0
1 A Ashish Reddy Sunrisers Hyderabad 0 0 0 0 0 125 0 73 47 0 0
2 A Chandila Rajasthan Royals 0 0 0 0 0 4 0 0 0 0 0
3 A Chopra Kolkata Knight Riders 42 11 0 0 0 0 0 0 0 0 0
4 A Choudhary Royal Challengers Bangalore 0 0 0 0 0 0 0 0 0 25 0
I am trying to add the columns with same name as per their scores in each year, also, for example, if A Ready is appearing two times, that means, 我正尝试添加与它们每年的分数相同名称的列,例如,如果“准备就绪”出现两次,这意味着,
I am just trying to add to create if else, but not able to get anywhere. 我只是想添加创建,如果没有,但无处可去。
we create one observation from these two, as the following 我们从这两个观察值中创建一个,如下所示
Name - A Reddy 名称-Reddy
Team - second observation team name 小组-第二观察队名称
2008,2009,...,2018 - and add columns data from year columns. 2008,2009,...,2018-并从year列添加列数据。
Try: 尝试:
df_out = df.groupby('batsman').sum()
#Sums all numeric columns of the dataframe
df_out['batting_team'] = df_out.index.map(df.drop_duplicates(['batsman'], keep='last').set_index('batsman')['batting_team'])
#Use drop duplicates to keep the last team and set_index to use in map
df_out.reset_index().reindex(df.columns, axis=1)
#Reset index and reorder dataframe columns like input dataframe
Output: 输出:
batsman batting_team 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018
0 A Ashish Reddy Sunrisers Hyderabad 0 0 0 0 35 125 0 73 47 0 0
1 A Chandila Rajasthan Royals 0 0 0 0 0 4 0 0 0 0 0
2 A Chopra Kolkata Knight Riders 42 11 0 0 0 0 0 0 0 0 0
3 A Choudhary Royal Challengers Bangalore 0 0 0 0 0 0 0 0 0 25 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.