Issue
How do I set a column format for a Pandas data frame, when I am printing it to a file?
Or where can I find documentation on how to do this?
All I am finding is information relating to how to format the DF for visualization, but not in regards of printing the DF in a format.
def print_Report(dataframe, originalFile, newFile):
pageNum = 1
start_row = 0
end_row = 49
max_row = len(dataframe.index)
ln4pad = 123
padding = ' '
#Grabs lines 5-17 of original report file via slice
originalHeader = retrieveHeader(originalFile)
while start_row < max_row:
with open(newFile, 'a') as f:
#Writes Lines 1-4 of header
ln4 = 'Page ' + str(pageNum)
f.write(f'\n1\n\n\n{ln4.rjust(ln4pad, padding)}\n\n'
#Writes Lines 5-17 of header
for item in originalHeader:
f.write(item)
#Writing DF to file
dfAsString = (dataframe.iloc[start_row:end_row]).to_string(index=False, header=False)
f.write(dfAsString)
#Update Values
pageNum += 1
start_row += 49
end_row += 49
if end_row > max_row
end_row = max_row
I know the issue is inserting whitespace at the proper places, for the column formats. But I am just having trouble in finding how to do this.
Desired Format:
EGG HATCH TIME EF EB
NO HR MN SECONDS SCAL DCAL
1 15 50 47.9293 67.1 21.6
2 15 50 48.9824 67.1 21.6
3 15 50 50.9657 67.2 20.4
Currently Printing as:
EGG HATCH TIME EF EB
NO HR MN SECONDS SCAL DCAL
1 15 50 47.9293 67.1 21.6
2 15 50 48.9824 67.1 21.6
3 15 50 50.9657 67.2 20.4
Solution
You could use the col_space
argument to to_string
to align your columns:
dfAsString = (dataframe.iloc[start_row:end_row]).to_string(index=False, header=False, ,col_space=[5,3,2,7,6,5])
The output should then look like:
NO HR MN SECONDS SCAL DCAL
1 15 50 47.9293 67.1 21.6
2 15 50 48.9824 67.1 21.6
3 15 50 50.9657 67.2 20.4
Answered By - Nick
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.