I am trying to enter into the following dataset:
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
the rows:
newbook1 = ['Il demone meschino', 'Fëdor Sologub', 1907, 'Feltrinelli']
newbook2 = ['le anime morte', 'Nikolaj Vasilevič Gogol Janovskij', 1882, 'Garzanti']
As you could see, the dataset created with the first chunk of code turns a with the first row marked with index 1.
To maintain it, I used the following code to add two further rows where the new rows are added as follows at the last one of the original dataset:
data.loc[len(data.index[-1] + 1)] = [newbook1, newbook2]
But the code does not work. If possible I would like to ask someone how to deal with this issue and try to fix it. Thanks
Try:
# create temporary dataframe with desired index
idx = data.index.max() + 1
x = pd.DataFrame(
[newbook1, newbook2],
index=range(idx, idx + 2),
columns=data.columns,
)
# concat the dataframes
data = pd.concat([data, x])
print(data)
Prints:
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 Il demone meschino Fëdor Sologub 1907 Feltrinelli
6 le anime morte Nikolaj Vasilevič Gogol Janovskij 1882 Garzanti