Search code examples
pythonpandascsvfixed-width

How to read in file with delimiter in pandas?


I am using the pandas library and how can I split the given dataframe into rows and columns based on comma seprated. Because if I try it gives the error it cannot seprate and throws following error.

6.1101,17.592
5.5277,9.1302
8.5186,13.662
7.0032,11.854
5.8598,6.8233
8.3829,11.886

And the above given line consist my dataset. And the code is:

import pandas as pd
from sklearn import linear_model
import matplotlib.pyplot as plt
dataframe = pd.read_fwf("challenge_dataset.txt")

Solution

  • The pandas.read_fwf can have delimiter argument.

    dataframe = pd.read_fwf("challenge_dataset.txt", delimiter=",")
    

    You can read more in pandas.read_fwf

    read_csv is automatically reads with comma separator, although you can change the delimiter argument in read_csv as well.