This is an almost exact duplicate:
Python, Pandas: write the contents of a DataFrame to a text file
I am again reporting the answer from the quoted SO question with some very small changes to fit this case.
You can use two methods.
np.savetxt()
, in which case you should have something like the following:
np.savetxt('xgboost.txt', a.values, fmt='%d', delimiter="\t", header="X\tY\tZ\tValue")
Assuming a
is a data frame. Of course, you can change the delimiter you want (tab, comma, space, etc.).
Another option mentioned in the answer I attached, and in the answer here from @MYGz, is to use the to_csv method, i.e.:
a.to_csv('xgboost.txt', header=True, index=False, sep='\t', mode='a')
source share