Home > front end >  Save lists as rows of a dataframe
Save lists as rows of a dataframe

Time:07-11

I am new to pandas. I hope this is not too easy :). I have tried to solve this problem without success.

I am using beatifulsoup to scrape a website. My variable gets the result I am looking for.

var = [sd.get_text() for sd in x.select("li")]

The variable contains this output:

var =
[A, B, C, D, E, F, G, H]
[I, J, K, L, M, M, N, O, P]
[Q, R, S, T, U, V, W, X]

In addition, I have the title of the columns:

columns = ["Element1", "Element2", "Element3", "Element4", "Element5", "Element6", "Element7", "Element8"]

I want to store each list collected in the variable as a row in my Dataframe. I think I have to iterate over the variable with a for loop to do this, but I'm not sure.

The result I'm looking for is this:

Element1 Element2 Element3 Element4 Element5 Element6 Element7 Element8
A B C D E F G H
I J K L M N O P
Q R S T U V W X

Thanks!

CodePudding user response:

Convert your variable to a list of lists and pass it to a DataFrame constructor -

myvar = [[A, B, C, D, E, F, G, H],
[I, J, K, L, M, M, N, O, P],
[Q, R, S, T, U, V, W, X]]

df = pd.DataFrame(myvar, columns=columns)

CodePudding user response:

not sure how the first variable is stored

I will assume text:

data = '''
[A, B, C, D, E, F, G, H]
[I, J, K, L, M, N, O, P]
[Q, R, S, T, U, V, W, X]'''

added data cleaning step:

import re
data = re.sub(r'\[|\]', ' ', data)

to df:

import pandas as pd
from io import StringIO
df = pd.read_csv(StringIO(data), names=columns)
  • Related