0
$\begingroup$

Need to replace database of the column in specific refine query with multiple operations as mention in below image. Trying such operation as an individual, but can't understand which method to use can make in one column with multiple operations.

Using Boolean methods to justify results but how can I do in one line code of python to get a replacement of refining/ categorized values to a specific column.

import pandas as pd
db = pd.read_csv('Database Path')
db.head(2)

stoppage time in minutes | Activity
-------------------------120 | Stopped
-------------------------240 | Stopped

#Need to refine below code for specific requirement as mention above:

db['New Column(Time Category)'] =

db['Stoppage time in minutes'] < 120 OR
db['Stoppage time in minutes'] > 120 & db['Stoppage time in minutes'] < 240 OR
db['Stoppage time in minutes'] > 240 & db['Stoppage time in minutes'] < 360 OR

# So Need Result like as below:

stoppage time in minutes | Activity | New Column(Time Category)
-------------------------120 | Stopped | <2 HRS
-------------------------240 | Stopped | 2 - 4 HRS

enter image description here

Require this solution due to a pool of high numbers of databases.

$\endgroup$

1 Answer 1

0
$\begingroup$

You can use the python ternary operator with list comprehension:

df['Buckets(HRS)'] =
 ['<2hrs' if x<=120 else '2-4' if 120>x<=240 else '4-6' if 240>x<=360 else '>6' for x in db['Stoppage time in minutes']]
$\endgroup$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.