Pandas DataFrames - how do I export list 'X' to a CSV so it appears as a string? The problem is when I open the CSV using Excel it appears in date format.
X=['1-4', '1-5', '2-3', '4-8']
ie. when list 'X' is exported to a CSV and opened with Excel it appears as a date:
I would like list 'X' to appear in Excel as is - that is, not converted it to date format.
Desired output for Excel is:
I have tried the following code - but it throws an error:
import pandas as pd
X=['1-4', '1-5', '2-3', '4-8']
Y=[1,4,3,5]
df=pd.DataFrame(list(zip(X,Y)))
column_names=['A','B']
df.columns=[column_names]
df.A.to_string()
df.to_csv('yyy.csv', mode='a', header=True)
Thankyou
worked fine with me...
maybe the excel or whatever program u use to open the file is casting it... try open it as text file...
Even if Excel reads in date format, when you open in pandas it will come in original format (at least in my case). If someone only wants to save data in csv and work in pandas again, it should be fine.
I also tried doing the 2nd option here (https://www.winhelponline.com/blog/stop-excel-convert-text-to-number-date-format-csv-file/) which transform the data as text. And then saving again. It worked for me.
Related
I´m struggling to open a text file using pandas.
I have this dataset from an experiment and it should be a 87x249 table.
However, when I use the pandas df.read_csv() command I get all the time a table which is 87x1. I tried to change the delimiters but I always get different tables of 87x1.
I tried to open the ascii file in excel and then save it as a csv. Then it worked and I got a nice table. But the point for me is to use the txt file directly.
I'm trying to write the time data into excel with python (I'm using Pandas). When I write time data to excel I have excel number format 'General':
Sample Screenshot
But I need to have the number format as 'Time' - which I have when I paste the data as values manually to the excel file.
Is it possible to do the same with python? If yes how can I do it?
I have tried to change the values into DateTime object but when I write the data it always deletes cells format in excel file
df['starttime'] = pd.to_datetime(df['starttime']).dt.strftime('%I:%M:%S %p')
Have you tried to use Pandas?
Writing Excel Files Using Pandas
We'll be storing the information we'd like to write to an Excel file
in a DataFrame. Using the built-in to_excel() function, we can extract
this information into an Excel file.*
Step 1: install pandas in your py env
pip install pandas
Step 2: let's import the Pandas module:
import pandas as pd
Step 3:
use the to_excel() function to write the contents to a file. The only argument is the file path:
df.to_excel('./states.xlsx')
I have one Python file that helps me pull and organize datetime data from a server into a pandas DataFrame, and then export out into a nice and usable CSV. My second Python script needs to read the CSV and analyze the data. If I do not touch the exported CSV, the analysis can read the CSV and runs smoothly. However, as soon as I try to merge a few CSV files together with Excel/other spreadsheet software, I get a datetime error
ValueError("time data %r does not match format %r" %
ValueError: time data '2019-12-26 23:00' does not match format '%Y-%m-%d %H:%M:%S'
Even though, it is a direct copy/paste and still saved as a CSV. Any guru can provide some insight on this matter?
Pretty sure this is an Excel issue, not a Python problem.
If you load a .csv that has timestamp strings into Excel, Excel recognizes the datetimes - and formats them. This format seems to default to MM.DD.YYYY hh:mm (the date component might be different depending on your locale):
If you save the file in Excel, the seconds are removed in the .csv!
The only procedure that seems to reliably prevent this behavior is to set a specific date/time format for the respective column, e.g. DD.MM.YYYY hh:mm:ss. AFAIK, You'll have to do this manually for each workbook
Or perhaps write a macro. In older Excel versions, I had a PERSONAL.XLSB for that; should still work with newer versions, you'll have to put it in C:\Users\[username]\AppData\Roaming\Microsoft\Excel\XLSTART
you can use .xlsx format instead of .csv, presumably you won't loose format information there
I see from the error that not all your CSV files have the same time format.
Some on them are in [hour:minute] and some in [hour:minute:second].
ValueError: time data '2019-12-26 23:00' does not match format '%Y-%m-%d %H:%M:%S'
Make sure that datetime fields in all of your CSV matched the same format.
All,
I have a script that runs and when it does it saves the dates in a csv file by appending the file every time it runs.
The code shown below
with open('historyWeekly.csv','a',newline='') as f:
writer=csv.writer(f)
writer.writerow([todayScript])
the name of the csv file is 'historyWeekly.csv' and the format of todayScript is '06-05-2018'
The date is saved succesfully. However when I open the csv file I do find that the format has changed to 06/05/2018.
Using python, how can I force the format to stay the same?
or how can I read the csv with the correct format ('06-05-2018' instead of 06/05/2018)
Many Thanks
proposed solution to my question is to read the csv in python and convert the data back to the desired format.
pd.read_csv("path/historyWeekly.csv")
initial data:
Dates
0 06/05/2018
history['Dates']=pd.to_datetime(history['Dates'], format="%m/%d/%Y")
history['Dates']=history['Dates'].dt.strftime('%m-%d-%Y')
Dates
0 06-05-2018
I am not sure I can ask this below question here.
I generated a CSV file. Each CSV line contains a field in this format {digits}-{digits} such as 4-48, 5-62, ...
When I open the CSV file using Microsoft Excel. I got Apr-48 ( for 4-48) and May-62 (for 5-62)
Anyone has any ideas? I expected what I can see in excel is 4-48, 5-62. I already tried to format the column already. I used general format OR Text format but It didn't work.
Thank you!
You can import the csv into Excel from Data > From Text then go through the wizard and import the columns as text (on step 3 of the wizard) and then excel should not try and be "clever" and covert them to a date.