Formatting a column with pandas
Question: I'm new to Pandas and Python.

We have a firewall application that parses out our ACLs in CSV format. The
problem is -it provides way too much info -the format of the data makes the
info useless

We've been editing these queries by hand until now.

I've figured out how to use pandas to "pull" the columns we need. Now I need
to reconfigure one of the columns to the proper format.

So far my code looks like this:

    
    
    import pandas as pd
        f=pd.read_csv("/Volumes/Untitled/ACL-SOURCE.csv")
        keep_col = ['Device name','Source','Destination','Service']
        new_f = f[keep_col]
    
    # this pulls the 4 columns I ned out of the original 20 column CSV.
    
    # If I do a print of 'new_f" i get the following:
    
       Device name      Source    Destination        Service
    0  ACL-NAME-V1     ABC-123        MEC-KLM        ssh/tcp
    1  ACL-NAME-V1     ABC-123        MEC-KLM       3306/tcp
    2  ACL-NAME-V1     MEC-456        MEC-KLM        ssh/tcp
    3  ACL-NAME-V1     MEC-456        MEC-KLM       3306/tcp
    4  ACL-NAME-V1     MEC-456        MEC-KLM       4444/tcp
    5  ACL-NAME-V1     MEC-456        MEC-KLM       8888/tcp
    6  ACL-NAME-V1     MEC-456        MEC-KLM  4567-4568/tcp
    7  ACL-NAME-V1     MEC-456        MEC-KLM           icmp
    

At this point what I want to do is format the last row to Move the tcp in
front of the port number and remove the '/'. the end result will be from
ssh/tcp to tcp ssh

for some reason I can't wrap my head around this.

Please help. Thanks,


Answer: apply will apply a function to everything in your series. The thing is to make
the function. I called it switch and 'x' is all the data in the series.

    
    
    def switch(x):
        a = x.split('/')
        a.reverse()
        return ' '.join(a)
    
    df['Service'].apply(switch)
    

