0

How to split a column value to multiple columns

import pandas as pd

dataframe = pd.read_csv(r"logon.csv", nrows= 9128)
dataframe[["place","room","computer_number"]] = dataframe["location"].str.split("-",expand=True, nrows=9128)
dataframe.drop(["location"], axis=1, inplace= True)
dataframe.to_csv("logon.csv", index= False)

error:

File "C:\Python310\lib\site-packages\pandas\io\parsers\c_parser_wrapper.py", line 225, in read
    chunks = self._reader.read_low_memory(nrows)
  File "pandas\_libs\parsers.pyx", line 805, in pandas._libs.parsers.TextReader.read_low_memory
  File "pandas\_libs\parsers.pyx", line 861, in pandas._libs.parsers.TextReader._read_rows
  File "pandas\_libs\parsers.pyx", line 847, in pandas._libs.parsers.TextReader._tokenize_rows
  File "pandas\_libs\parsers.pyx", line 1960, in pandas._libs.parsers.raise_parser_error
pandas.errors.ParserError: Error tokenizing data. C error: Expected 6 fields in line 278, saw 7
Manjunath K Mayya
  • 1,078
  • 1
  • 11
  • 20
  • May be this will help? https://stackoverflow.com/questions/56258448/only-read-certain-rows-in-a-csv-file-with-python – hteza Mar 04 '22 at 04:09

1 Answers1

0

Error you see is from csv parser. This simply means that one of the lines in file logon.csv has extra fields (7 fields instead of 6 as per the header). Does your csv file has header row?

For e.g. consider this file:

id,date,location
1,1-1-2020,ND-1-34
2,1-1-2020,NY-1-32
3,1-1-2020,NF-1-34
4,1-1-2020,ID-3-14
5,1-1-2020,OD-1-34
6,1-1-2020,NX-5-38
7,1-1-2020,NC-1-94
8,1-1-2020,AD-9-30
9,1-1-2020,NX-5-38
10,1-1-2020,NC-1-94
11,1-1-2020,ID-3-14
12,1-1-2020,OD-1-34

It parses correctly with pd.read_csv(r"logon.csv", nrows=10)

But this csv file will fail with the same python code, because line 5 has an extra field.

id,date,location
1,1-1-2020,ND-1-34
2,1-1-2020,NY-1-32
3,1-1-2020,NF-1-34
4,1-1-2020,ID-3-14,1
5,1-1-2020,OD-1-34
6,1-1-2020,NX-5-38
7,1-1-2020,NC-1-94
8,1-1-2020,AD-9-30
9,1-1-2020,NX-5-38
10,1-1-2020,NC-1-94
11,1-1-2020,ID-3-14
12,1-1-2020,OD-1-34

with error pandas.errors.ParserError: Error tokenizing data. C error: Expected 3 fields in line 5, saw 4

Pankaj Saini
  • 1,164
  • 1
  • 5
  • 4