Python, Pandas:Write Content of Dataframe into Text File

Python, Pandas : write content of DataFrame into text File

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.

data frame to file.txt python

This is an almost exact duplicate of the following:

Python, Pandas : write content of DataFrame into text File

I report again here the answer from the cited SO question with some very small modifications to fit this case.

You can use two methods.

np.savetxt(), in which case you should have something like the following:

np.savetxt('xgboost.txt', a.values, fmt='%d', delimiter="\t", header="X\tY\tZ\tValue")  

assuming a is the dataframe. Of course you can change the delimiter you want (tab, comma, space,etc.).

The other option, as mentioned in the answer I attached and in the answer here from @MYGz, is to use the to_csv method, i.e.:

a.to_csv('xgboost.txt', header=True, index=False, sep='\t', mode='a')

How to export a column from a dataframe to a text file with left alignment in Python Pandas?

If your column you want to write is named COLUMN_NAME, you can do:

with open("output.txt", "w") as f_out:
f_out.write("\n".join(df["COLUMN_NAME"]))

This creates output.txt:

Michael Jordan
Scottie Pippen
Dirk

Format pandas dataframe output into a text file as a table (formatted and aligned to the max length of the data or header (which ever is longer))

Use to_markdown:

out = df.to_markdown(index=False, tablefmt='pipe', colalign=['center']*len(df.columns))
print(out)

# Output:
| ID | TABLE.F1 | O |
|:-----------:|:----------:|:-----:|
| 11404371006 | Y | False |
| 11404371007 | NULL | False |
| 11404371008 | N | False |
| 11404371009 | N | False |
| 11404371010 | N | False |
| 11404371011 | N | False |

To remove the second line:

out = out.split('\n')
out.pop(1)
out = '\n'.join(out)
print(out)

# Output
| ID | TABLE.F1 | O |
| 11404371006 | Y | False |
| 11404371007 | NULL | False |
| 11404371008 | N | False |
| 11404371009 | N | False |
| 11404371010 | N | False |
| 11404371011 | N | False |

Trying to read a text file wtih pandas. Data is text wrapped, can't get pandas to split into seperate columns

Combining all the comments, Pandas .read_csv() can probably load this for you with a few options

  • header=None (no header provided)
  • delimiter=r"\s+" runs of whitespace of any kind are one contiguous delimiter
  • lineterminator=";" data lines don't end at \n, but continue to each ;

I've used an io.StringIO() to directly load from the given text in an interpreter, but this shouldn't be necessary if you have a file you can read from instead (just pass the filename or a reference to it) .. if you find you need to clean up the input data, you can load the file contents as a string and then make a StringIO (for example, I .strip()'d off the newlines I'd added to clean up the input)

>>> import pandas as pd
>>> data = """
... 1 164477000 73449000 0 0 512 306 396173.0 512 10 6 0 0 0 1 10 10 0 N 80.110001
... 219 11.062500 0.069141 0.000000 0.000000 0.000000 0 0.000000 0.000000 0.000000
... 0.000000 0.000000 0.000000 0 NA NA NA NA NA 3.472138 NA 0.678533 NA NA
... NA NA NA 95.849327 NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
... NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
... NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
... NA NA NA NA ;
... 2 200368000 83175000 0 0 318 87 167005.0 318 991 6 0 0 0 1 991 991 0 N 142.179993
... 231 1.125000 0.007031 0.000000 0.000000 0.000000 0 0.000000 0.000000 0.000000
... 0.000000 0.000000 0.000000 0 NA NA NA NA NA NA NA NA NA NA NA NA NA 100.000000
... NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
... NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
... NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA ;
... """.strip() # throw out empty lines added to improve display above
>>> from io import StringIO # make the data block a file-like for this example
>>> df = pd.read_csv(StringIO(data), header=None, delimiter=r"\s+", lineterminator=";")
>>> df
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ... 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
0 1 164477000 73449000 0 0 512 306 396173.0 512 10 6 0 0 0 1 ... NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN \n NaN NaN NaN NaN
1 2 200368000 83175000 0 0 318 87 167005.0 318 991 6 0 0 0 1 ... NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN

[2 rows x 124 columns]

Saving dataframe after text lines in .txt file

You can use to_csv to append new dataframe (tab separated) to an existing file:

file="example.txt" 
f = open(file, "w")
f.write(f"Multiple\nlines\ngo here\n\n")
f.close()
df.to_csv(file, mode='a', index=False, header=True, sep = "\t")

The output of this is:

Multiple
lines
go here

Time (s) X Position
2 7
6 2
9 6
8 8
10 5
12 9

Write each row of pandas dataframe into a new text file - pythonic way

I've written something like this and it works. anyways thanks for your inputs guys

for index, row in p.iterrows():
if i > len(p):
break
else:
f = open(str(i)+'.txt', 'w')
f.write(row[0])
f.close()
i+=1

where p is a dataframe.



Related Topics



Leave a reply



Submit