read a csv in python

To read a CSV file in Python, we can use the pandas read_csv() function. Here's an example:

main.py
import pandas as pd

# read CSV file into a DataFrame
df = pd.read_csv('filename.csv')
87 chars
5 lines

Replace 'filename.csv' with the actual path and filename of your CSV file.

If the first row of your CSV file contains headers, you can specify that using the header parameter:

main.py
# read CSV file with headers
df = pd.read_csv('filename.csv', header=0)
72 chars
3 lines

If the CSV file is not comma-separated, you can specify the delimiter using the delimiter parameter:

main.py
# read CSV file with semicolon delimiter
df = pd.read_csv('filename.csv', delimiter=';')
89 chars
3 lines

Once you've read the CSV file into a DataFrame, you can manipulate and analyze the data using pandas functions.

related categories

gistlibby LogSnag