[Solved] Consider the following Employee.csv file :

   RSS

1
Topic starter

Consider the following Employee.csv file :

 

empid,Name,Age,City,Sal
100,Ritesh,25,mumbai,15000
101,Akash,26,Goa,16000
,,,,
102,Mahima,27,Hyderabad,20000
103,Lakshay,28,Delhi,18000
104,Manu,25,Mumbai,25000
105,Nidhi,26,Delhi,
106,Geetu,30,Bangaluru,28000

 

1. WAP to display the shape of employee.csv file.

2. WAP to display Name, Age and Salary from employee.csv

3. WAP to display only 5 records from employee.csv.

4. WAP to display records without header.

5. WAP to display employee.csv file without index numbers.

6. WAP to display employee.csv file with new column names.

7. WAP to modify the employee name from Mahima to Harsh.

8. WAP to create a new CSV file by copying the contents of employee.csv.

9. WAP to create a duplicate file for employee.csv containing Empid and Employee Name.

This topic was modified 1 year ago 3 times by CodeWithBishal-admin
1 Answer
1
Topic starter

1) WAP to display the shape of employee.csv file.

Code:

import pandas as pd

data = pd.read_csv("path-of-file.csv")

print("shape of employee.csv file: ")

print(csvData.shape)

Output:

shape of employee.csv file:
(8, 5)

2. WAP to display Name, Age and Salary from employee.csv

Code:

import pandas as pd


data= pd.read_csv("path-of-file.csv",usecols=["Name","Age","Salary"])


print(data)

Output:

  Name Age Salary
0 Ritesh 25.0 15000.0
1 Akash 26.0 16000.0
2 NaN NaN NaN
3 Mahima 27.0 20000.0
4 Lakshay 28.0 18000.0
5 Manu 25.0 25000.0
6 Nidhi 26.0 NaN
7 Geetu 30.0 28000.0

3. WAP to display only 5 records from employee.csv.

Code:

import pandas as pd


data= pd.read_csv("path-of-file.csv")


print(data.head(5))

Output:

  empid Name Age City Salary
0 100.0 Ritesh 25.0 mumbai 15000.0
1 101.0 Akash 26.0 Goa 16000.0
2 NaN NaN NaN NaN NaN
3 102.0 Mahima 27.0 Hyderabad 20000.0
4 103.0 Lakshay 28.0 Delhi 18000.0

4. WAP to display records without header.

Code:

import pandas as pd


data = pd.read_csv("path-of-file.csv", header=None)


print(data)

Output:

  0     1    2   3    4
0 empid Name Age City Salary
1 100 Ritesh 25 mumbai 15000
2 101 Akash 26 Goa 16000
3 NaN NaN NaN NaN NaN
4 102 Mahima 27 Hyderabad 20000
5 103 Lakshay 28 Delhi 18000
6 104 Manu 25 Mumbai 25000
7 105 Nidhi 26 Delhi NaN
8 106 Geetu 30 Bangaluru 28000

5. WAP to display employee.csv file without index numbers.

Code:

import pandas as pd


data = pd.read_csv("path-of-file.csv")


print(data.to_string(index=False))

Output:

empid Name Age City Salary
100.0 Ritesh 25.0 mumbai 15000.0
101.0 Akash 26.0 Goa 16000.0
NaN NaN NaN NaN NaN
102.0 Mahima 27.0 Hyderabad 20000.0
103.0 Lakshay 28.0 Delhi 18000.0
104.0 Manu 25.0 Mumbai 25000.0
105.0 Nidhi 26.0 Delhi NaN
106.0 Geetu 30.0 Bangaluru 28000.0

6. WAP to display employee.csv file with new column names.

import pandas as pd


data = pd.read_csv("path-of-file.csv", names=["a","b","c","d","e"])


print(data)

7. WAP to modify the employee name from Mahima to Harsh.

import pandas as pd

data = pd.read_csv("path-of-file.csv")

df = pd.DataFrame(data)

df["Name"] = df["Name"].replace("Mahima", "Harsh")

print(df)

8. WAP to create a new CSV file by copying the contents of employee.csv.

import pandas as pd

data = pd.read_csv("path-of-file.csv")

df = pd.DataFrame(data)

df.to_csv(""path-of-file-2.csv)

print(df)

9. WAP to create a duplicate file for employee.csv containing Empid and Employee Name.

import pandas as pd

data = pd.read_csv("path-of-file.csv", usecols=["empid","Name"])

df = pd.DataFrame(data)

df.to_csv("path-of-file.csv-2", header=["Empid","Employee Name"])

print(df)
This post was modified 1 year ago 3 times by CodeWithBishal-admin