'Return the max value in a new column for each repeating value in the other column [duplicate]
Background: I was hoping to generate a new column named: datasample based on another column named: end_bin from a table.
Question: Is there a way to return the max value in each row of the new column if the value is repeated in the previous column.
Expected result:
| end_bin | datasample |
|---|---|
| 6 | 1 |
| 8 | 1 |
| 10 | 1 |
| 2 | 3 |
| 3 | 1 |
| 2 | 3 |
| 2 | 3 |
I couldnt find a method to do this in pandas, any help is appreciated:)
Solution 1:[1]
Your question is unclear, but it looks like you want the size per group:
df['datasample'] = df.groupby('end_bin')['end_bin'].transform('size')
Output:
end_bin datasample
0 6 1
1 8 1
2 10 1
3 2 3
4 3 1
5 2 3
6 2 3
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | mozway |
