Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/extjs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 添加具有相同列的观测值,并为每个观测值创建一个唯一的行_Python_Python 3.x_Pandas_Dataframe - Fatal编程技术网

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

Python 添加具有相同列的观测值,并为每个观测值创建一个唯一的行,python,python-3.x,pandas,dataframe,Python,Python 3.x,Pandas,Dataframe,我有一个数据集,如下所示: 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 Ashis

我有一个数据集,如下所示:

          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
我试着根据他们每年的分数添加同名的列,例如,如果一个Ready出现两次,这意味着

我只是想添加以创建if-else,但没有任何结果

我们从这两个方面创建一个观察结果,如下所示

名字-红色

小组-第二观察小组名称

20082009,…,2018-并从年份列中添加列数据。

尝试:

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
输出:

          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
尝试:

输出:

          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

您可以发布此代码段所需的输出吗?听起来你只需要
df.groupby('batsman').sum()
我需要年份列中的输出,正如它给出的那样,你的代码,但我还需要击球队列和用新的最新击球列条目回复的值。试试这个:
u=df.groupby('batsman');pd.concat([u.sum(),u['batting_team'].last()],axis=1)
您能为这个片段发布所需的输出吗?听起来你只需要
df.groupby('batsman').sum()
我需要年份列中的输出,正如它给出的那样,你的代码,但我还需要击球队列和用新的最新击球列条目回复的值。试试这个:
u=df.groupby('batsman');pd.concat([u.sum(),u['batting_team'].last()],axis=1)