KV No-3 Bhopal Shift-2 - IP - ImportExport Between CSV and Dataframe
KV No-3 Bhopal Shift-2 - IP - ImportExport Between CSV and Dataframe
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. MySQL
b. Dataframe
c. CSV
d. Excel
a. True
b. False
7. CSV file can also be considered as Binary files
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
a. Smaller in size
b. Human readable
c. Easy to parse
d. Test and numeric data are distinct
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()
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
5. Assertion. The read_csv() function of Python Pandas can read data of a csv file
into any of pandas data structures.
read_csv() function.
6. Assertion(A) The read_csv() function reads a csv file's data into a DataFrame.
file.
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
Reason (R): The information is organized with one record on each line
20 MCQ
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
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
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. 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 '@'………………………………………
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.
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)
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:
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=':’)
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
1. <DF>.read_csv(<file>)
2. <File>. read_csv( )(<DF>)
3. <DF> = pandas.read(<file>)
4. <DF> = pandas.read_csv(<files>)
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
d Any software
10. Which of the following advantage motivates to use CSV files over database
transfer?
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
1 B2 C Kanitkar 180
Write a program to create csv file lib.csv with columns bname and price.
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 :
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
Write the code to export this dataframe into the csv file Marks.csv.