0

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")
smci
  • 32,567
  • 20
  • 113
  • 146

2 Answers2

4

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.

omri_saadon
  • 10,193
  • 7
  • 33
  • 58
  • How can I seprate the data such that the first corresponds to x value of graph and second to the y value? –  Jun 25 '17 at 17:41
  • 1
    Note that pandas read_fwf is for fixed width format, which the OP is not showing. This answer works but read_csv is more appropriate – JimLohse Oct 27 '17 at 18:14
0

You need read_csv for comma separated values:

rt pandas as pd
dataframe = pd.read_csv("challenge_dataset.txt")

https://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_csv.html

JimLohse
  • 1,209
  • 4
  • 19
  • 44