代码之家  ›  专栏  ›  技术社区  ›  Kavya shree

如何在python中使用pandas以表格格式显示文本?

  •  0
  • Kavya shree  · 技术社区  · 6 年前



    标题1 URL2
    标题1 URL3
    标题1 URL4
    标题1 URL5
    标题1 URL6

    想用熊猫把它显示成桌子吗

    标题1 url1
    URL 2

    URL 4

    URL 6

    1 回复  |  直到 6 年前
        1
  •  2
  •   Abbas    6 年前

    IIUC:以下是你能做的:

    from pandas.compat import StringIO
    import pandas as pd
    import numpy as np
    
    text = '''title url
    title1 URL1
    title1 URL2
    title1 URL3
    title1 URL4
    title1 URL5
    title1 URL6'''
    
    df = pd.read_csv(StringIO(text), sep='\s+')
    #Drop duplicates by keeping first
    df['title'] = df['title'].drop_duplicates(keep='first')
    #Replace nan with white space
    df = df.replace(np.nan, ' ', regex=True)
    df.to_html('test.html')
    

    您将得到如下输出:

    enter image description here