首页 > 解决方案 > 打印特定列。熊猫数据框

问题描述

该程序输出一个包含两列的表。我可以指定要显示哪些列吗?

def main():
    bulletins = os.listdir(INPUT_DATA_DIR)

    df = pd.DataFrame(bulletins)
    df.columns = ['filename']
    df['html'] = df.filename.apply(read_file)
    print(df.head())


def get_document_id(page):
    soup = BeautifulSoup(page, 'lxml')
    div = soup.find('div')
    print(div)

def read_file(filename):
    with open(INPUT_DATA_DIR / filename,'r') as f:
        data = f.read()
    return data

现在我有两个专栏,以后还会有更多。我可以只输出某些列吗?例如,我可以输出前两列吗?

目前我有这张桌子:

filename                                               html
0    support.hpe.com-hpesc-public-api-document-c008...  <!DOCTYPE html><html xmlns:msxsl="urn:schemas-...
1    support.hpe.com-hpesc-public-api-document-c043...  <!DOCTYPE html><html xmlns:msxsl="urn:schemas-...
2    support.hpe.com-hpesc-public-api-document-c008...  <!DOCTYPE html><html xmlns:msxsl="urn:schemas-...
3    support.hpe.com-hpesc-public-api-document-c007...  <!DOCTYPE html><html xmlns:msxsl="urn:schemas-...
4    support.hpe.com-hpesc-public-api-document-c018...  <!DOCTYPE html><html xmlns:msxsl="urn:schemas-...
..                                                 ...                                                ...
442  support.hpe.com-hpesc-public-api-document-c009...  <!DOCTYPE html><html xmlns:msxsl="urn:schemas-...
443  support.hpe.com-hpesc-public-api-document-c021...  <!DOCTYPE html><html xmlns:msxsl="urn:schemas-...
444  support.hpe.com-hpesc-public-api-document-c009...  <!DOCTYPE html><html xmlns:msxsl="urn:schemas-...
445  support.hpe.com-hpesc-public-api-document-c008...  <!DOCTYPE html><html xmlns:msxsl="urn:schemas-...
446  support.hpe.com-hpesc-public-api-document-c008...  <!DOCTYPE html><html xmlns:msxsl="urn:schemas-...

[447 rows x 2 columns]

标签: python-3.xpandas

解决方案


推荐阅读