1

i am trying to transpose a HTML retrieved code by beautiful soup. The website was having 2 HTML tables in it . i have taken the tables into a variable.

  table = pd.read_html(page)
  table1 =  table[0]    
  table2 =  table[1]

The output of table1 and table2 combined gives :

    0  1
0   a  A         
1   b  B
2   c  C
3   d  D
4   e  E
5   f  F
6   g  G

The 0 and 1 in starting row and column are auto generated by pandas while i have used

 table = pd.read_html(url)

I want to reshape the table 1 and table 2 in a such way that

table = ['a','A','b','B','c','C','d','D','e','E','f','F','g','G']          

I have other list

 Second_list = ['AA','BB','CC','DD','EE']

table dataframe should be matching with second_list so that both can be combined

table = table + Second_list

so that output of table would be

['a','A','b','B','c','C','d','D','e','E','f','F','g','G','AA','BB','CC','DD','EE']

any solution other than above approach is much appreciated, the goal was to remove the auto generated columns and rows by pandas and make the multicolumn data into single row

Thanks a lot in advance, have a great day

2
  • df.values.flatten() ?? Commented Jun 7, 2021 at 17:18
  • 1
    it was showing the same structure, thanks for the help :) Commented Jun 7, 2021 at 17:34

1 Answer 1

1

If you want to flatten the dataframe, you can use np.ravel. For example:

Second_list = ["AA", "BB", "CC", "DD", "EE"]
x = df.values.ravel().tolist() + Second_list
print(x)

Prints:

['a', 'A', 'b', 'B', 'c', 'C', 'd', 'D', 'e', 'E', 'f', 'F', 'g', 'G', 'AA', 'BB', 'CC', 'DD', 'EE']
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot , it is working like charm :) .is ravel() function is used to remove the auto generated rows and columns in pandas?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.