代码之家  ›  专栏  ›  技术社区  ›  Karn Kumar

如何从整个数据帧中只获取选定的列

  •  0
  • Karn Kumar  · 技术社区  · 6 年前

    我正试图从 html 我的页面 html格式 数据如下所示。

    1) HTML数据格式

                VM Name           User Name        Image Name                           Network  VCPUS  Memory(GB)  Disk(GB) Tenant     Region      KVM Host Power State                          URL               Created
    0      dbsw-powerbi  anokhe@ezy.com           unknown   {u'VLAN181': [u'192.168.57.91']}      4          16       100    APP  DBS-AP-IN  dbs-appkvm03          On  https://compute.ezy.com  2018-08-02T10:30:07Z
    1           pciedip  anokhe@ezy.com     dbsVDI-RHEL65   {u'VLAN181': [u'192.168.57.37']}      4          32       200    APP  DBS-AP-IN  dbs-appkvm01          On  https://compute.ezy.com  2018-04-18T06:39:38Z
    2  dbs-spbdatasync1  anokhe@ezy.com    dbsVDI-RHEL510  {u'VLAN181': [u'192.168.57.156']}      1           8        50    APP  DBS-AP-IN     dbs-kvm13          On  https://compute.ezy.com  2018-04-05T09:51:29Z
    3      dbsw-russian  anokhe@ezy.com  dbsVDI-WIN764-V1  {u'VLAN181': [u'192.168.57.216']}      1           4       100    APP  DBS-AP-IN  dbs-appkvm01          On  https://compute.ezy.com  2018-04-02T06:25:25Z
    4   dbs-spbdatasync  anokhe@ezy.com    dbsVDI-RHEL510  {u'VLAN181': [u'192.168.57.233']}      1           8        50    APP  DBS-AP-IN     dbs-kvm13          On  https://compute.ezy.com  2018-04-02T05:03:03Z
    

    我只是想试试熊猫 read_html 获取数据帧,但无法理解从数据帧获取特定列。我需要选择列 ['VM Name', 'User Name', 'Network', 'Region']

    2) 代码段

    from __future__ import print_function
    from signal import signal, SIGPIPE, SIG_DFL
    signal(SIGPIPE,SIG_DFL)
    import pandas as pd
    ##### Python pandas, widen output display to see more columns. ####
    pd.set_option('display.height', None)
    pd.set_option('display.max_rows', None)
    pd.set_option('display.max_columns', None)
    pd.set_option('display.width', None)
    pd.set_option('expand_frame_repr', True)
    
    # print(pd.read_excel('ssd.xlsx'))
    # Data = pd.read_html('http://openstacksearch/vm_list.html', header=0, flavor='bs4', index_col=['VM Name', 'User Name', 'Network', 'Region'])
    Data = pd.read_html('http://openstacksearch/vm_list.html', header=0, flavor='bs4')
    print(Data[0].head())
    
    2 回复  |  直到 6 年前
        1
  •  1
  •   Andrew    6 年前

    选择可以使用的列的子集

    Data = pd.read_html('http://openstacksearch/vm_list.html', header=0, flavor='bs4')
    Data = Data[['VM Name', 'User Name', 'Network', 'Region']]
    
        2
  •  2
  •   Karn Kumar    6 年前

    DataFrame 从处理过的 read_html

    代码如下所示。。。可能对某人有帮助

    import pandas as pd
    ##### Python pandas, widen output display to see more columns. ####
    pd.set_option('display.height', None)
    pd.set_option('display.max_rows', None)
    pd.set_option('display.max_columns', None)
    pd.set_option('display.width', None)
    pd.set_option('expand_frame_repr', True)
    ###### Data Extraction ##################
    '''
    pd.read_html returns you a list with one element and that 
    element is the pandas dataframe, i.e.
    Data = pd.read_html('url') will produce a list
    Data[0]  Will return a pandas DataFrame
    '''
    Data = pd.read_html('http://openstacksearch/vm_list.html', header=0, flavor='bs4')[0]
    Data1 = Data[['VM Name', 'User Name', 'Network', 'Region']]
    print(Data1)