简体   繁体   English

熊猫创建新的数据框,从多个观测值中选择最大值

[英]Pandas create new dataframe choosing max value from multiple observations

I would like to make a new dataframe based on a max value from a column. 我想基于列的最大值创建一个新的数据框。 However I have multiple observations from the same respondent and I only want to select the maximum value from the column value1 for each respondent. 但是,我从同一个响应者那里得到了多个观察结果,我只想从每个响应者的列value1中选择最大值。 Here is a simplifyed example: 这是一个简化的示例:

df: DF:

   respondent  value1  value2
0           1       3      12
1           1       5      34
2           1       1      43
3           2       4      12
4           2       6      34
5           2       9      54
6           3       2      32
7           3       1       2
8           3       3      21

Here is the result I would like to have: 这是我想要的结果:

newdf: newdf:

  respondent  value1  value2
0           1       5      34
1           2       9      54
2           3       3      21

Any ideas? 有任何想法吗?

The following achieves what you want and appears to be faster than @CT Zhu's answer: 以下实现了您想要的目标,并且看起来比@CT Zhu的答案要快:

In [30]:

df.loc[df.groupby('respondent').value1.idxmax().values]
Out[30]:
   respondent  value1  value2
1           1       5      34
5           2       9      54
8           3       3      21
In [31]:

%timeit df.loc[df.groupby('respondent').value1.idxmax().values]
%timeit df[df.groupby('respondent').value1.transform(lambda x: x==x.max())]
%timeit df.sort(['respondent', 'value1'], ascending=[1,0]).groupby('respondent').head(1)
100 loops, best of 3: 1.76 ms per loop
100 loops, best of 3: 2.99 ms per loop
100 loops, best of 3: 4.42 ms per loop

Also the above was achieved on pandas version 0.12.0 64-bit using python 3.3 上面的内容也是使用python 3.3在熊猫版本0.12.0 64位上实现的

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

相关问题 从符合特定标准的观察中创建新的 Pandas Dataframe - Create new Pandas Dataframe from observations which meets specific criteria 从 pandas 值计数创建新的 dataframe - create new dataframe from pandas value counts 从现有的熊猫数据帧创建新的熊猫数据帧,每天最多和最小 - Create new pandas dataframe from an existing one only with max and min per day 从最小值到最大值对多列的值进行排序,并将新列放入 pandas dataframe - Sort multiple columns' values from min to max, and put in new columns in pandas dataframe pandas select 每列的最小值和最大值并创建一个新的 dataframe - pandas select the minimum and max of each column and create a new dataframe 如何获得最大值为 k 连续行的新 pandas dataframe? - How to get new pandas dataframe with max value of k consecutive rows? 从熊猫数据框中提取群体观察结果 - Extracting group observations from pandas dataframe Pandas DataFrame合并选择更高的值 - Pandas DataFrame merge choosing the higher value Python pandas:根据组内的最大值创建新列,但使用来自附加(字符串)列的值 - Python pandas: create new column based on max value within group, but using value from additional (string) column 如何基于来自熊猫中其他数据框的多个条件在数据框中创建新的布尔列 - How to create a new boolean column in a dataframe based on multiple conditions from other dataframe in pandas
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM