Assignment – data transfer between csv files and dataframe

Assignment – data transfer between CSV files and data frame article provides you questions and answers for class XII 12 Informatics Practices. Here we go!

Type A: Objective Type Questions

MCQs

1 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

2 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

3 Which of the following function is used to transfer data from the data frame to CSV?

a read_csv()

b readCSV()

c to_csv()

d toCSV()

4 Which one of the following is the correct syntax to transfer data from the data frame to the CSV file?

a df.to_csv(‘filename’,options)

b df.tocsv(‘filename’,options)

c df.to_CSV(‘filename’,options)

d df.TO_CSV(‘filename’,options)

5 You can change the separator by using which of the following parameter in to_csv() function?

a separator

b spearator_characater

c sep_char

d sep

6 Which of the following parameter of to_csv() function specify the value of NaN?

a na_rep

b NaN_value

c NaN_rep

d NaN

7 Which of the following parameter of to_csv() function reduces the length of decimal values in fractional numbers?

a decimal

b float_format

c decimal_value

d float

8 Which of the following parameter accepts a boolean value to export data without headers?

a header

b headerrow

c Header_Row

d HEADER_ROW

9 Which of the following parameter export selected columns from the data frame to CSV?

a cols

b columns

c df.cols

d All of the above

10 What is the use of index_col parameter in read_csv() function?

a it assigns a new index to the specified column

b it will change the default index of data frame

c specifies the row index

d All of the above

Fill in the blanks

1 The ________ format refers to a tabular data that saved as plain text where data is separated by commas.

Answer: CSV

2 A _________ parameter receives CSV filename.

Answer: path_or_buf

3 The default value of na_rep parameter is _______ in to_csv() function.

Answer: ” (Two single quotes or None)

4 The default value for header parameter is _______ in to_csv() function.

Answer: True

5 The default value for columns parameter is ___________ in to_csv() function.

Answer: None

6 A ___________ parameter of read_csv() function receives a sequence or list of columns to assign columns to the dataframe.

Answer: names

7 A _______ parameter ignores the header and assign by default indexes to the dataframe from read_csv() function.

Answer: header = None

8 A ______ parameter of read_csv() ignore the header row from csv file.

Answer: skiprows = 1

Type B Short answer questions

For answers consider this article: import/export data between csv/dataframe

1 What is CSV format?

Ans.: The CSV format stores data as plain text values separated by comma. The data saved in CSV format are in tabular form. The tabular form refers to data stored in row and columns format.

2 What are the characteristics of CSV format?

Ans.:

  1. Data is stored in a row, one record equally considered as one row
  2. Each value in CSV is separated by comma
  3. To store a new row comma is not required at last but it is a new line character at the end of line

3 What are the advantages of CSV format?

1 It is a simple and compact format for data storage

2 It is a common format for data transfer

3 It can be opened by any spreadsheet software and text editors as well

4 It supports import/export

4 How to load data from CSV to dataframe?

Ans.: To load or transfer data from CSV to dataframe you can use read_csv() function. First you need to import pandas module and then read_csv(‘filename’) function can be used to load data. Consider this example:

import pandas as pd
df = pd.read_csv("mydata.csv")

5 Consider the following code and write what happens to after using it? Assume that pandas module is already imported as pd.

  1. pd.read_csv(“students.csv”, sep=”|”)
  2. pd.read_csv(“students.csv”,index_col=”rollno”)
  3. pd.read_csv(“students.csv”,header=None, names=[‘RNo’,’Student_Name’,’Fees’])
  4. pd.read_csv(“students.csv”,nrows=5)
  5. pd.read_csv(“students.csv”,skiprows=[1,3,5])
  6. pd.to_csv(“students.csv”,na_rep=”UnPaid”)
  7. pd.to_csv(“students.csv”,float_format=”%.2f”)
  8. pd.to_csv(“students.csv”,columns=[‘rollno’,’Student_Name’])

Ans.:

  1. It will replaces the default separator, with | symbol
  2. it assigns new index to data frame i.e. rollno
  3. It ignores the headers from CSV file and assigns new header to each columns as specified in the values as names
  4. It read only top 5 rows from CSV file
  5. This statement skiprows specified in the value i.e. 1st, 3rd and 5th
  6. This statement replaces the NaN with the value “UnPaid”
  7. It will change the decimal numbers and restrict them upto 2 point decimals
  8. It export only given columns into CSV file

6 Ms. Priya is working on an application made in python. She wrote the following command to customize the column header but getting an error. Provide her solution for the same.

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

Solution:

Df = pd.read_csv(“adm.csv”,names=[“AdmNo”,”FirstName”,”LastName”,”Class”])

Explanation:

Error 1: The letter C should be small not capital.

Error 2: read_csv() function doesn”t have columns attribute. It will take names as an argument to change the header.

7 Mr. Prakash want to hide the header from csv. He has written the following statement in python.

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

Solution:

Df = pd.read_csv(“d:\\data.csv”,header=None,skiprows=1)

Explanation:

Error 1: // is not used in windows.

Error 2: The parameter header accepts the value None to hide header in dataframe from CSV.

Error 3: He missed one parameter i.e. skiprows otherwise it will display the header as row 1.

8 Ms. Hetvee want to store 3rd ,4th ,6th ,8th ,9th row from the data.csv file to dataframe. She wrote following statement, but not getting exact output. Help her to solver her error. (Note: data.csv file contains 10 rows)

Df = pd.read_csv(“data.csv”,skiprows=[3,4,6,8,9])

Solution:

In this statement there is no syntactical error, but logical errors are there. She need to write the row indexes which she don’t want to store in dataframe. The correct statement will be like this:

Df = pd.read_csv('adm.csv',header=None,skiprows=[1,2,5,7,10])

8 Ms. Smita wrote this code to store the following data in similar pattern in CSV file (Separate each by * and ignore default index):

SNO Name Score Rank

1 Sachin 345 1

2 Manish 300 3

3 Saurabh 250 4

Solution:

import pandas as pd
emp_dict = {'SNO':[1,2,3],
                  'Name':['Sachin','Manish','Saurabh'],
                  'Score':[345,300,250],
            'Rank':[1,2,3]}
df=pd.DataFrame(emp_dict)
df.to_csv('D:\\mydata.csv',index=False,sep='*')

9 She wants to display names from the CSV file by using the following code, but she is getting error. Help her to store data in a proper manner.

df = pd.read_csv('D:\\mydata.csv')
print(df.Name)

Solution:

In the above code, data stored in csv with the separator ‘*’. So when the values stored with other separators except , it will be stored in one column only in csv. So she need to used sep parameter with read_csv function.

df = pd.read_csv('D:\\mydata.csv',sep='*')
print(df.Name)

Thank you for reading this article. If you are learning something new from our website share with your friends and classmates for our motivation. Feel free to ask your doubt in the comment section. If you are looking for any specific solutions or anything which is related to the subject use our contact us section and raise your demand. We will try to provide you the solution.

Sign up our website by email to get new updates.

Comment Your Views

%d bloggers like this: