[英]How do I create a .txt file from a pandas dataframe?
I have a dataframe in my jupyter notebook, which follows the below format for ~1500 rows:我的 jupyter 笔记本中有一个 dataframe,它遵循以下格式,大约 1500 行:
BUSINESS CUSTOMER_ID RISK_RATING
PVB 1000033280 HR
SLA 1000064680 LR
I want to output this df as a.txt file, similar to how I have used df.to_csv
.我想将 output 这个 df 作为 a.txt 文件,类似于我使用
df.to_csv
的方式。
I have tried this code:我试过这段代码:
np.savetxt(r'Y:\FILEPATH\df.txt', df.values, fmt='%d', delimiter='\t')
But I am currently getting this error message:但我目前收到此错误消息:
TypeError: Mismatch between array dtype ('object') and format specifier ('%d %d %d %d %d %d %d %d')
I also want to make sure the column headers ( "BUSINESS", "CUSTOMER_ID" & "RISK_RATING"
) are included in the.txt file.我还想确保列标题(
"BUSINESS", "CUSTOMER_ID" & "RISK_RATING"
)包含在 .txt 文件中。
Any help would be much appreciated thank you.任何帮助将不胜感激,谢谢。
You can use df.to_csv and just give a.txt name to your file.您可以使用 df.to_csv 并为您的文件提供 a.txt 名称。 See below:
见下文:
df.to_csv('filename.txt')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.