How to fix pandas not reading first column from csv file?

Learn, how to fix pandas not reading first column from csv file? By Pranit Sharma Last updated : October 06, 2023

Pandas is a special tool that allows us to perform complex manipulations of data effectively and efficiently. Inside pandas, we mostly deal with a dataset in the form of DataFrame. DataFrames are 2-dimensional data structures in pandas. DataFrames consist of rows, columns, and data.

CSV files or Comma Separated Values files are plain text files but the format of CSV files is tabular in nature. As the name suggests, in a CSV file, each specific value inside the CSV file is generally separated by a comma. The first line identifies the name of a data column. The further subsequent lines identify the values in rows.

Col_1_value, col_2_value ,  col_3_value
Row1_value1 , row_1_value2 , row_1_value3
Row1_value1 , row_1_value2 , row_1_value3

Here, the separator character (,) is called a delimiter. There are some more popular delimiters. E.g.: tab(\t), colon (:), semi-colon (;) etc.

Pandas not reading first column from csv file

We use pd.read_csv() method to read a CSV file. Many times when we read a CSV file, it does not read the first column. This happens because we do not use a delimiter.

Hence, we need to use the delimiter as a parameter while loading the file.

Let us understand with the help of an example,

Python code to fix pandas not reading first column from csv file

# Importing pandas package
import pandas as pd

# Importing numpy package
import numpy as np

# Loading csv file
d = pd.read_csv('csv_include_help.csv', sep=',')

# Creating a DataFrame
df = pd.DataFrame(d)

# Display Original df
print("Original DataFrame:\n",df,"\n")

Output

The output of the above program is:

Example: How to fix pandas not reading first column from csv file?

Python Pandas Programs »


Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.