16

With comma separated text stored in a var like below

data = """
Class,Name,Long,Lat
A,ABC11,139.6295542,35.61144069
A,ABC20,139.630596,35.61045559
A,ABC03,139.6300307,35.61327781
B,ABC54,139.7787818,35.68847945
B,ABC05,139.7814447,35.6816882
B,ABC06,139.7788191,35.681865
B,ABC24,139.7790396,35.67781697
"""

is there a quick way to read this into a pandas data frame without have to store to a file and use pd.read_csv(). I'm coming from R and it provides a nice way to do this like below.

text <- "
State,District,County,Num Voters,Total Votes in State,Votes for None,Candidate Name,Party,Votes Scored
CA,San Diego,Delmar,190962,48026634,2511,A1,IND,949
CA,San Diego,Delmar,190962,48026634,2511,A2,RP(K),44815
"
df <- read.table(textConnection(text), sep = ",", header = TRUE)
2

2 Answers 2

28

With io.StringIO object (in-memory stream for text I/O):

import pandas as pd
from io import StringIO

data = """
Class,Name,Long,Lat
A,ABC11,139.6295542,35.61144069
A,ABC20,139.630596,35.61045559
A,ABC03,139.6300307,35.61327781
B,ABC54,139.7787818,35.68847945
B,ABC05,139.7814447,35.6816882
B,ABC06,139.7788191,35.681865
B,ABC24,139.7790396,35.67781697
"""

df = pd.read_csv(StringIO(data))
print(df)

The output:

  Class   Name        Long        Lat
0     A  ABC11  139.629554  35.611441
1     A  ABC20  139.630596  35.610456
2     A  ABC03  139.630031  35.613278
3     B  ABC54  139.778782  35.688479
4     B  ABC05  139.781445  35.681688
5     B  ABC06  139.778819  35.681865
6     B  ABC24  139.779040  35.677817
Sign up to request clarification or add additional context in comments.

Comments

-3
from prettytable import PrettyTable

def create_table(data):
    data = data.strip().split('\n')
    pt = PrettyTable()
    pt.field_names = data[0].split(',')

    for row in data[1:]:
        pt.add_row(row.split(','))

    return pt

data = """
Class,Name,Long,Lat
A,ABC11,139.6295542,35.61144069
A,ABC20,139.630596,35.61045559
A,ABC03,139.6300307,35.61327781
B,ABC54,139.7787818,35.68847945
B,ABC05,139.7814447,35.6816882
B,ABC06,139.7788191,35.681865
B,ABC24,139.7790396,35.67781697
"""

table = create_table(data)

print(table)
+-------+-------+-------------+-------------+
| Class |  Name |     Long    |     Lat     |
+-------+-------+-------------+-------------+
|   A   | ABC11 | 139.6295542 | 35.61144069 |
|   A   | ABC20 |  139.630596 | 35.61045559 |
|   A   | ABC03 | 139.6300307 | 35.61327781 |
|   B   | ABC54 | 139.7787818 | 35.68847945 |
|   B   | ABC05 | 139.7814447 |  35.6816882 |
|   B   | ABC06 | 139.7788191 |  35.681865  |
|   B   | ABC24 | 139.7790396 | 35.67781697 |
+-------+-------+-------------+-------------+

or

from prettytable import from_csv
from io import StringIO

table = from_csv(StringIO(data))

print(table)

1 Comment

That doesn't return an instance of pandas.DataFrame as the question asks for, but an instance of prettytable.PrettyTable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.