Panda iloc only returns header 行和数据框中的第一个值行

Panda iloc only returns header row and first value row in dataframe

即使删除 iloc 也不起作用。我已尝试将其导入 CSV 文件,但无法解决问题。 the link 上的其他表格似乎没有与这张图相同的问题。当我 运行 现在这样做时,我没有得到 30 个数据行,而是只得到 2 个。

代码如下:

#import libraries
import pandas as pd
import lxml


#makes terminal larger to see full table
pd.set_option('display.max_rows', 5000)
pd.set_option('display.max_columns', 5000)
pd.set_option('display.width', 1000)

#specify the url
quote_page = 'https://ysx-mm.com/main-board/listing/company/lc00001/'

#makes it into a dataframe
dfs = pd.read_html(quote_page)
df = dfs[2]
ds = df.iloc[0:30, 0:5]
print(ds)

#exports as a csv file
ds.to_csv('test.csv', index=False)

你只需要改成这样:

dfs = pd.read_html(quote_page, header =0, flavor = 'bs4')

然后你的代码 return:

            Date   Open   High    Low  Close  Trading volume (shares)  Trading value (MMK)  No. of listed Shares  Market Cap. (mil MMK)
0     16 Mar 2021   9300   9300   9300   9300                      554              5152200              33109694                 307920
1     15 Mar 2021   9400   9400   9400   9400                        1                 9400              33109694                 311231
2     12 Mar 2021   9400   9400   9400   9400                      110              1034000              33109694                 311231
3     12 Mar 2021   9400   9400   9400   9400                      110              1034000              33109694                 311231
4     11 Mar 2021   9300   9300   9300   9300                       60               558000              33109694                 307920
5     10 Mar 2021   9400   9400   9400   9400                       30               282000              33109694                 311231
6      9 Mar 2021   9500   9500   9400   9400                       31               294400              33109694                 311231
7      8 Mar 2021   9500   9500   9500   9500                       17               161500              33109694                 314542
8      5 Mar 2021   9300   9300   9300   9300                       49               455700              33109694                 307920
9      4 Mar 2021   9500   9500   9500   9500                       55               522500              33109694                 314542
10     3 Mar 2021   9500   9500   9500   9500                       50               475000              33109694                 314542
11     1 Mar 2021   9500   9500   9500   9500                      791              7514500              33109694                 314542
12    26 Feb 2021   9300   9300   9300   9300                       26               241800              33109694                 307920
13    25 Feb 2021   9300   9300   9300   9300                     1162             10806600              33109694                 307920
14    24 Feb 2021   9200   9300   9200   9300                      311              2872100              33109694                 307920
..................................................