Project github address: bitcarmanlee easy-algorithm-interview-and-practice
often have students private messages or leave messages to ask related questions, V number bitcarmanlee. The classmates of star on github, within the scope of my ability and time, I will try my best to help you answer related questions and make progress together.
For a Dataframe in pandas, it is often necessary to create a new column based on one of the columns. For example, a common example: the level range needs to be determined based on the score. Let's take a look at how to achieve it.
def getlevel(score):
if score < 60:
return "bad"
elif score < 80:
return "mid"
else:
return "good"
def test():
data = {'name': ['lili', 'lucy', 'tracy', 'tony', 'mike'],
'score': [85, 61, 75, 49, 90]
}
df = pd.DataFrame(data=data)
# 两种方式都可以
# df['level'] = df.apply(lambda x: getlevel(x['score']), axis=1)
df['level'] = df.apply(lambda x: getlevel(x.score), axis=1)
print(df)
The result of running the above code
name score level
0 lili 85 good
1 lucy 61 mid
2 tracy 75 mid
3 tony 49 bad
4 mike 90 good
To achieve the above functions, the apply method in the dataframe is mainly used.
In the above code, a new column named level is added to the dataframe. The level is derived from the score column. If it is less than 60, it is bad, between 60-80 is mid, and above 80 is good.
Where axis=1 means that the rows of the original dataframe remain unchanged, and the dimensions of the columns have changed.