Reputation: 285
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
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