Python Forum
Reshape txt file into particular format using python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Reshape txt file into particular format using python
#1
1.I need to convert the following .txt file into csv format using Python. Basically, we need to reshape the wide txt data to long data format, but there doesn't seem to be a straightforward way to do this as my output data needs sourcefile, rownumber, columnnumber and value.

2.Input .txt file look likes this. See attached image

3.Output .csv files looks like the below attached image. In my output csv file, I need SourceFile(name of the file),RowNumber(which row, Value field is coming),ColumnNumber(which column, value field is coming),and Value(actual data).

Here is the code:
for fn in txt_files:
    df2 = pd.read_csv(fn,sep="\t",header=None) #Reading file
    df2 = all_dfs.dropna(axis=1, how='all') #Drop the columns where all columns NaN
    df2 = all_dfs.dropna(axis=0, how='all') #Drop the rows where all columns are NaN
    rs = pd.DataFrame(columns= ['SourceFile','RowNumber','ColNumber','HeaderName','Value'])
    for i,c in enumerate(df2.columns):
        rs = rs.append(pd.DataFrame({'SourceFile': Path(fn.name),'RowNumber': range(1, len(df2) + 1),'ColNumber': i + 1,'HeaderName': c,'Value': df2[[str(c)]].values[:, 0],                                 }))                                                         
 rs['Value'].replace('', np.nan, inplace=True)
 rs.dropna(subset=['Value'], inplace=True)
 rs.to_csv('Data.csv', index=False)
Input File Attached
Any help????

Attached Files

Thumbnail(s)
       

.txt   Inputfile.txt (Size: 21.89 KB / Downloads: 176)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Numpy reshape mr_byte31 1 1,110 Apr-22-2022, 06:33 PM
Last Post: deanhystad
  How can we transcode encoding file uml url format Anldra12 9 3,411 Jul-25-2021, 09:30 AM
Last Post: Anldra12
  How to design a save file format? philipbergwerf 5 4,144 Apr-26-2021, 07:39 PM
Last Post: Gribouillis
  CPC File Format (Cartesian Perceptual Compression) - Can Python Convert / Handle Them PSKrieger 2 2,469 Nov-11-2020, 02:57 PM
Last Post: PSKrieger
  Need help implmenting if/else or case statements for option to choose file format. samlee916 1 2,012 Jul-22-2020, 06:06 PM
Last Post: Larz60+
  copy/pasting in excel WHILE keep file format zarize 0 1,965 Jun-23-2020, 03:51 PM
Last Post: zarize
  Preserve xml file format tanffn 3 3,894 Jan-03-2020, 09:35 AM
Last Post: Larz60+
  Load and format a CSV file fioranosnake 11 4,511 Oct-30-2019, 12:32 PM
Last Post: perfringo
  ValueError: Index contains duplicate entries, cannot reshapeā€¯ error when I try to use Smiling29 11 9,098 Oct-26-2019, 09:52 PM
Last Post: Smiling29
  Please suggest python code to format DNA sequence FASTA file rajamdade 4 3,177 Oct-24-2019, 04:36 AM
Last Post: rajamdade

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020