Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to make possible to add new row to a dataset without changing index

I have the following dataset

       Books'Title                        Authors  Publishing Year  \
1     Il nome della rosa                    Umberto Eco             1980   
2  L'amore che ti meriti                 Daria Bignardi             2014   
3  Memorie dal sottsuolo              Fëdor Dostoevskij             1864   
4                Oblomov  Ivan Alexandrovich Goncharov              1859   

  Publication House  
1          Bompiani  
2         Mondadori  
3           Rizzoli  
4       Feltrinelli 

I have built it as follows:

data = [("Il nome della rosa","Umberto Eco", 1980), 
        ("L'amore che ti meriti","Daria Bignardi", 2014), 
        ("Memorie dal sottsuolo", " Fëdor Dostoevskij", 1864), 
        ("Oblomov", "Ivan Alexandrovich Goncharov ", 1859)]

index = range(1,5,1)
data = pd.DataFrame(data, columns = ["Books'Title", "Authors", "Publishing Year"], index = index)
data

pubhouses = ["Bompiani", "Mondadori", "Rizzoli", "Feltrinelli"]
data.insert(3, 'Publication House', pubhouses)
data 

I am trying adding new rows as follows in the 4th position but without changing the original index of the dataset. By following the suggestions of this website page Insert a row to pandas dataframe

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

new_row = ['Le avventure di Pinocchio', 'Carlo Collodi',  1883, 'Giunti']
new_row 

for i in range(1, 6):
    data.loc[-1] = new_row
    data.index = data.index + 1
    data = data.sort_index()
data

But I am getting the following dataset

enter image description here

May I ask – since I am a beginner how to possibly perform this operation? How would it be possible to exchange the original index of the dataset?

Thanks

>Solution :

Because your index is numeric and loc and iloc are the same here (and only here), you can enlarge your dataframe easily:

data.loc[data.index[-1] + 1] = new_row
print(data)

# Output
                 Books'Title                        Authors  Publishing Year Publication House
1         Il nome della rosa                    Umberto Eco             1980          Bompiani
2      L'amore che ti meriti                 Daria Bignardi             2014         Mondadori
3      Memorie dal sottsuolo              Fëdor Dostoevskij             1864           Rizzoli
4                    Oblomov  Ivan Alexandrovich Goncharov              1859       Feltrinelli
5  Le avventure di Pinocchio                  Carlo Collodi             1883            Giunti

Else you have to build a dataframe and concatenate it:

df = pd.DataFrame([new_row], columns=data.columns)
data = pd.concat([data, df], ignore_index=True)
print(data)

# Output
                 Books'Title                        Authors  Publishing Year Publication House
0         Il nome della rosa                    Umberto Eco             1980          Bompiani
1      L'amore che ti meriti                 Daria Bignardi             2014         Mondadori
2      Memorie dal sottsuolo              Fëdor Dostoevskij             1864           Rizzoli
3                    Oblomov  Ivan Alexandrovich Goncharov              1859       Feltrinelli
4  Le avventure di Pinocchio                  Carlo Collodi             1883            Giunti
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading