adikh
adikh

Reputation: 306

single space as a separator in to_csv in pandas dataframe

I want to create a text file from pandas dataframe. Two columns are being extracted from dataframe. I want a single space as a separator in txt file. Here is the code I am using.

merge_df[['hypothesis_transcript','speaker_tag']].to_csv('/path/hypothesis_tag.txt',sep='\t',index= False)
merge_df[['reference_transcript','speaker_tag']].to_csv('/path/reference_tag.txt',sep='\t',index= False)

It generates the output,

en dat is dan meestal dat dus   (recreatie-1731)
de uh   (recreatie-1732)

But I want a single space only,

en dat is dan meestal dat dus (recreatie-1731)
de uh (recreatie-1732)

I used sep = ' ' , which don't give output as expected.

Upvotes: 0

Views: 83

Answers (1)

DumbCoder
DumbCoder

Reputation: 445

You can try merging both columns into one column separated by a space and the writing just that column to the output.

merge_df['Merged_Column'] = merge_df[['hypothesis_transcript','speaker_tag']].astype(str).apply(' '.join, axis=1)

merge_df['Merged_Column'].to_csv('/path/reference_tag.txt',index= False)

Upvotes: 1

Related Questions