Hrihaan
Hrihaan

Reputation: 285

Saving a txt file using Pandas

I used pandas to replace the blank column spaces with nan in a txt file named A_B_1. How can I save this txt file as it is (blank spaces replaced with nan) to some path using pandas? I used the following code to read from it:

import pandas as pd
data= pd.read_table("/Users/Hrihaan/Desktop/Code/A_B_1.txt", sep="\s+", header=None).values

Upvotes: 0

Views: 993

Answers (1)

cs95
cs95

Reputation: 403128

Assuming data is a 2D numpy array, you first convert it to a pandas data frame and then use df.to_csv and pass a tab separator:

pd.DataFrame(data).to_csv('out.csv', sep='\t')

Upvotes: 1

Related Questions