Informatics Practices
How can we import specific columns from a CSV file?
Python Data Handling
1 Like
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)
Answered By
3 Likes
Related Questions
Write a program that reads students marks from a ‘Result.csv’ file and displays percentage of each student.
Write the name of function to store data from a dataframe into a CSV file.
What are advantages of CSV file formats ?
What all libraries do you require in order to bring data from a CSV file into a DataFrame ?