in Education by
I have the dataset that has the no_employees column that is the str object. whats is a best way to create the new column (company_size) in a data frame and fill it with values based on a no_employees column like in example below mental_health_df = pd.read_csv("Mental Health.csv") pd.set_option('display.max_columns', None) mental_health_df.head(100) no_employees company_size | 6-25 |Small More than 1000 |Extremely Large 500-1000 |Very Large 26-100 |Medium 100-500 |Large 1-5 |Very Small Select the correct answer from above options

1 Answer

0 votes
by
 
Best answer
Please bin using df.cut import numpy as np df['company_size']=pd.cut(df['no_employees']. astype('category').cat.codes*10,[-np.inf,9,19,29,39,49,np.inf], labels=['Very Small','Large','Medium','Very Large','Small','Extremely Large']) print(df) no_employees company_size 0 6-25 Small 1 More than 1000 Extremely Large 2 500-1000 Very Large 3 26-100 Medium 4 100-500 Large 5 1-5 Very Small It works like this: #Converted no of employees to codes but for ease of defining bins multiplied by ten df['no_employees']. astype('category').cat.codes*10 #Decided to bin using df.cut pd.cut(df['no_employees']. astype('category').cat.codes*10,\ [-np.inf,9,19,29,39,49,np.inf], labels=['Very Small','Large','Medium','Very Large','Small','Extremely Large']) Learn Python for Data Science Course to improve your technical knowledge.

Related questions

0 votes
    I have the function and want to create a new column df['growth_factor'] which will have a derived value in it. ... can I achieve this? Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    I have a dataframe with 2 columns and I want to add the new column; This new column should be updated based on ... +=1 is not working. Select the correct answer from above options...
asked Jan 11, 2022 in Education by JackTerrance
0 votes
    I have a set of oil wells compiled in the panda's data frame. It looks like this: wells = pd.DataFrame({'date ... -01-01 FIELDZ 10.8 Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    I have 2 data frames df1 Name 2010 2011 0 Jack 25 35 1 Jill 15 20 df2 Name 2010 2011 0 Berry 45 25 1 ... used the code df1.add(df2) Select the correct answer from above options...
asked Jan 18, 2022 in Education by JackTerrance
0 votes
    I have data like this: id a b c d 1 y y z z 2 y z y y 3 y y y y I want to count the value of "y" ... 1 2 2 1 3 4 Can anyone help me? Select the correct answer from above options...
asked Jan 9, 2022 in Education by JackTerrance
0 votes
    A few days ago I've started learning pygame. So, now I've got the code which allows me to draw different ... ) py.display.update() Select the correct answer from above options...
asked Jan 11, 2022 in Education by JackTerrance
0 votes
    I have col1 in a pandas df. I want to make col2: col1 col2 1 1 1 2 1 3 1 4 2 2 ... questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 22, 2022 in Education by JackTerrance
0 votes
    I have a 20 x 4000 dataframe in python using pandas. Two of these columns are named Year and quarter. I'd ... anyone help with that? Select the correct answer from above options...
asked Jan 28, 2022 in Education by JackTerrance
0 votes
    I have the list of sets like so. I basically want to convert this to the dictionary and to address duplicate keys, ... ] = val return d Select the correct answer from above options...
asked Jan 9, 2022 in Education by JackTerrance
0 votes
    I have this dataframe and trying to select the last n (=2) rows if the present value is True so I code as ... , I should select 50,40 Select the correct answer from above options...
asked Jan 8, 2022 in Education by JackTerrance
0 votes
    I want to calculate a percentage, for each id, of True values from all rows of the id. Here an example ... df.num_true/df.num_col1_id Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    To the data frame df: Player Team Points Mean Price Value Gameweek 1 Jim Leeds 4.4 4.40 10.44 0.44 2 Jim ... scalar What am I missing? Select the correct answer from above options...
asked Jan 18, 2022 in Education by JackTerrance
0 votes
    Suppose I have a Tensorflow tensor. How do I get the dimensions (shape) of the tensor as integer values? I ... 'Dimension' instead. Select the correct answer from above options...
asked Feb 8, 2022 in Education by JackTerrance
0 votes
    I want to select rows from a DataFrame based on values in some column in pandas, How can I do it? I ... WHERE column_name = some_value Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    In python, I want to sum all of the values in a variable. My code is x=100 y=200 for i in range(a,b) ... of all numbers which is 7500. Select the correct answer from above options...
asked Jan 11, 2022 in Education by JackTerrance
...