1

I need help...I'm trying to merge tables from different sites.

I succeeded to extract each tables what I want, but I can't merge them into one.. I used 'for~~in' but failed.

[my goal] This image is what I'm trying to make. I'm really new to coding and this community, sorry if the question seems bad.

Thanks for reading Sincerely, KB

import pandas as pd 

ticker = '005930' #'139480'
table = pd.read_html('http://comp.fnguide.com/SVO2/ASP/SVD_Main.asp?pGB=1&gicode=A'+str(ticker)+'&cID=&MenuYn=Y&ReportGB=&NewMenuID=Y&stkGb=701')

report1 = table[10]
report2 = report1.loc[[0, 1, 3, 17]]
report2

#for tickers in ticker:
#    url=print('http://comp.fnguide.com/SVO2/ASP/SVD_Main.asp?pGB=1&gicode=A'+str(ticker)+'&cID=&MenuYn=Y&ReportGB=&NewMenuID=Y&stkGb=701')
Dawn
  • 13
  • 3
  • if the index is the same, you can do a merge. You don't need to do a for loop. Using loops in pandas is a bad thing. There are much better ways to solve two dataframe. Search stack overflow on how to merge two datasets – Joe Ferndz Nov 30 '20 at 06:41
  • @JoeFerndz Thanks! I solved the problem with many help (I got another problem though lol) :):) – Dawn Dec 01 '20 at 07:23

2 Answers2

3

You can join them with a simple concat function.

both_tables = pd.concat([report1, report2])

both_tables

Example

First table

import pandas as pd

df1 = pd.DataFrame([
['1', 'Fares', 32, True],
['2', 'Elena', 23, False],
['3', 'Steven', 40, True]])

df1.columns = ['id', 'name', 'age', 'decision']

df1

enter image description here

Second table

df2 = pd.DataFrame([
['4', 'Max', 24, True],
['5', 'Elena', 20, False],
['6', 'Steven', 40, True]])

df2.columns = ['id', 'name', 'age', 'decision']

df2

enter image description here

Joining both tables togehter

df3 = pd.concat([df1, df2])

df3

enter image description here

I hope I could help you out a bit.

Maximilian Freitag
  • 777
  • 2
  • 5
  • 20
  • Umm this is table from same site. I'm not trying to merge report 1 and 2. I'm trying to merge report 2 and different ticker's report '139480'. Sorry my question was not detailed. – Dawn Nov 30 '20 at 06:51
  • 1
    Wow! Now I get it. Thanks a lot!! Bless you!! :):) – Dawn Nov 30 '20 at 06:52
0

Also I suggest you to read this website which explain concatenating for tables in detail.

https://pandas.pydata.org/pandas-docs/stable/getting_started/intro_tutorials/08_combine_dataframes.html

Reihaneh Kouhi
  • 427
  • 1
  • 4
  • 19