Pandas python delimiter column

Viewed 40

I made a script that process a .txt file and make a .xlsx one. The datas from .txt file needs to appear as two columns in the .xlsx.

The problem its that i used separator and split, but between the groupname "admins" and username "user1" exist also that blank space and the script its taking him also, because if no elements are on the group name "space_mobile_app" that row should not be on the output (I put below how the output should look) This is how .txt file looks:

.txt file

This is my script:

def groupmembership():
    # readeing the file with current timestr form declared path in folder .\svnrawdatas
    timestr = time.strftime("%Y-%m-%d")
    pathdest = path_dir()
    df = pd.read_csv(rf"{pathdest}\{timestr}-rawGitData_group.bck.txt", sep='|',header=None)
    df.columns = ['groups']
    df['New Columns'] = df['groups'].apply(lambda x: x.split(':')[0])
    df['groups'] = df['groups'].apply(lambda x: ' '.join(x.split(':')[1:]))
    df['groups'] = df['groups'].apply(lambda x: x.split(' '))
    df = df.explode('groups')
    df.rename(columns={'groups': 'accountName', 'New Columns': 'groupName'}, inplace=True)
    df["accountName"] = df["accountName"].str.strip()
    print(df)

This is what he outputs now:

script output

And this is what she should output:

enter image description here

1 Answers

Add this code snippet before the print. Basically it is filtering the row that has empty account name


df= df[df['accountName']!='']

Related