0

I have dataframe where my resulting output columns doesn't aligned correctly as requited and that's basically a column called UID which i need to align right.

I have tried Styler but that's not working.

MY code:

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

Result Output:

                   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

What i tried:

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

Expected:

                   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

My pandas Version:

>>> pd.__version__
'1.1.5'

I am using this code on my Linux machine(RedHat7)

2

2 Answers 2

1

Use tabulate to display. below is the code.

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))
Sign up to request clarification or add additional context in comments.

1 Comment

Vivs, tanks for the answer +1 for the same but this i have tried already, i was Just looking for the usage of df.styler.
1

The answer assumes that all columns are left-aligned. I wrote the code based on the table styles in the 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

I combined it with your code.

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

7 Comments

Thanks @r-beginners, how i can align this with my code because these styles i have applied already and giving me <pandas.io.formats.style.Styler object at 0x7fb864956c18>
My code is able to change its appearance in the final output. How do you plan to use it?
It is not possible to process it as a data frame in its html state. So it can only be used for the final output. If you need to process it after styling, you should use ljust().
My response was that I wrote the code with the understanding that I wanted to format the output. What is it that you want to do? Are you asking me to show you how to use ljust()?
NO, ljust() i know, i was just talking about how the approach given by you can be fitted to my situation.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.