in Education by
I have a set of oil wells compiled in the panda's data frame. It looks like this: wells = pd.DataFrame({'date':['2019-01-01', '2019-01-01','2019-01-01', '2019-01-01','2019-01-01', '2019-01-01'], 'well': ['FIELDX-1D', 'FIELDX-2D', 'FIELDY-1C', 'FIELDY-1B', 'FIELDZ-1A', 'FIELDZ-1J'], 'oil_mmbd':[3.1, 5.6, 9.8, 2.5, 4.7, 6.1]}) date well oil_mmbd 0 2019-01-01 FIELDX-1D 3.1 1 2019-01-01 FIELDX-2D 5.6 2 2019-01-01 FIELDY-1C 9.8 3 2019-01-01 FIELDY-1B 2.5 4 2019-01-01 FIELDZ-1A 4.7 5 2019-01-01 FIELDZ-1J 6.1 What I am trying to achieve is to create a new variable (ie 'Field') that compiles the aggregate output of each field. For this, I need to get rid of the last part of each well's name; but I am not able to find the solution for this using Python and Pandas. I want to get to the table that looks like this: wells_agg = pd.DataFrame({'date':['2019-01-01', '2019-01-01','2019-01-01'], 'field': ['FIELDX', 'FIELDY', 'FIELDZ'], 'oil_mmbd':[8.7, 12.3, 10.8]}) date field oil_mmbd 0 2019-01-01 FIELDX 8.7 1 2019-01-01 FIELDY 12.3 2 2019-01-01 FIELDZ 10.8 Select the correct answer from above options

1 Answer

0 votes
by
 
Best answer
You can strip the suffix of a well column using wells.well.str.split('-').str[0]). Therefore instead of wells.groupby('well'), use wells.groupby(wells.well.str.split('-').str[0])). See solution below with this in mind. wells.groupby(['date',wells.well.str.split('-')\ .str[0]]).oil_mmbd.sum().reset_index() date well oil_mmbd 0 2019-01-01 FIELDX 8.7 1 2019-01-01 FIELDY 12.3 2 2019-01-01 FIELDZ 10.8 If you want to know more about the Data Science then do check out the following Data Science which will help you in understanding Data Science from scratch

Related questions

0 votes
    This is my function: def sub(number): tmp = [] tmp.append(number) while len(str(number)) > 1: tmp.append( ... do this in the python? 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 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 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 am trying to create a new column where in, True if last n rows are True in other column. It works fine ... better way to achieve it? Select the correct answer from above options...
asked Jan 8, 2022 in Education by JackTerrance
0 votes
    The code works fine but the only problem I encountered an error is: bad operand type for unary +: 'str'. This is ... for unary +: 'str' Select the correct answer from above options...
asked Jan 11, 2022 in Education by JackTerrance
0 votes
    I have a List : Old_list =['orders_ce_deduped.01.csv','orders_ce_deduped.02.csv'] I need to get the 01 and 02 part ... = ['01','02'] Select the correct answer from above options...
asked Jan 19, 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 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 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
    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
    I have the pandas data frame with a column designated to town names. After each town name, I am adding a word " ... .csv', index=False) Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    I have the pandas data frame with the column designated to town names. After each town name, I am adding a word ... .csv', index=False) Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    I have the 4D array, which is defined as follows: B = np.array( [[[[0.5000, 0.5625], [0.5000, ... loops and ideally one function call. Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
...