In this post you will learn how to save dataframe as text file.
Working in data analysis as a data scientist you have to be ready for non-standard solutions. Fortunately, Pandas is a powerful Python library and it does a lot. One such task is to save the dataframe in a text format.
How to save dataframe as text file in Pandas
Pandas does not offer a function that saves data in txt format, however, I found a workaround for this problem. Below I will paste two different dataframe solutions in text format.
How to save dataframe as text file using to_string function
The first way to write a dataframe as text is to use the to_string function. I just added the name of the file as a parameter.
import pandas as pd my_df = pd.DataFrame({'id':['id1','id2','id3', 'id2'], 'Column1': ['2', '7', '6', '7'], 'Column2': ['2', '5', '8', '5'], 'Column3': ['4', '1', '9', '1']}) my_df.to_string('my_file.txt')
In the file system I can see that the file has been created in my working folder. You can of course change the location.
How to save dataframe as text file using to_csv function
The second way to save the dataframe as text is to use the to_csv function. This may seem strange to you, but csv is actually a text file. For this reason, the to_csv function is most suitable for saving files in txt format.
As a parameter, I added the name of the file and changed the separator. By default, the to_csv function uses a comma as the separator, so I changed the separator to a tab.
import pandas as pd my_df = pd.DataFrame({'id':['id1','id2','id3', 'id2'], 'Column1': ['2', '7', '6', '7'], 'Column2': ['2', '5', '8', '5'], 'Column3': ['4', '1', '9', '1']}) print(my_df) my_df.to_csv('my_file2.txt', sep='\t')
The file was created the same as in the first case.
Similarly, you can save your file as a TSV file.
import pandas as pd my_df = pd.DataFrame({'id':['id1','id2','id3', 'id2'], 'Column1': ['2', '7', '6', '7'], 'Column2': ['2', '5', '8', '5'], 'Column3': ['4', '1', '9', '1']}) print(my_df) my_df.to_csv('my_file2.tsv', sep="|")
The use of the to_csv and to_string functions is identical. You can choose the one that suits you better. If you have other requirements, please review the documentation for both of these functions to compare the capabilities of both methods.
Pingback: How To Read Csv • Pandas How To
Pingback: How To Save Dataframe As Csv • Pandas How To
Pingback: How To Create A Dataframe • Pandas How To