Home > Software design >  How to align the dataframe in python to respective column in txt file?
How to align the dataframe in python to respective column in txt file?

Time:07-22

Dataframe format :

   ID               party test          MRK  Bundle  Entity
0  16069518     Australia 4538   4508329.38             abc
1  15907047         India 7595    -24646.59             cbd
2   1890070        Canada 3481  21556268.87             xyz
3  10978227       America 7595   -258747.70             yac
4  13968376         Italy 6310    146648.45             gbc

While transferring the above dataframe to txt, the txt should be in the below format

Required format in txt with spacing:

ID                  party         test            MRK    Bundle ID   Entity
16069518        Australia         4538     4508329.38                   abc
15907047            India         7595      -24646.59                   cbd
1890070            Canada         3481    21556268.87                   xyz
10978227          America         7595     -258747.70                   yac
13968376            Italy         6310      146648.45                   gbc

The column space width between ID and party -> I in ID should start from 1st position and y in party should end at 42nd position), plus the data in the ID and party column should align as mentioned above.

The column space width between ID and test, t in test should end at 93rd position

The column space width between ID and MRK, K in MRK should end at 114th position)

The column space width between ID and Bundle ID -> D in Bundle ID should end at 134th position

The column space width between ID and Entity -> y in Entity should end at 151st position

The data in ID column should be left aligned and for the other columns it should be right aligned.

Can you help me to generate the python code to reflect the data in the above txt format? Thanks a lot for your help

enter image description here

Dataframe: Required txt Format enter image description here

CodePudding user response:

The C %-style formatting is discouraged, but it still makes some sense when doing columnar formats like this.

import pandas as pd
columns = ["ID","party","test","MRK","Bundle","Entity"]
data = [
    [ 16069518, "Australia", 4538,4508329.38,"","abc"],
    [ 15907047, "India", 7595,-24646.59, "","cbd"],
    [ 1890070, "Canada", 3481,21556268.87,"","xyz"],
    [ 10978227, "America", 7595,-258747.70 ,"","yac"],
    [ 13968376, "Italy", 6310,146648.45, "","gbc"],
]

df = pd.DataFrame(data, columns=columns)
print(df)

fmth = "%-10s2sPs!s ss"
fmt  = "%-10d2sPd!.2f ss"

with open('outfile.txt','w') as out:
    print(fmth % tuple(columns), file=out)
    for n,row in df.iterrows():
        print(fmt % (row['ID'],row['party'],row['test'],row['MRK'],row['Bundle'],row['Entity']), file=out)

Output:

         ID      party  test          MRK Bundle Entity
0  16069518  Australia  4538   4508329.38           abc
1  15907047      India  7595    -24646.59           cbd
2   1890070     Canada  3481  21556268.87           xyz
3  10978227    America  7595   -258747.70           yac
4  13968376      Italy  6310    146648.45           gbc
ID                                   party                                              test                  MRK              Bundle           Entity
16069518                         Australia                                              4538           4508329.38                                  abc
15907047                             India                                              7595            -24646.59                                  cbd
1890070                             Canada                                              3481          21556268.87                                  xyz
10978227                           America                                              7595           -258747.70                                  yac
13968376                             Italy                                              6310            146648.45                                  gbc

CodePudding user response:

Thank you Tim, below is the script to transfer the data to the txt file

fmth = "%-10s2sPs!s ss"
fmt  = "%-10s2sPs!s ss"
columns = ["ID","party","test","MRK","Bundle","Entity"]
column=fmth % tuple(columns)
text_file = open(r"C:\data.txt", "w")
text_file.write(column)
text_file.close()
for n,row in df_decomposed.iterrows():
    cleanedList = fmt % (row['ID'], row['party'], row['test'], row['MRK'], row['Bundle'], row['Entity'])
    with open(r"C:\data.txt", "a ") as file_object:
         file_object.seek(0)
         data = file_object.read(100)
         if len(data) > 0 :
             file_object.write("\n")
         file_object.write(cleanedList)
  • Related