Here's is my Python code:
import pandas as pd
data = [{'Countries':[],2016:[],2017:[],2018:[],2019:[]}]
df = pd.DataFrame(data)
opt='y'
while(opt=='y'):
print("1. Add a new record to the end of the file")
print("2. Display the selected records")
print("3. Delete a Student record")
print("4. Update a Student record")
print("5. Reports of Students")
ch=int(input("Enter your choice"))
if ch==1:
df=pd.read_csv("info.csv")
i=len(df)
cntry=input("Enter country name")
i=int(input("Enter international tourism arrival rate in 2016"))
ii=int(input("Enter international tourism arrival rate in 2017"))
iii=int(input("Enter international tourism arrival rate in 2018"))
iv=int(input("Enter international tourism arrival rate in 2019"))
df.loc[cntry]=[i,ii,iii,iv]
df.to_csv("info.csv")
print("A new record is successfully inserted\n\n")
elif ch==2:
df=pd.read_csv("info.csv")
print(df)
opt=input("want to continue")
My CSV file:
Country,2016,2017,2018,2019
France,82.5,86.4,87.1,89.4
US,80.3,81.9,82.2,82.8
China,74.2,76.9,77.6,79.6
Italy,59.5,60.7,61.5,62.9
Turkey,55.6,58.3,60.1,62.1
Germany ,35.3,37.6,40.5,45.8
Thailand,37.5,37.5,38.2,38.9
UK,34.2,35.6,36.4,38.2
Japan,36.7,37.7,35.4,36.3
Greece,25.6,28.7,30.5,31.2
Malaysia,26.3,27.2,29.6,30.1
Russia Fed,25.6,27.9,30.3,29.3
Canada,24.8,25.9,26.7,25.8
Poland,23.4,24.4,25.2,24.6
India,20.1,21.2,22.3,22.6
I was trying to add more entries to the file, it took in all the data required but it showed this error:
Traceback (most recent call last):
File "C:\Users\Dell\OneDrive\Desktop\agj.py", line 22, in <module>
df.loc[cntry]=[i,ii,iii,iv]
File "C:\Users\Dell\anaconda3\lib\site-packages\pandas\core\indexing.py", line 670, in __setitem__
self._setitem_with_indexer(indexer, value)
File "C:\Users\Dell\anaconda3\lib\site-packages\pandas\core\indexing.py", line 874, in _setitem_with_indexer
return self._setitem_with_indexer_missing(indexer, value)
File "C:\Users\Dell\anaconda3\lib\site-packages\pandas\core\indexing.py", line 1118, in _setitem_with_indexer_missing
raise ValueError("cannot set a row with mismatched columns")
ValueError: cannot set a row with mismatched columns