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)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With