【问题标题】:How align the pandas dataframe columns如何对齐熊猫数据框列
【发布时间】:2021-08-16 09:44:54
【问题描述】:

我有一个数据框,其中我的结果输出列没有按照要求正确对齐,这基本上是一个名为 UID 的列,我需要对齐 right

我已经尝试过 Styler,但这不起作用。

我的代码:

from io import BytesIO
import subprocess
import pandas as pd
from IPython.display import display
######################
lcmd='ldapsearch -x -LLL -b "ou=Functional,ou=People,ou=dtc,o=dtc"'
result = subprocess.check_output(lcmd, shell=True)
buffer = BytesIO(result)
df = pd.read_csv(filepath_or_buffer = buffer, header=None, names=['LoginShell', 'ExpiryDate', 'UID'])
df = df.applymap(lambda x: x.split(': ')[-1])
df = df[df.columns[::-1]]
print(df.head())

结果输出:

                   UID ExpiryDate LoginShell
0             auto_soc   20991212  /bin/bash
1             sambakul   20991212  /bin/bash
2  services2go-jenkins   20991212  /bin/bash
3              rdtest0   20991212  /bin/bash
4                 sudo   20991212  /bin/bash

我尝试了什么:

>>> df.style.set_properties(**{'text-align': 'left'})
<pandas.io.formats.style.Styler object at 0x7f052c2998d0>

or

>>> df.style.set_properties(subset=["UID", "ExpiryDate", "LoginShell"], **{'text-align': 'left'})
<pandas.io.formats.style.Styler object at 0x7f052b226fd0>

预期:

                   UID                  ExpiryDate  LoginShell
0                  auto_soc             20991212    /bin/bash
1                  sambakul             20991212    /bin/bash
2                  services2go-jenkins  20991212    /bin/bash
3                  rdtest0              20991212    /bin/bash
4                  sudo                 20991212    /bin/bash

我的熊猫版本:

>>> pd.__version__
'1.1.5'

我在我的 Linux 机器 (RedHat7) 上使用此代码

【问题讨论】:

标签: python pandas dataframe


【解决方案1】:

答案假定所有列都是左对齐的。我根据official reference中的表格样式编写了代码。

import pandas as pd
import numpy as np
import io

data = '''
                   UID ExpiryDate LoginShell
0             auto_soc   20991212  /bin/bash
1             sambakul   20991212  /bin/bash
2  services2go-jenkins   20991212  /bin/bash
3              rdtest0   20991212  /bin/bash
4                 sudo   20991212  /bin/bash
'''

df = pd.read_csv(io.StringIO(data), delim_whitespace=True)

from IPython.display import HTML
styles = [dict(selector='td', props=[('text-align','left')])]
html = df.style.set_table_styles(styles)
html
        UID     ExpiryDate  LoginShell
0   auto_soc    20991212    /bin/bash
1   sambakul    20991212    /bin/bash
2   services2go-jenkins     20991212    /bin/bash
3   rdtest0     20991212    /bin/bash
4   sudo    20991212    /bin/bash

我把它和你的代码结合起来了。

from io import BytesIO
import subprocess
import pandas as pd
from IPython.display import display

lcmd='ldapsearch -x -LLL -b "ou=Functional,ou=People,ou=dtc,o=dtc"'
result = subprocess.check_output(lcmd, shell=True)
buffer = BytesIO(result)
df = pd.read_csv(filepath_or_buffer = buffer, header=None, names=['LoginShell', 'ExpiryDate', 'UID'])
df = df.applymap(lambda x: x.split(': ')[-1])
df = df[df.columns[::-1]]
styles = [dict(selector='td', props=[('text-align','left')])]
html = df.style.set_table_styles(styles)
html

【讨论】:

  • 感谢@r-beginners,我如何将其与我的代码对齐,因为我已经应用了这些样式并给了我&lt;pandas.io.formats.style.Styler object at 0x7fb864956c18&gt;
  • 我的代码能够改变它在最终输出中的外观。你打算如何使用它?
  • 无法将其作为 html 状态的数据框进行处理。所以它只能用于最终输出。如果需要在造型后进行处理,请使用ljust()
  • 我的回答是,我在编写代码时理解我想要格式化输出。你想做什么?你是要我教你如何使用ljust()吗?
  • 不,ljust() 我知道,我只是在谈论您提供的方法如何适合我的情况。
【解决方案2】:

使用表格显示。下面是代码。

from io import BytesIO
import subprocess
import pandas as pd
from tabulate import tabulate

lcmd='ldapsearch -x -LLL -b "ou=Functional,ou=People,ou=dtc,o=dtc"'
result = subprocess.check_output(lcmd, shell=True)
buffer = BytesIO(result)
df = pd.read_csv(filepath_or_buffer = buffer, header=None, names=['LoginShell','ExpiryDate', 'UID'])
df = df.applymap(lambda x: x.split(': ')[-1])
df = df[df.columns[::-1]]
print(tabulate(df, showindex=False, headers=df.columns))

【讨论】:

  • Vivs, tank for the answer +1 for the same but this 我已经尝试过了,我只是在寻找 df.styler 的用法。
猜你喜欢
  • 2020-03-12
  • 2023-03-11
  • 2013-06-18
  • 2016-01-28
  • 2019-09-09
  • 2018-09-07
  • 2020-06-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多