Issue
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]
I add what this variable prints by request of comments:
['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']
['I', 'J', 'K', 'L', '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!
Result
Finally, I was able to solve the problem.
I have passed my var variable to a new list containing only one column.
list_columns = ["1"]
df = pd.DataFrame(newvar, columns=list_columns)
split_df = pd.DataFrame(df['1'].tolist(), columns = ["Element1", "Element2", "Element3", "Element4", "Element5", "Element6", "Element7", "Element8"])
split_df.to_csv('element.csv', index=False)
In this way I managed to store all the data with the correct columns.
Thanks @Mortz for the help!
Solution
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)
Answered By - Mortz
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.