Pandas Add Timestamp to file name during saving Data Frame in Excel - pandas

While i am saving Data Frames in Excel format ("*.xlsx"), i would like to add time stamp to Excel file name.
How can i add run time time stamp to Data Frame while saving in Excel format while saving file using Pandas? Thank you

You can use datetime module. It's one of the python standard library, you can access it by pd.datetime like below.
import pandas as pd
writer = pd.ExcelWriter('output_{}.xlsx'.format(pd.datetime.today().strftime('%y%m%d-%H%M%S')))
df1.to_excel(writer,'Sheet1')
df2.to_excel(writer,'Sheet2')
writer.save()
And you can check format code table for the detail.

Related

How to maintain mutiindex view in pandas while converting pandas dataframe to csv file?

I have pandas dataframe where mutiindex is there(pic 1) but when i am converting it csv it not showing as multiindex(pic 2)
I am using to_csv() .Is there any parameter i need to pass to get right format?
pic 1:
pic:2
Tried as per suggestion, below is the pic
If you're not bothered about getting a CSV as output the way I do this is by putting the data in an XLSX file.
# Create the workbook to save the data within
workbook = pd.ExcelWriter(xlsx_filename, engine='xlsxwriter')
# Create sheets in excel for data
df.to_excel(workbook, sheet_name='Sheet1')
# save the changes
workbook.save()
Can you try this and see if it formats how you want?
Maybe this can be helpful for you:
pd.DataFrame(df.columns.tolist()).T.to_csv("dataframe.csv", mode="w", header=False, index=False)
df.to_csv("dataframe.csv", mode="a", header=False, index=False)
I guess you are using an older version of pandas. If you are using <0.21.0, there is a tupleize_cols parameter you can play with. If above, just save as to_csv. It will default to each index in one row -

Pandas read into a single column

I am reading from a .txt file into a dataframe using pandas read_csv(). The data is in this form in the txt file (this is a sample):
abcd,efgh,ijklm
qwerty,uoipqwrt
mznxlakspqowieu
As you can see there are different number of commas in each line. But ultimately I want to put each line of the text file in a single column. How can this be achieved?

Not able to read the .xlsx file to pandas dataframe

enter image description here
I have succesfully pulled the required data to a .xlsx worksheet. But when I am going to convert the data to pandas dataframe an error pops out. How to solve the issue?
You could try without using 'b' while opening that file
output = open('test.xlsx','w')

Excel 2016 - Using Get Data to import CSV and format to show in a different column in the worksheet

Currently I have a CSV file that I am trying to import into excel using Get Data, and format to show each value separated in a different column within the excel workbook. I have the CSV with the values separated in the file in such a way as seen below:
VBU|"VBU Name"|"PO Number"|"Customer Order Number"|"Line Number"|"Insert Date/Time"|"Ack Date"|"Confirm Date"|"Company Item Number"|"Vendor Model Number"|"Quantity Ordered"|"Line Status"|"Back Order Reason"|"Cancel Reason"|"Origin State"|"Destination State"|"Tracking Number"|"Expected Delivery Date"|"Expected Ship Date"|"Actual Ship Date"
I am having a hard time separating each value into it's separate column in the excel workbook after importing it using the Get Data - From Text/CSV.
Is there a step I am missing like a custom delimiter for instance, in order to have this imported and formatted in such a way that each column shows the individual value (column 1 = VBU, column 2 = VBU Name, Column 3 = PO Number), etc.? This CSV file, all these values are crammed into the first column when I open the CSV in excel 2016.
Any help would be appreciated.
Nevermind, it was an anomally in one file. Using Get Data -> From File -> From Text/CSV and setting delimiter as | did the trick and I was able to transform it and separate the values into different columns

Missing header values while using to_csv function

I am trying to read excel files using python and writing each sheet's data into separate csv file.
I am able to write the whole data but its just I am unable to get the header values.
Is there any way this can be resolved.
Here is the piece of code I am writing.
xyz.to_csv(f, index=False, header=None)