in Education by
I have col1 in a pandas df. I want to make col2: col1 col2 1 1 1 2 1 3 1 4 2 2 2 3 2 4 3 3 3 4 4 4 In other words, for each distinct value in col1 I want col2 to be increasing integers that start with the value in col1 and counts up by one until there are no more rows. The data is structured in a way so that max(col1) = 2450: when col1 = 1, there are 2450 rows when col1 = 2, there are 2449 rows when col1 = 2450, there is 1 row JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
You could GroupBy column col1, take the cumcount of the groups and add col1: df['col2'] = df.groupby('col1').cumcount().add(df.col1) Output col1 col2 0 1 1 1 1 2 2 1 3 3 1 4 4 2 2 5 2 3 6 2 4 7 3 3 8 3 4 9 4 4

Related questions

0 votes
    I have the dataset that has the no_employees column that is the str object. whats is a best way to create the new ... 1-5 |Very Small Select the correct answer from above options...
asked Jan 19, 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
    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 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 three columns like follows: Sales Population Income Price 14 48777.0 285.0 nan 17 12550.0 ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    I have a dataframe with which if a cell has a value other than "." then I need python to return ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 3, 2022 in Education by JackTerrance
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 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
    SELECT id, amount FROM report I need amount to be amount if report.type='P' and -amount if report.type='N' ... this to the above query? Select the correct answer from above options...
asked Jan 28, 2022 in Education by JackTerrance
0 votes
    In [31]: print(np.poly1d((3,2))) 3 x + 2 In [32]: a = np.array(( np.poly1d( ... JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    I have a view like this in Hive: id sequencenumber appname 242539622 1 A 242539622 2 A 242539622 3 ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 16, 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
    Based on solute - solvent interactions, arrange the following in order of increasing solubility in n- octane and ... CHM3OH,CHM3CN . Select the correct answer from above options...
asked Jan 5, 2022 in Education by JackTerrance
...