MRHarv
MRHarv

Reputation: 503

Creating a column to have different values depending on its value in another column in pandas

Consider the following (dummy) pandas dataset:

enter image description here

How would the 'Type' column be constructed in Python?

To clarify, it needs to be constructed in a way that the 6 highest month values have an 'F' in Type, with the remaining rows having an 'A' in Type. This is because the Month column may not always be from 0-10 - e.g. if the Month ranged from 0-15, then Months 10-15 would have an 'F' in Type.

Upvotes: 0

Views: 42

Answers (2)

Marco Goldin
Marco Goldin

Reputation: 130

Even better with numpy:

month_limit = df['Month'].max()-5

df['type'] = np.where(df['Month'] >= month_limit, 'F', 'A')

Upvotes: 2

Rafaó
Rafaó

Reputation: 599

Hope this will work:

month_limit = df['Month'].max()-5
df['Type'] = ['F' if x >= month_limit else 'A' for x in df['Month']]

Upvotes: 0

Related Questions