简体   繁体   English

添加具有相同列的观测值,并为每个观测值创建一个唯一的行

[英]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.

相关问题 连续计数每个观察值 - Count each observation a row 删除列并为每个删除的列创建唯一的行 Pandas Dataframe - Remove Columns And Create Unique Row For Each Removed Column Pandas Dataframe 如何使用python计算一列中每一行的唯一值? - How to count the unique values of each row in one column with python? 删除对于非唯一 id 具有相同列值的观察 - Removing observations that have the same column values for a non unique id 将相同的列表添加到新列中的pandas DataFrame中的每一行 - Adding the same list to each row in a pandas DataFrame in a new column 删除所有观察值具有相同值的列会影响我的模型吗? - Will removing a column having same values for all observations affect my model? 如何迭代 Pandas 中的列值并根据同一行中多列的值创建新的观察? - How can I iterate over column values in Pandas and create a new observation based on the values of multiple columns in the same row? 将一行中的每个元素相乘并在同一数据框中追加新列? - Multiply each element in one row and append new column in same dataFrame? 使用 Python 将数据框中的所有观察值与列的特定观察值相除 - Dividing all observations in a data frame with a particular observation of a column with Python Pandas:为每个唯一行获取一个新列 - Pandas: get a new column for each unique row
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM