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
CodePudding user response:
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.
CodePudding user response:
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)