0% found this document useful (0 votes)
60 views18 pages

KV No-3 Bhopal Shift-2 - IP - ImportExport Between CSV and Dataframe

Uploaded by

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

KV No-3 Bhopal Shift-2 - IP - ImportExport Between CSV and Dataframe

Uploaded by

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

IMPORTING / EXPORTING DATA BETWEEN DATAFRAME AND

CSV FILE

15 QUESTIONS ON MLL

1. The data of any CSV file can be shown in which of the following software?
(a) MS Word (b) Notepad (c) Spreadsheet (d) All of the above

2. Tabular data that saved as plain text where data values are separated by commas

a. Dataframe b. CSV c. MySQL d. All of the above

3. Look at this image and identify the file type

a. MySQL
b. Dataframe
c. CSV
d. Excel

4. Select the INCORRECT statement

a. CSV files occupies less memory space


b. CSV files are easy to read and write manually
c. CSV files are compatible only with spreadsheet
d. Data value of CSV files can be separated by any delimiter like
comma/semicolon/space/tab.

5. All individual rows of CSV files are called

a. Tuple b. Data c. Record d. Cardinality

6. CSV files are text files

a. True
b. False
7. CSV file can also be considered as Binary files

a. True b. False c. Sometimes d. Not sure

8. Look at the anatomy of CSV file and select correct abbreviation for 1 and 2

a. Attributes, 2. Rows
b. Columns, 2. Data
c. Degree, 2. Record
d. Header, 2. Record

9. Which of the following is not relavent to CSV files?

a. Smaller in size
b. Human readable
c. Easy to parse
d. Test and numeric data are distinct

10. Default delimeter in CSV is

a. ;
b. :
c. I
d. ,
11. A CSV file can take __________ as delimeter

a. ;
b. |
c. \t
d. @
e. All of the above
12. To read data from CSV file in pandas dataframe _______ method is used.

a. read_csv() b. to_csv() c. reader() d. writer()

13. ________ method saves data of dataframe to CSV file.

a. read_csv()
b. to_csv()
c. reader()
d. writer()

14. Rama want to make a dataframe ‘saledata’ based on data of sale.csv which is
stored in folder ‘Python’ in C: drive. What code she should write for it?

a. Saledata = pd.read_csv(“sale.csv”)|
b. Saledata = pd.read_csv(“C:/Python/sale.csv”)
c. Saledata = pd.to_csv(“C:/Python/sale.csv”)
d. All of the above

15. If you want to read top 20 rows of data from CSV file, which argument would
you give to read_csv()?

a. Rows
b. Nrows
c. Header
d. Head

15 QUESTIONS ASSERTION AND REASONONG

ASSERTION AND REASONING based questions. Mark the correct choice as


i. Both A and R are true and R is the correct explanation for A
ii. Both A and R are true and R is not the correct explanation for A
iii. A is True but R is False
iv. A is false but R is True
1. Assertion (A): - The acronym CSV is a short form for Comma Separated Values,
which refers to a tabular data saved as plain text where data values are separated
by commas. Data can be imported to a Dataframe from csv file.
Reasoning (R):- If we have the data in a CSV file, we can import the data. But,
Python’s Pandas library must be imported to the program.

2. Assertion(A) : Only database can support import/export to CSV format.


Reasoning (R) : CSV file can be created and edited using any text editor

3. Assertion(A) : All the columns of CSV file can be separated by comma ‘ , ‘ or


other delimiter.
Reasoning (R) : CSV is a short form for Comma Separated Values, which refers
to a tabular data saved as plain text where data values are separated by commas
only.

4. Assertion(A) Python Panda library offers functionality to interact with a CSV


file.

Reasoning (R) : Panda's read_csv() and to_csv() functions can read-

from and write-to CSV files.

5. Assertion. The read_csv() function of Python Pandas can read data of a csv file
into any of pandas data structures.

Reasoning(R). DataFrame is a compatible data structure for

read_csv() function.

6. Assertion(A) The read_csv() function reads a csv file's data into a DataFrame.

Reasoning( R) : The to_csv() function writes a DataFrame on to a csv

file.

7. Assertion(A) : By default, read_csv() uses the values of first row as column


headers in DataFrames.

Reasoningg( R) : header argument is given to ensure that the top/first row's data
is used as data and not as column headers.

8. Assertion(A) : The nrows argument can be used to read only the top 10 rows of
data from a CSV file using the read_csv() function in pandas. Reasoning( R) :
The nrows argument specifies the number of rows of the file to read.
9. Assertion(A) : CSV is a short form for Comma Separated Values files , occupies
less memory space .
Reasoning( R) : CSV files which refers to a tabular data saved as plain text.

10.Assertion (A): CSV (Comma Separated Values) is a file format for data

storage which looks like a text file.

Reason (R): The information is organized with one record on each line

and each field is separated by comma.

11.Assertion(A) : We can read specific rows from the csv files.


Reasoning( R) : The nrows attribute is used to read specific rows from the CSV
files.
12.Assertion(A) : Pandas is a library of python.
Reasoning( R) : We import pandas and import and export data between dataframe
and csv files.
13.Assertion (A):- CSV (Comma Separated Values) is a file format for data

storage which looks like a text file.


Reasoning (R):- A csv file is a two-dimensional labelled data structure like a
table of MySQL.
14.Assertion(A) : There are two functions we would use to read data and write data
to and from a CSV file into a DataFrame.
Reasoning( R) : The function used to read data from a CSV file into a
DataFrame is read_csv().The function used to fetch data from an SQL table into
a DataFrame is read_sql().
15.Assertion(A) : CSV stands for comma separated value file.

Reasoning( R) : A CSV file can not take other delimiter.

20 MCQ

1. A CSV file can take character as separator.


(a), (b) - (c) I (d) \t (e) only (a) (f) all of these

2. The correct statement to read from a CSV file in a dataframeis :


(a) <DF>.read_csv(<file>)
(b) <File>. read_csv( )(<DF>)
(c) <DF> = pandas.read(<file>)
(d) <DF> = pandas.read_csv(<files>)

3. Which argument do you specify with read_csv( ) to specify a separator character ?


(a) character
(b) char
(c) separator
(d) sep

4. To suppress first row as header, which of the following arguments is to be given in


read_csv( ) ?
(a) noheader = True
(b) header = None
(c) skipheader = True
(d) header - Null

5. To read specific number of rows from a CSV file, which argument is to be given in
read_csv( ) ?
(a) rows = <n>
(b) nrows = <n>
(c) n_rows - <n>
(d) number_rows = <n>

6.To skip first 5 rows of CSV file, which argument will you give in read_csv( ) ?
(a) skip_rows = 5
(b) skiprows = 5
(c) skip - 5
(d) noread - 5

7. To skip 1st, 3rd and 5th rows of CSV file, which argument will you give in read_csv(
)?
(a) skiprows = 11315
(b) skiprows - (1, 3, 5]
(c) skiprows = [1, 5, 1]
(d) Any of these
8. While reading from a CSV file, to use a column's values as index labels, argument
given in read_CSV( ) is :
(a) index
(b) index_col
(c) index_values
(d) index_label

9. Nowadays for developing Machine learning projects programmers rely on


CSV files rather than databases. Why?
(a) csv can be used with proprietary softwares only.
(b) cSv files can be downloaded from open source websites free of cost.
(c) csv files need not be imported while creating the projects
(d) csv is a simple and well formatted mode for data storage

10. Which of the following is not a parameter (argument) of read_csv() method?


a. sep b. names c. header d. sort

11. Raju want to make a dataframe ‘saledata’ based on data of sale.csv which is stored
in folder ‘Python’ in C: drive. What code he should write for it?

a. Saledata = pd.read_csv(“sale.csv”)|
b. Saledata = pd.read_csv(“C:/Python/sale.csv”)
c. Saledata = pd.to_csv(“C:/Python/sale.csv”)
d. All of the above

12. (i)_______ attribute used with read_csv() to import selective records/rows in


dataframe
(ii)________ attribute used with read_csv() to specify the number of row whose
values are to be used as column names.

a. (i) nrows, (ii) names


b. (i) rows, (ii) pos
c. (i) nrows, (ii) header
d. (i) head, (ii) nrows
13.Attribute used to specify the separator character for values being imported in
dataframe using read_csv()

a. Sepatator
b. Sep
c. Sepies
d. Space
14.Write code to read data from CSV file student.csv stored in C: in dataframe ‘std’
including exculsive columns rollno, name, percent where all values are separated
by semicolon ‘:’.

a. Std = pd.read_csv(“C:\student.csv”, sep = ‘;’, names =


[‘rollno’,’name’,’percent’])
b. Std = pd.read_csv(“C:\student.csv”, sepies = ‘;’, names =
(‘rollno’,’name’,’percent’))
c. Std = pd.read_csv(“C:\student.csv”, sep = ‘;’, header =
[‘rollno’,’name’,’percent’])
d. Std = pd.read_csv(“C:\student.csv”, sepies = ‘;’, columns =
[‘rollno’,’name’,’percent’])
15.Which of the following attribute should be taken to import first 10 rows in a
dataframe from CSV?

a. Nrows = 10
b. Rows = 10
c. Skiprows = 10
d. Head = 10

16.You need to import CSV package in order to store a DataFrame in a CSV file.
a) True b) False

17.Write command to store data of DataFrame mdf into a CSV file Mydata.csv,
with separator character as '@'………………………………………

18.A CSV file is also known as a ______.


a) Flat File b) 3D File c) String File d) Random File

19.The data of any CSV file can be shown in which of the following software?
(a) MS Word (b) Notepad (c) Spreadsheet (d) All of the above
20.Nowadays for developing Machine learning projects programmers rely on
CSV files rather than databases. Why?
(a) csv can be used with proprietary softwares only.
(b) cSv files can be downloaded from open source websites free of cost.
(c) csv files need not be imported while creating the projects
(d) csv is a simple and well formatted mode for data storage

STUDY MATERIAL

CSV FILES CSV (Comma-separated values) files are the comma separated values. This
type of file can be view as an excel file and separated by commas. CSV file is nothing
more than a simple text file. However, it is the most common, simple and easiest
method to store tabular data. This particular format arranges tables by a specific
structure divided into rows and columns.

Once we have the DataFrame, we can persist it in CSV on the local disk.

Let’s first create CSV file using data that is currently present in the DataFrame, we can
store the data of this DataFrame in CSV format using API called
to_CSV (…) of Pandas.

Importing/Exporting Data between CSV files and DataFrames

Pandas read_csv() function is used to import a CSV file to DataFrame format.

Syntax: df.read_csv('file_name.CSV', header=None)

Here,
header allows you to specify which row will be used as column names for your
DataFrame. Expected int value or a list of int values.

If your file does not have a header, then simply set header=None.

To export a Pandas DataFrame to a CSV file, use to_csv function. This saves a
DataFrame as a CSV file.
Syntax: to_csv(parameters)

1. Write python code to write DataFrame data into “a.csv” file.


Ans.
import pandas as pd
Dic={ ‘empno’: (101,102,103,104), ’name’:(‘a’,’b’,’c’,’d’),
’salary’: (3000,5000,8000,9000)}
df=pd.DataFrame(Dic)
df.to_csv(“a.csv”)
Note: csv can be opened in excel, notepad, etc.

2. Modify the above code and write the data in d:\software folder.
Ans.
import pandas as pd
Dic={‘empno’:(101,102,103,104),’name’:(‘a’,’b’,’c’,’d’),
’salary’:(3000,5000,8000,9000)}
df=pd.DataFrame(Dic)
df.to_csv(r”D:\software\a.csv”) # or df.to_csv(“D:\\software\\a.csv”)

Read CSV File as Pandas Using the read_csv() function from the pandas
package, you can import tabular data from CSV files into pandas DataFrame:

3. Write python code to read a csv file “test.csv” from D:\


Ans.
import pandas as pd
df = pd.read_csv(‘d:\\test.csv’) #read the csv file
print(df)

How to Create CSV File in Excel


Step 1: Open excel and write data in worksheet
Step 2: Select file option from menu and click on save as ...
Step 3: Select drive and folder name where you want to save csv file
Step 4: Click on save as type option and select csv option from list.
Step 5: Click on save button

Other attributes used with csv file:


Here is the Pandas read CSV syntax with its parameters.
Syntax: pd.read_csv(filepath , sep=’ ,’ , header=’infer’, index_col=None,
usecols=None, skiprows=None, nrows=None)

header:
By default, read_csv() uses the values of first row as column headers in dataframes.
Header argument is used to ensure that the top/first row’s data is used as data and not as
column headers
df = pd.read_csv( “File Path” , header = None )
nrows:
To read top n rows of data ‘nrows’ argument is used.
df = pd.read_csv( “File Path” , header = None , nrows = n)

sep:
It stands for separator, default is ‘, ‘.
df = pd.read_csv('sample.csv', sep=':’)

skiprows: Skips passed rows in the new data frame.


df = pd.read_csv("people.csv", skiprows = [1,5])
names: We can exclusively specify column names using the parameter names while
creating the DataFrame using the read_csv() function.

import pandas as pd
m = pd.read_csv("data.csv", sep=",", header=0, names=['Rno', 'S_Name', 'S_Class',
'Section'])
print(m)

usecols
Retrieves only selected columns from the CSV file.
df = pd.read_csv('people.csv',
header=0,
usecols=["First Name", "Sex", "Email"])

index_col
This is to allow you to set which columns to be used as the index of the dataframe. If
None, there are no index numbers displayed along with records.
df = pd.read_csv('people.csv',
header=0, index_col=["Id", "Job Title"],
usecols=["Id", "Job Title", "Email"], nrows=3)

10 QUESTIONS CBT
1. To create dataframe T from following csv file temp.csv stored in c drive, which of
the following code is correct?

a. T = pd.read_csv(“c:\temp.csv”)
b. T = pd.read_csv(“c:\temp.csv”,sep = “ “)
c. T = pd.read_csv(“c:\temp.csv”, sep = “\n”)
d. T = pd.read_csv(“c:\temp.csv”, sep = “\t”)

2. Select appropriate code to export all data of dataframe df to csv file temp.csv located
in c drive excluding row labels.

a. df.to_csv(“C:\temp.csv”)
b. df.to_csv(“C:\temp.csv”, index = False)
c. df.to_csv(“C:\temp.csv”, header = False)
d. None of the above

3. In order to work with CSV files from panda, you need to import ............... , other
than pandas.

1. .csv
2. pandas.io
3. newcsv
4. no extra package required

4. The correct statement to read from a CSV file in a DataFrame is :

1. <DF>.read_csv(<file>)
2. <File>. read_csv( )(<DF>)
3. <DF> = pandas.read(<file>)
4. <DF> = pandas.read_csv(<files>)

5. To suppress first row as header, which of the following arguments is to be given in


read_csv() ?

1. noheader = True
2. header = None
3. skipheader = True
4. header = Null

6. Identify the function which can save dataframe df into csv file.
(i) df.write_csv() (ii) df.store_csv() (iii) df.to_csv() (iv) df.create_csv()

7. To skip first 5 rows of CSV file, which argument will you give in read_csv() ?

1. skiprows = 5
2. skip_rows = 5
3. skip = 5
4. noread = 5

8. To skip 1st, 3rd and 5th row of CSV file, which argument will you give in
read_csv() ?
1. skiprows = 1 | 13 | 5
2. skiprows = [1, 5, 1]
3. skiprows = [1, 3, 5]
4. any of these

9. The data of CSV files can be shown in which of the following software?
a Spreadsheet software only

b Spreadsheet Software and Text Editor

c Text Editors only

d Any software

10. Which of the following advantage motivates to use CSV files over database
transfer?

a CSV a common format for data interchange

b Nearly all spreadsheets and databases support import/export to CSV format

c No need to learn or remember any database command or any other tools

d A simple, common, and ubiquitous for data storage

15 CASE STUDY BASED

1. Write a program to read all content of “student.csv” in a dataframe and display


records of only those students who scored more than 80 marks. Records stored in
students is in format : Rollno, Name, Marks

2. For given sale.CSV located in C:\invetory

Shivangi want to create dataframe ‘Sales’ as given below


Help her to write correct code to do so.

3. Write a program to read from CSV file “c:\python\data.csv” where the delimeter is @
with following conditions
(i) Give exclusive column names as col1, col2, col3
(ii) First row should be taken as data and not as column headers

4.For given dataframe book


Bcode Bname Author Price

0 B1 C++ Ravichanran 200

1 B2 C Kanitkar 180

2 B3 ETC Kennedy 230

Write a program to create csv file lib.csv with columns bname and price.

5. Ms Priya is working on an application made in python. She wrote the command tp


customize the column header but getting an error . Provide her the solution for the same
and also give the explanation.

Df =
pd.read_Csv(“adm.csv”,columns=[“AdmNo”,”FirstNAme”,”LAstNAme”,”Class”])

6.Mr Prakash has created a csv file to store the students details with header rows. While
reading data from csv file into the dataframe he wants to hide the header from the csv
file. He has written the following code but getting an error.:

Df= pd.read_csv(“d://adm.csv”,header=”no”)

Provide him the solution for the same and also give the explanation.
7. Ms Smita wants to write the following data in similar pattern in csv file (separated
each by * and ignore default index). Help her to do the same.

8. Write a program to read data from a CSV file where separator character is '@'.
Make sure that :

 the top row is used as data, not as column headers.


 only 10 rows are read into DataFrame.

Let the contents of the file bike.csv be the following:

Let the contents of the file bike.csv be the following:

Honda@2500

Yamaha@2800

Suzuki@2100

Kawasaki@3200

Ducati@3500

BMW@4500

Harley-Davidson@5500

KTM@4000

Triumph@5300

Aprilia@4800

Indian@5700
Royal Enfield@3000
9. Ms. Payal wants to create a CSV file from another CSV file. The file contains 5
columns EmpName, EmpId, Salary, Designation, DOB. Payal wants to read first three
rows from the file. She has found the code to do the task she wants , but one line of
code is missing. Help her to complete the code.

import pandas as pd
df = pd.read_csv(“E:\\Data\\Employee.csv”)
df.to_csv(“E:\\Data\\Emp.csv”, ___________________

10. Write a program that reads from a CSV files where the separator character is
‘#’.Read only first 5 rows in your dataframe.
i. Give column headings as EmpName, Designation, Salary.
ii. Make sure to read first row as data and not as column headers.

11. Write a program to show the detail of the student who scored the highest marks.
Rama stored the students details in “Data.csv” in given below :
Rollno, Name, Marks
1, Aman, 35
2, Kanak, 1
3, Anuj, 33
4, suman, 25
Help her to show the detail of the student who scored the highest marks by importing
this data in a dataframe.

12. Amit, a student of class 12th is trying to write a program to search the record from
“data.csv” according to the admission number input from the user. Structure of record
saved in “data.csv” is Adm_no, Name, Class, Section, Marks.

13. Rohit, a student of class 12th, is learning CSV File Module in Python. Help him to
show the detail of the student who are class XI by importing this data in a dataframe.

CSV File

1,AKSHAY,XII,A
2,ABHISHEK,XI,A
3,ARVIND,XII,A
4,RAVI,XII,A
5,ASHISH,XI,A
14. Write a program to read data from a CSV file where separator character
is ';'. Make sure that :
• the top row is used as data, not as column headers.
• only 5 rows are read into DataFrame.
Let the contents of the file std.csv be the following:

1 , AKSHAY , XII , A
2 , ABHISHEK , XII , A
3 , ARVIND , XII , A
4 , RAVI , XII , A
5, ASHISH , XII , A

15.Consider the following dataframe :

Write the code to export this dataframe into the csv file Marks.csv.

You might also like