Creating a CSV file using Excel:
1.Launch Microsoft Excel
Emp id name age city salary
101 Akash 25 Mumbai 25000
102 Ritesh 27 Goa 32000
103 Mahima 29 Hyderabad 43000
104 Lakshay 33 Mumbai
105 Nidhi 26 Delhi 33000
2. Type the data given in the above Table in the Excel sheet You will also notice that some values
are missing in the table representing NaN .
3. Save the file as Emp.csv (.csv is very important)
4.To view this CSV file, open any Text Editor (Notepad preferably) and explore the folder in which
you have stored this file.
Reading from a CSV file into a dataframe
import pandas as pd
df=pd.read_csv("C:\\Users\\Admin\\Desktop\\product.csv")
print(df)
Reading selected columns from the csv file
import pandas as pd
df=pd.read_csv("D:\\vidya\\2020-21\\csvdem.csv" ,usecols=['name','salary'])
print(df)
Reading selected rows from the csv file Print the nrows excluding the header
import pandas as pd
df=pd.read_csv("D:\\vidya\\2020-21\\csvdem.csv" ,nrows=3)
print(df)
Read csv file without the header
import pandas as pd
df=pd.read_csv("D:\\vidya\\2020-21\\csvdem.csv" ,header=None)
print(df)
0 1 2 3 4
0 empid name age city salary
1 101 Akash 25 Mumbai 25000
2 102 Ritesh 27 Goa 32000
3 103 Mahima 29 Hyderabad 43000
4 104 Lakshay 33 Mumbai NaN
5 105 Nidhi 26 Delhi 33000
import pandas as pd
df=pd.read_csv("D:\\vidya\\2020-21\\csvdem.csv" ,header=None, skiprows=1)
print(df)
Reading a csv file with out index
import pandas as pd
df=pd.read_csv("D:\\vidya\\2020-21\\csvdem.csv" ,index_col=0)
print(df)
Reading a csv file with the names other than the default:
import pandas as pd
df=pd.read_csv("D:\\vidya\\2020-21\\csvdem.csv" ,header=None,skiprows=1,
names=['eid','ename','eage','ecity','esal'])
print(df)
--------------------------------------------------------------------------------------------------------------------------------- -
Creating a dataframe into a CSV file:
import pandas as pd
student={'rollno':[12,13,14,15,16],
'sname':['vid','sid','rev','mal','gal'],
'marks':[345,432,450,432,376]}
df=pd.DataFrame(student)
print(df)
df.to_csv('d:\\vidya\\2020-21\\stu.csv')
Add/Update/delete
For all these operations first you have to read from the csv file to a dataframe make the changes and
rewrite to the csv file
Addition:
Add a new row or rows to the dataframe
import pandas as pd
stu={'rollno':[20],'sname':['lap'],'marks':[42]}
df=pd.read_csv('d:\\vidya\\2020-21\\stu.csv')
print(df)
df1=pd.DataFrame(stu)
df=df.append(df1)
#print(df)
df.to_csv('d:\\vidya\\2020-21\\stu.csv',index=False)
df=pd.read_csv('d:\\vidya\\2020-21\\stu.csv',index_col=0)
print(df)
Updating a csv file:
import pandas as pd
df=pd.read_csv('d:\\vidya\\2020-21\\stu.csv')
print(df)
df.replace('vid','vi',inplace=True)
df.to_csv('d:\\vidya\\2020-21\\stu.csv',index=False)
df=pd.read_csv('d:\\vidya\\2020-21\\stu.csv',index_col=0)
print(df)
Deleting in a csv file:
import pandas as pd
stu={'rollno':[20],'name':['lap'],'marks':[42]}
df=pd.read_csv('d:\\vidya\\2020-21\\stu.csv')
print(df)
for rindex,rvalue in df.iterrows():
if (rvalue['marks']==42):
pos=rindex
df=df.drop(pos)
df.to_csv('d:\\vidya\\2020-21\\stu.csv',index=False)
df=pd.read_csv('d:\\vidya\\2020-21\\stu.csv',index_col=0)
print(df)
Install pip install sql_connector for the database connectivity programs