0% found this document useful (0 votes)
41 views3 pages

Assign 1

The document discusses reading and writing CSV and text files using Pandas in Python. It creates CSV files with student data, reads the files into DataFrames, and prints, selects, and writes subsets of the data to new files. CSV files with 10 rows of student roll numbers, names and marks are created. DataFrames are used to read the CSV files, print and select the first few and last few rows, and write the subsets to new CSV and text files.

Uploaded by

Prachi Aggarwal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views3 pages

Assign 1

The document discusses reading and writing CSV and text files using Pandas in Python. It creates CSV files with student data, reads the files into DataFrames, and prints, selects, and writes subsets of the data to new files. CSV files with 10 rows of student roll numbers, names and marks are created. DataFrames are used to read the CSV files, print and select the first few and last few rows, and write the subsets to new CSV and text files.

Uploaded by

Prachi Aggarwal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

# Q1.Create a csv file abc.csv with ten rows(Roll no , name , marks).

# Read csv file and assign to data frame df.

import pandas as pd
df=pd.read_csv("./files/abc.csv")
df

Rollno Name Marks


0 1 Prachi 98
1 2 Paridhi 99
2 3 Aadya 56
3 4 Priya 90
4 5 Sehaj 50
5 6 Dhairya 80
6 7 Sneha 88
7 8 Jhanvi 78
8 9 Pragati 86
9 10 Tanya 70

# Print first 6 observation and save to data frame df1.

df1 = df[:6]

print(df1)

Rollno Name Marks


0 1 Prachi 98
1 2 Paridhi 99
2 3 Aadya 56
3 4 Priya 90
4 5 Sehaj 50
5 6 Dhairya 80

# Write df1 to csv and colon separated file.

df1.to_csv("./files/df1.csv")

df1.to_csv("./files/df11.csv",sep=":")

df2=pd.read_csv("./files/df1.csv")

df2

Unnamed: 0 Rollno Name Marks


0 0 1 Prachi 98
1 1 2 Paridhi 99
2 2 3 Aadya 56
3 3 4 Priya 90
4 4 5 Sehaj 50
5 5 6 Dhairya 80

# Q2. Create a data frame df3 Reading only three rows from abc.csv
df3=pd.read_table("./files/abc.csv",sep=',',nrows=3)

df3

Rollno Name Marks


0 1 Prachi 98
1 2 Paridhi 99
2 3 Aadya 56

# Create a txt file abc.txt with ten rows(Roll no , name , marks).


Read abc.txt file and assign to df9.

df9=pd.read_csv("./files/abc.txt")

df9

Rollno Name Marks


0 1 Prachi 98
1 2 Paridhi 99
2 3 Aadya 56
3 4 Priya 90
4 5 Sehaj 50
5 6 Dhairya 80
6 7 Sneha 88
7 8 Jhanvi 78
8 9 Pragati 86
9 10 Tanya 70

# Print first 5 observation and save to data frame df10

df10=pd.read_table("./files/abc.txt",sep=',',nrows=5)

df10

Rollno Name Marks


0 1 Prachi 98
1 2 Paridhi 99
2 3 Aadya 56
3 4 Priya 90
4 5 Sehaj 50

# Print last 4 observation and save to data frame df11.

df11=pd.read_table("./files/abc.txt",sep=',',skiprows=[1,2,3,4,5,6])

df11

Rollno Name Marks


0 7 Sneha 88
1 8 Jhanvi 78
2 9 Pragati 86
3 10 Tanya 70
# Write df10 to a csv file.

df10.to_csv("./files/df10.csv",sep='|')

df10

Rollno Name Marks


0 1 Prachi 98
1 2 Paridhi 99
2 3 Aadya 56
3 4 Priya 90
4 5 Sehaj 50

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy