Reputation: 503
Consider the following (dummy) pandas dataset:
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
Reputation: 130
Even better with numpy:
month_limit = df['Month'].max()-5
df['type'] = np.where(df['Month'] >= month_limit, 'F', 'A')
Upvotes: 2
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