Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create pandas dataframe from string (in csv format)

Tags:

python

pandas

I have a string in this format:

"A1","B1","C1","D1","E1","F1","G1","H1"\n"A2","B2","C2","D2","E2","F2" etc

where A to H are columns and the numbers refer to the rows.

I'm looking for the quickest way to create a pandas dataframe.

A long (in time to complete) approach I tried is to use:

df = pd.DataFrame()
for row in data:
    reader = csv.reader(row)
    mylist = []
    for element in reader:
        if element!=['','']:
            mylist.append(element[0])
    df2 = pd.DataFrame([mylist])
    df = df.append(df2)

I'm looking for a quicker way.

like image 387
alwayscurious Avatar asked Oct 24 '18 10:10

alwayscurious


People also ask

How do you create a DataFrame from a string?

Method 1: Create Pandas DataFrame from a string using StringIO() One way to achieve this is by using the StringIO() function. It will act as a wrapper and it will help us to read the data using the pd. read_csv() function.

Can you create a DataFrame from a CSV file?

Method #3: Using the csv module: One can directly import the csv files using the csv module and then create a data frame using that csv file.


1 Answers

I believe you need StringIO with read_csv:

import pandas as pd

data = '"A1","B1","C1","D1","E1","F1","G1","H1"\n"A2","B2","C2","D2","E2","F2"'
df = pd.read_csv(pd.compat.StringIO(data), header=None)

print (df)


    0   1   2   3   4   5    6    7
0  A1  B1  C1  D1  E1  F1   G1   H1
1  A2  B2  C2  D2  E2  F2  NaN  NaN
like image 154
jezrael Avatar answered Sep 21 '22 08:09

jezrael