【发布时间】: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) 上使用此代码
【问题讨论】:
-
这不是关于“如何左对齐”,而是如何将您的列与 pandas
df.style对齐。