Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python pandas NameError: StringIO is not defined

I am unable to read data in Pandas: Input:

import pandas as pd

data = 'a,b,c\n1,2,3\n4,5,6'

pd.read_csv(StringIO(data),skipinitialspace=True)

Output:

NameError:name 'StringIO' is not defined

Please let me know why the error occurred and also let me know what to import.

like image 673
Abhishek Avatar asked May 30 '16 17:05

Abhishek


1 Answers

Try to add the below packages These packages should add this line at the beginning of your script.

import io
from io import StringIO
import string
import pandas as pd
from pandas.compat import StringIO
from collections import Counter

try:
    from StringIO import StringIO
except ImportError:
    from io import StringIO

After adding the above packages I am not getting the below error

ModuleNotFoundError: No module named 'StringIO'
like image 148
Thrindadh Avatar answered Sep 16 '22 12:09

Thrindadh