CBSE Class 12 Informatics Practices Question 25 of 44

Data Handling using Pandas — Question 26

Back to all questions
26
Question

Question 23

How can we import specific columns from a CSV file?

Answer

To import specific columns from a CSV file, we can use the usecols parameter of the read_csv function from the pandas library. The usecols parameter is used to specify the list of columns to be read from the CSV file. The syntax is pd.read_csv('filename.csv', usecols = ['column1', 'column2',...]).

For example, the following command will access Name and Age columns of Employee file.

df = pd.read_csv("Employee.csv", usecols = ['Name', 'Age'])
print(df)