137

I have pandas DataFrame like this

        X    Y  Z    Value 
0      18   55  1      70   
1      18   55  2      67 
2      18   57  2      75     
3      18   58  1      35  
4      19   54  2      70   

I want to write this data to a text file that looks like this:

18 55 1 70   
18 55 2 67 
18 57 2 75     
18 58 1 35  
19 54 2 70 

I have tried something like

f = open(writePath, 'a')
f.writelines(['\n', str(data['X']), ' ', str(data['Y']), ' ', str(data['Z']), ' ', str(data['Value'])])
f.close()

It's not correct. How to do this?

Sounak
  • 4,803
  • 7
  • 30
  • 48

8 Answers8

217

You can just use np.savetxt and access the np attribute .values:

np.savetxt(r'c:\data\np.txt', df.values, fmt='%d')

yields:

18 55 1 70
18 55 2 67
18 57 2 75
18 58 1 35
19 54 2 70

or to_csv:

df.to_csv(r'c:\data\pandas.txt', header=None, index=None, sep=' ', mode='a')

Note for np.savetxt you'd have to pass a filehandle that has been created with append mode.

EdChum
  • 376,765
  • 198
  • 813
  • 562
83

The native way to do this is to use df.to_string() :

with open(writePath, 'a') as f:
    dfAsString = df.to_string(header=False, index=False)
    f.write(dfAsString)

Will output the following

18 55 1 70   
18 55 2 67 
18 57 2 75     
18 58 1 35  
19 54 2 70 

This method also lets you easily choose which columns to print with the columns attribute, lets you keep the column, index labels if you wish, and has other attributes for spacing ect.

johnDanger
  • 1,990
  • 16
  • 22
  • 2
    Seems like this should be top answer, doesn't use any additional libraries than user requested (pandas). – Fred Zimmerman Jul 09 '21 at 21:22
  • @FredZimmerman I guess. `df.to_csv()` is usually better than this. And many people prefer working with np for speed reasons, they can make good use of `np.savetxt()`, which is still part of the question since it uses pandas for the Dataframe as a start. – questionto42 Jan 05 '22 at 19:21
  • df.to_csv() would not generalize well to a dataframe that contains strings, as the method will insert quotes everywhere. But np.savetxt() with fmt='%s' will handle strings very well. – yzerman Sep 06 '22 at 04:15
35

You can use pandas.DataFrame.to_csv(), and setting both index and header to False:

In [97]: print df.to_csv(sep=' ', index=False, header=False)
18 55 1 70
18 55 2 67
18 57 2 75
18 58 1 35
19 54 2 70

pandas.DataFrame.to_csv can write to a file directly, for more info you can refer to the docs linked above.

Anzel
  • 19,825
  • 5
  • 51
  • 52
  • 2
    this will run into a lot of trouble when escaping needs to happen, it's not the solution to the general Pandas case! – matanster Aug 11 '18 at 12:45
  • I'm surprised by the amount of upvotes on this. Not something I'd recommend – DanielBell99 Mar 15 '22 at 14:50
  • @StressedBoi_69420 care to elaborate? My answer here is of course not ideal as OP picked the better answer. It would be great if you provide more context, better still you provide an alternative solution -- so everyone can learn from your insight. – Anzel Mar 15 '22 at 18:28
  • To `print(...)` isn't to store to a text file canonically; also your solution seems to use `Python 2`. – DanielBell99 Mar 16 '22 at 09:44
18

Late to the party: Try this>

base_filename = 'Values.txt'
with open(os.path.join(WorkingFolder, base_filename),'w') as outfile:
    df.to_string(outfile)
#Neatly allocate all columns and rows to a .txt file
Nicolas Gervais
  • 33,817
  • 13
  • 115
  • 143
Rene Duchamp
  • 2,429
  • 2
  • 21
  • 29
  • 2
    This doesn't give a tab delimited text file, seems to output a space delimited file. I like the elegance of this code, is there a way to make the output tab delimited? – AHegde Aug 12 '17 at 06:09
  • @AHegde If you use tab delim then it would not have the beautiful formatting? Im just guessing as using space allows for neat alignment of rows/columns with different length. – Simon Chemnitz-Thomsen Sep 05 '22 at 09:04
12

@AHegde - To get the tab delimited output use separator sep='\t'.

For df.to_csv:

df.to_csv(r'c:\data\pandas.txt', header=None, index=None, sep='\t', mode='a')

For np.savetxt:

np.savetxt(r'c:\data\np.txt', df.values, fmt='%d', delimiter='\t')
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Manohar Rana
  • 121
  • 1
  • 4
2

I used a slightly modified version:

with open(file_name, 'w', encoding = 'utf-8') as f:
    for rec_index, rec in df.iterrows():
        f.write(rec['<field>'] + '\n')

I had to write the contents of a dataframe field (that was delimited) as a text file.

Community
  • 1
  • 1
2

Way to get Excel data to text file in tab delimited form. Need to use Pandas as well as xlrd.

import pandas as pd
import xlrd
import os

Path="C:\downloads"
wb = pd.ExcelFile(Path+"\\input.xlsx", engine=None)
sheet2 = pd.read_excel(wb, sheet_name="Sheet1")
Excel_Filter=sheet2[sheet2['Name']=='Test']
Excel_Filter.to_excel("C:\downloads\\output.xlsx", index=None)
wb2=xlrd.open_workbook(Path+"\\output.xlsx")
df=wb2.sheet_by_name("Sheet1")
x=df.nrows
y=df.ncols

for i in range(0,x):
    for j in range(0,y):
        A=str(df.cell_value(i,j))
        f=open(Path+"\\emails.txt", "a")
        f.write(A+"\t")
        f.close()
    f=open(Path+"\\emails.txt", "a")
    f.write("\n")
    f.close()
os.remove(Path+"\\output.xlsx")
print(Excel_Filter)

We need to first generate the xlsx file with filtered data and then convert the information into a text file.

Depending on requirements, we can use \n \t for loops and type of data we want in the text file.

Stidgeon
  • 2,673
  • 8
  • 20
  • 28
1

If you have a Dataframe that is an output of pandas compare method, such a dataframe looks like below when it is printed:

    grossRevenue          netRevenue               defaultCost
             self  other         self         other             self  other
2098        150.0  160.0          NaN           NaN              NaN    NaN
2110       1400.0  400.0          NaN           NaN              NaN    NaN
2127          NaN    NaN          NaN           NaN              0.0  909.0
2137          NaN    NaN     0.000000  8.900000e+01              NaN    NaN
2150          NaN    NaN     0.000000  8.888889e+07              NaN    NaN
2162          NaN    NaN  1815.000039  1.815000e+03              NaN    NaN

I was looking to persist the whole dataframe into a text file as its visible above. Using pandas's to_csv or numpy's savetxt does not achieve this goal. I used plain old print to log the same into a text file:

 with open('file1.txt', mode='w') as file_object:
            print(data_frame, file=file_object)
Binita Bharati
  • 5,239
  • 1
  • 43
  • 24