【问题标题】:Python Pandas wrap text in cell output it to_htmlPython Pandas 将文本包装在单元格中输出它 to_html
【发布时间】:2020-08-12 07:40:23
【问题描述】:

我正在尝试在某个点换行文本,例如| 在单元格中并将其导出为 html。

一个例子:

import pandas as pd
df = pd.DataFrame({'EmployeeId': ['157', '292', '643', '124', '355'],
                     'City': ['Mumbai|Bangalore', 'Pune|Mumbai|Delhi', 'Mumbai|Bangalore', 'Mumbai|Pune', 'Bangalore']})

print(df)
df.to_html('test1.html')

输出:

   EmployeeId      City
0  157        Mumbai|Bangalore 
1  292        Pune|Mumbai|Delhi
2  643        Mumbai|Bangalore 
3  124        Mumbai|Pune      
4  355        Bangalore     

我会有一个这样的 html 文件(预期):

Image

输出:

   EmployeeId  City
0  157        Mumbai
              Bangalore 
1  292        Pune
              Mumbai
              Delhi
2  ...         ...  

任何帮助将不胜感激。

【问题讨论】:

  • 检查 str.contains

标签: python html string pandas dataframe


【解决方案1】:

基本上这个想法是使用str.split(),后跟explode()。类似下面的代码应该会有所帮助。

(df.set_index(['EmployeeId']).apply(lambda x:x.str.split('|').explode()).reset_index())   

输出会是这样的

  EmployeeId       City
0        157     Mumbai
1        157  Bangalore
2        292       Pune
3        292     Mumbai
4        292      Delhi
5        643     Mumbai
6        643  Bangalore
7        124     Mumbai
8        124       Pune
9        355  Bangalore

【讨论】:

  • 您可以参考这里的相关问题:stackoverflow.com/questions/50731229/…
  • (df.set_index(['EmployeeId']).apply(lambda x:x.str.split('|').explode()).reset_index()).groupby([' EmployeeId','City']).count().to_html('test1.html') 我认为很少有额外的代码可以帮助这个例子
【解决方案2】:

让我们做吧

yourdf=df.City.str.split('|').explode().to_frame('City').join(df[df.columns.difference(['City'])])
        City EmployeeId
0     Mumbai        157
0  Bangalore        157
1       Pune        292
1     Mumbai        292
1      Delhi        292
2     Mumbai        643
2  Bangalore        643
3     Mumbai        124
3       Pune        124
4  Bangalore        355

【讨论】:

    【解决方案3】:

    非常感谢您的帮助!

    我试试这个方法。我相信有更好的代码。

    import pandas as pd # Import the data
    df = pd.DataFrame({'EmployeeId': ['157', '292', '643', '124', '355'],
                       'City': ['Mumbai|Bangalore', 'Pune|Mumbai|Delhi', 'Mumbai|Bangalore', 'Mumbai|Pune', 'Bangalore']})
    #print(df)
    
    df1 = df["City"].str.split("|", expand=True).stack().reset_index(level=1, drop=True)
    #print(df1)
    
    df2 = pd.concat([df, df1], axis=1, sort=False)
    #print(df2)
    
    df2 = df2.drop(["City"], axis=1)
    #print(df2)
    
    df2.loc[(df2["EmployeeId"].duplicated()  ), ["EmployeeId"]] = ''
    df2.columns = ['EmployeeId', 'City New']
    print(df2)
    
    df2.to_html('test1.html')
    

    输出:

      EmployeeId   City New
    0        157     Mumbai
    0             Bangalore
    1        292       Pune
    1                Mumbai
    1                 Delhi
    2        643     Mumbai
    2             Bangalore
    3        124     Mumbai
    3                  Pune
    4        355  Bangalore
    

    当导出到_html时,我得到了这个:

    Image 1: With Grid

    有没有像这样在没有网格的情况下导出它(也许有样式)?

    Image 2: Without Grid

    【讨论】:

    • 如果您更新问题本身的相关部分,而不是将其添加到答案中,那将是理想的选择。
    • 感谢您的建议,以后我会记住的
    猜你喜欢
    • 1970-01-01
    • 2016-10-09
    • 2020-02-13
    • 2017-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-08
    • 1970-01-01
    相关资源
    最近更新 更多