如何对齐熊猫数据框列

时间:2021-05-28 06:27:11

标签: python pandas dataframe

我有数据框,其中生成的输出列未按要求正确对齐,这基本上是一个名为 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)上使用此代码

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

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

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))