Python Forum
export dataframe to file.txt
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
export dataframe to file.txt
#1
Dear Community,

I would like to export a dataframe with empty cells, floats and strings to a file.txt. I have tried several functions such as to_csv, np.save and f.write. The best so far was f.write. However, I cannot get format that I want.
I can export without problems the first two lines. The problem is the float part of the dataframe. This is the code that I am using:
import pandas as pd
import numpy as np
X=3
Y=3
# Create matrix with dimensions X,Y
lon=[[-180, -175, -170],[-180, -175, -170],[-180, -175, -170]]

#Create data and lon dataframe
data=pd.DataFrame([['gridtype=','curvilinear'],['gridsize=',X*Y]])
dslon=pd.DataFrame(lon)

#Create a columns of empty values with Y rows 
emptycol=pd.DataFrame([['']]*Y)
#allocate the string 'xvals=' in the first cell
emptycol.iloc[0,0]='xvals='

#Merge dataframe empty col with dslon
horizontal_concat = pd.concat([emptycol, dslon],axis=1)
#Reset columns index
horizontal_concat.columns = range(horizontal_concat.columns.size)
#Merge the rows of horizontal_concat on the bottom of data and reset index
c_concat =pd.concat([data, horizontal_concat], axis=0)
c_concat.index = range(c_concat.index.size)
#Replace Nan values by empty cells
cc=c_concat.replace(np.nan,'')
#Write file into a data.txt 
for j in range(0,5):
 [line]=cc.iloc[[j]].values   
 with open('data.txt', 'a') as f:     
  if j <3:    
   for row in line:
     f.write(str(row))
   f.write('\n')
  else:
   line_h=np.empty((len(line[:])),dtype=object)  
   line_h[:]=line.reshape(1,-1)  
   for row in line_h:
    f.writelines(str(row)+',') 
   f.write('\n')
 f.close() 
I would like to export to this txt format
gridtype=curvilinear             
gridsize= 9      
xvals=  -180,  -175,  -170,    
        -180,  -175,  -170,    
        -180,  -175,  -170,     
Thanks so much in advance for your help.

Kind regards,

PD: I am aware that the code looks dirty. There is problably another way to write the same script, but clear and clean. I am open to new ideas. I would like to improve my python skills step by step.
Reply


Messages In This Thread
export dataframe to file.txt - by dramauh - Apr-18-2022, 05:53 AM
RE: export dataframe to file.txt - by snippsat - Apr-18-2022, 11:01 AM
RE: export dataframe to file.txt - by dramauh - Apr-18-2022, 11:56 AM
RE: export dataframe to file.txt - by snippsat - Apr-18-2022, 06:29 PM
RE: export dataframe to file.txt - by dramauh - Apr-19-2022, 02:23 AM
RE: export dataframe to file.txt - by sarahroxon7 - Apr-21-2022, 01:23 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Export dataframe to xlsx - Error "zipfile.BadZipFile: File is not a zip file" Baggio 10 62,777 Mar-12-2021, 01:02 PM
Last Post: buran
  How to form a dataframe reading separate dictionaries from .txt file? Doug 1 4,288 Nov-09-2020, 09:24 AM
Last Post: PsyPy
  Export Co-ords to CSV file gtbiyb 1 1,898 Sep-19-2019, 07:59 PM
Last Post: j.crater
  How to add a dataframe to an existing excel file wendysling 2 28,258 May-09-2019, 07:00 PM
Last Post: wendysling
  convert images into pixel dataframe into csv file using python synthex 3 17,571 Feb-17-2019, 06:26 AM
Last Post: scidam
  Write specific rows from pandas dataframe to csv file pradeepkumarbe 3 5,592 Oct-18-2018, 09:33 PM
Last Post: volcano63
  access a very large file? As an array or as a dataframe? Angelika 5 5,011 May-18-2017, 08:15 AM
Last Post: Angelika

Forum Jump:

User Panel Messages

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