I have data in 1 min intervals, I want to group the column inputs as low-medium-high.
If between 0-33: low If between 33-67: medium If between 67-100: high
from this:
df
timestamp val1
2019-06-05 40
2019-06-05 12
2019-06-05 78
to this:
df
timestamp val1
2019-06-05 medium
2019-06-05 low
2019-06-05 high
CodePudding user response:
You can use pandas.cut()
.
bins = [0, 33, 67, 100]
labels = ['low', 'meduin', 'high']
df['val1'] = pd.cut(df['val1'], bins, labels=labels)
print(df)
timestamp val1
0 2019-06-05 meduin
1 2019-06-05 low
2 2019-06-05 high
CodePudding user response:
You could do something like:
df['val1'] = df['val1'].apply(lambda x: 'low' if 0 < x < 33 else 'med' if 33 <= x < 66 else 'high')
CodePudding user response:
Another possible solution, based on numpy.where
:
df['val1'] = np.where(df.val1 <= 33, 'low',
np.where(df.val1 < 67, 'medium', 'high'))
Output:
timestamp val1
0 2019-06-05 medium
1 2019-06-05 low
2 2019-06-05 high