
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Extract csv file specific columns to list in Python
To extract csv file for specific columns to list in Python, we can use Pandas read_csv() method.
Steps
Make a list of columns that have to be extracted.
Use read_csv() method to extract the csv file into data frame.
Print the exracted data.
Plot the data frame using plot() method.
To display the figure, use show() method.
Example
import pandas as pd from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True columns = ["Name", "Marks"] df = pd.read_csv("input.csv", usecols=columns) print("Contents in csv file:\n", df) plt.plot(df.Name, df.Marks) plt.show()
The csv file contains the following data −
Name | Marks |
---|---|
Arun | 98 |
Shyam | 75 |
Govind | 54 |
Javed | 92 |
Raju | 87 |
Output
When we execute the code, it will extract the data from the csv file and show the following plot −
- Related Articles
- How to read CSV file in Python?
- How to convert CSV columns to text in Python?
- How to convert CSV File to PDF File using Python?
- How to Sort CSV by multiple columns in Python ?
- How to add timestamp to csv file in Python?
- Display specific columns of a file in Linux?
- Python Pandas- Create multiple CSV files from existing CSV file
- Create a GUI to convert CSV file to Excel file using Python
- How to save a Python Dictionary to CSV file?
- How to extract file extension using Python?
- How to extract data frame columns stored in a list in R?
- Reading and Writing CSV File using Python
- Python - How to write pandas dataframe to a CSV file
- Python program to extract only the numbers from a list which have some specific digits
- Python Extract specific keys from dictionary?

Advertisements