-2

what does this exactly mean? or what does it do? a is a column in the dataframe b is another column in the dataframe both have numbers in each row

df['a'] = df[['a', 'b']].min(axis=1)

I tried doing the research online but dont seem to find an answer

2 Answers2

1

For each row, it compares columns a and b and takes minimum one and overwrites column a with new minimum values. Check pandas.DataFrame.min.

Here is another stackoverflow question-answer.

Ersel Er
  • 731
  • 6
  • 22
0

df['a'] is referring to the column A as a list

df[['a','b']] is referring to the columns A and B as a DataFrame

The code you have shared gets the minimum value of columns A and B, by row, and updates the respective value in the column 'A'.

Try this code out to make sense of what is going on

import pandas as pd

df = pd.DataFrame({'A':[1,2,3,4,5], 'B': [5,4,3,2,1]})
print(df)
df['A'] = df[['A', 'B']].min(axis = 1)
print(df)
Haris
  • 19
  • 2
  • A column isn't type of `list` or sth. else. Pandas columns generally have their custom data types which are series. You can get list from series with `Series.tolist()` – Ersel Er Apr 17 '22 at 06:40