filling a column values with max value in pandas

Viewed 868

I have some data like this:

pd.DataFrame({'code': ['a', 'a', 'a', 'b', 'b', 'c'],
                      'value': [1,2,3, 4, 2, 1] })



+-------+------+-------+
| index | code | value |
+-------+------+-------+
| 0     | a    | 1     |
+-------+------+-------+
| 1     | a    | 2     |
+-------+------+-------+
| 2     | a    | 3     |
+-------+------+-------+
| 3     | b    | 4     |
+-------+------+-------+
| 4     | b    | 2     |
+-------+------+-------+
| 5     | c    | 1     |
+-------+------+-------+

i want add a column that contain the max value of each code :

| index | code | value | max |
|-------|------|-------|-----|
| 0     | a    | 1     | 3   |
| 1     | a    | 2     | 3   |
| 2     | a    | 3     | 3   |
| 3     | b    | 4     | 4   |
| 4     | b    | 2     | 4   |
| 5     | c    | 1     | 1   |

is there any way to do this with pandas?

2 Answers

Use GroupBy.transform for new column of aggregated values:

df['max'] = df.groupby('code')['value'].transform('max')

You can try this as well.

df["max"] = df.code.apply(lambda i : max(df.loc[df["code"] == i]["value"]))
Related