0
import pandas as pd

data = {'term':[2, 7,10,11,13],'pay':[22,30,50,60,70]}
df = pd.DataFrame(data)
 pay  term
0   22     2
1   30     7
2   50    10
3   60    11
4   70    13

df.loc[2] = [49,9]
print(df)

  pay  term
0   22     2
1   30     7
2   49     9
3   60    11
4   70    13

Expected output :

   pay  term
0   22     2
1   30     7
2   49     9
3   50    10
4   60    11
5   70    13

If we run above code, it is replacing the values at 2 index. I want to add new row with desired value as above to my existing dataframe without replacing the existing values. Please suggest.

mathew
  • 75
  • 7

3 Answers3

2

You could not be able to insert a new row directly by assigning values to df.loc[2] as it will overwrite the existing values. But you can slice the dataframe in two parts and then concat the two parts along with third row to insert.

Try this:

new_df = pd.DataFrame({"pay": 49, "term": 9}, index=[2])
df = pd.concat([df.loc[:1], new_df, df.loc[2:]]).reset_index(drop=True)
print(df)

Output:

   term  pay
0     2   22
1     7   30
2     9   49
3    10   50
4    11   60
5    13   70
Shubham Sharma
  • 68,127
  • 6
  • 24
  • 53
  • 1
    This is an exact replication of [this](https://stackoverflow.com/a/15889056/9758194). I personally feel the whole question is a dup to be honest. OP could quite easily find his answer with a little bit of effort and research. Maybe that's just me? – JvdV Apr 01 '20 at 15:20
  • 1
    @Yes, this question should be marked as duplicate. – Shubham Sharma Apr 01 '20 at 15:21
0

A possible way is to prepare an empty slot in the index, add the row and sort according to the index:

df.index = list(range(2)) + list(range(3, len(df) +1))
df.loc[2] = [49,9]

It gives:

   term  pay
0     2   22
1     7   30
3    10   50
4    11   60
5    13   70
2    49    9

Time to sort it:

df = df.sort_index()

   term  pay
0     2   22
1     7   30
2    49    9
3    10   50
4    11   60
5    13   70
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
0

That is because loc and iloc methods bring the already existing row from the dataframe, what you would normally do is to insert by appending a value in the last row.

To address this situation first you need to split the dataframe, append the value you want, concatenate with the second split and finally reset the index (in case you want to keep using integers)

#location you want to update
i = 2

#data to insert
data_to_insert = pd.DataFrame({'term':49, 'pay':9}, index = [i])

#split, append data to insert, append the rest of the original
df = df.loc[:i].append(data_to_insert).append(df.loc[i:]).reset_index(drop=True)

Keep in mind that the slice operator will work because the index is integers.

jcaliz
  • 3,891
  • 2
  • 9
  • 13