in Education by
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 Leeds 8.9 6.65 12.97 2.53 3 Jim Leeds -1.8 3.83 10.70 -2.27 I need to add a new row at the index 0 and fill it with some dummy values, plus the open price. For that I am trying: df.loc[-1] = [df['Player'].item(), df['Team'].item(), 0.0, 0.0, (df['Price'].item() - df['Value'].item()), 0.0] df.index = df.index +1 # shifting index df = df.sort_index() # sorting by index then resseting In order to end up with: Player Team Points Mean Price Value Gameweek 0 Jim Leeds 0.0 0.0 10.00 0.00 1 Jim Leeds 4.4 4.40 10.44 0.44 2 Jim Leeds 8.9 6.65 12.97 2.53 3 Jim Leeds -1.8 3.83 10.70 -2.27 But I'm getting: df.loc[-1] = [df['Player'].item(), return self.values.item() ValueError: can only convert an array of size 1 to a Python scalar What am I missing? Select the correct answer from above options

1 Answer

0 votes
by
 
Best answer
Change your code to df.loc[0] = [df['Player'].iloc[0], df['Team'].iloc[0], 0.0, 0.0, (df['Price'].iloc[0] - df['Value'].iloc[0]), 0.0] df = df.sort_index() If you are a beginner and want to know more about Python the do check out the Python for Data Science

Related questions

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 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 2 columns in the python dataframe. I want to check each row in my Column A for any value that ... for this particular purpose. Select the correct answer from above options...
asked Jan 9, 2022 in Education by JackTerrance
0 votes
    I have this following datetime64[ns] type timestamps. x1=pd.Timestamp('2018-04-25 00:00:00') x2=pd.Timestamp('2020- ... -04-25 00:53:00 Select the correct answer from above options...
asked Jan 9, 2022 in Education by JackTerrance
0 votes
    In [60]: print(row.index) Int64Index([15], dtype='int64') I already know that the row number is ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 12, 2022 in Education by JackTerrance
0 votes
    I am trying to groupby a column and compute value counts on another column. import pandas as pd dftest = pd. ... Amt, already exists Select the correct answer from above options...
asked Feb 1, 2022 in Education by JackTerrance
0 votes
    I'm starting with input data like this df1 = pandas.DataFrame( { "Name" : ["Alice", "Bob", "Mallory", ... Any hints would be welcome. Select the correct answer from above options...
asked Jan 28, 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 am trying to alter the data in the panda's df. Using below, where X >=5, I want to change the corresponding Y row to 1. Where X...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    I have the below data frame lst = [['A',1],['B',0],['C',1],['D',0],['E',1],['F',1],['G',1]] ... give all val with 1. df[df.val == 1] Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    I have the list like this where items are separated by ":". x=['john:42:engineer', 'michael:29:doctor' ... engineer 1 michael 29 doctor Select the correct answer from above options...
asked Jan 10, 2022 in Education by JackTerrance
0 votes
    I am trying to transpose from df1 to df2 in pandas. Can anyone help me with the codes that transpose data from wide to ... /25/20 4 6 8 Select the correct answer from above options...
asked Jan 9, 2022 in Education by JackTerrance
0 votes
    I have two data frames with the closest matching DateTime index, sometimes matching. An object is to merge two of ... I achieve this? Select the correct answer from above options...
asked Jan 19, 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
...