Convert CSV to Pandas Dataframe

Convert CSV to Pandas Dataframe

To convert a CSV file to a pandas DataFrame, you'll first need to install pandas:

pip install pandas 

Once you have pandas installed, you can use the read_csv() function to read the contents of a CSV file into a DataFrame.

Here's a simple example:

import pandas as pd # Read the CSV file into a DataFrame df = pd.read_csv('filename.csv') # Display the first few rows of the DataFrame print(df.head()) 

Additional Options in read_csv():

  • delimiter or sep: Specify a different delimiter if your CSV uses something other than a comma.

    df = pd.read_csv('filename.csv', delimiter=';') 
  • header: Specify which row number to use as a header. Default is 0 (the first row). Use None if there's no header.

    df = pd.read_csv('filename.csv', header=None) 
  • names: If you're not using a header in the file (or even if you are), you can set column names manually.

    df = pd.read_csv('filename.csv', header=None, names=['Column1', 'Column2', ...]) 
  • index_col: Specify a column to use as the index for the DataFrame.

    df = pd.read_csv('filename.csv', index_col='ColumnName') 
  • dtype: Define types for columns.

    df = pd.read_csv('filename.csv', dtype={'ColumnName': 'int32'}) 
  • skiprows: Skip a number of rows at the beginning of the file.

    df = pd.read_csv('filename.csv', skiprows=2) 
  • na_values: Specify additional strings to recognize as NA/NaN.

    df = pd.read_csv('filename.csv', na_values=['NA', 'MISSING']) 

This is just a subset of the options available. The read_csv() function is versatile and can handle many different types of CSV formats and quirks. You can always refer to its documentation for a full list of options.


More Tags

bpmn cpu-registers ios12 ecmascript-6 proximitysensor parsec gnu-coreutils sharpssh prepared-statement reachability

More Programming Guides

Other Guides

More Programming Examples