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.
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.
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.
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
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