简体   繁体   English

pandas dataframe 中 to_csv 中的单个空格作为分隔符

[英]single space as a separator in to_csv in pandas dataframe

I want to create a text file from pandas dataframe.我想从 pandas dataframe 创建一个文本文件。 Two columns are being extracted from dataframe.从 dataframe 中提取了两列。 I want a single space as a separator in txt file.我想要一个空格作为 txt 文件中的分隔符。 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,它生成 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.我使用了sep = ' ' ,它没有按预期给出 output 。

You can try merging both columns into one column separated by a space and the writing just that column to the output.您可以尝试将两列合并为一个由空格分隔的列,然后仅将该列写入 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM