how to print a dataframe after groupby in the below format? - python

I have a dataframe in python in the below format
I tried to create a groupby command and the output is like this:
But, I want the output in the below format:
How can I get the output in the above format in python(jupyter notebook)?

Related

Pandas automatically converts string to date

I need to work with a csv file in which one column contains values like these: 1/2, 2/1, 3/1, etc.
When I load the csv into a pandas data frame object, automatically the values look like:01-Feb,02-Jan,03-Jan, etc.
How can I load this csv into a dataframe object in which the values of this columns are converted as strings?
I have tried this
df = pd.read_csv("/Users/Name/Desktop/QM/data.csv", encoding='latin-1',dtype=str)
But the dates remains
it sounds like there is format for that column some way...
anyway, you can just convert back to string following this Pandas Series.dt.strftime

Pandas read CSV datetime column plotting

I’m using Pandas to import data from a csv. One of the columns is datetime. I'm using...
datetime = ['datetime']
df= pd.read_csv(File, parse_dates=datetime)
...to import the datetime column as "datetime64[ns]", which according to...
df.info()
...is in the correct format. However, when i plot against the "datetime" column it looks like this:
When it should be looking like this (this is how it looks when I'm working directly with the same data directly extracted from SQL).
I assume the datetime column is still not quite in the correct dtype (even though it states it is datetime64[ns]).
What should i do to ensure that the 1st plot looks like the 2nd?

How to fix weird date values to datetime type in pandas

I'm new to Python and dataframes.
I have a date value that is not formatted as date. Since this value has a 'weird' format, pandas' function to_datetime() doesn't work properly. The values are formatted like:
['20190630', '20190103']
This is the 'yyyymmdd' format.
I have tried to slice the values and make different columns where I extract the year- month- day. But this doesn't work, since the slicing wasn't working. This is the code I have now, but it isn't doing anything.
df.Date = pd.to_datetime(df.date)
I would like to have the dd-mm-yyyy format and datetime type. What can I do?

How can i convert my date column to datetime?

I have imported some data but the date column is in this format: 50:58.0, 23:11.0.. etc- when i click on the cell in excel however it is: 02/05/2019 07:50:58 (for the first one 50:54.0). So when i import into python as a pandas table it still retains the 50:54.0 format although i do not know why.
I tried changing the column to datetime as:
df['EventTS'] = pd.to_datetime(df['EventTS'], format='%d%b%Y:%H:%M:%S.%f')
but it doesn't work the error is time data '07:27.0' does not match format '%d%b%Y:%H:%M:%S.%f' (match)
without changing format in excel how do i correct this issue in python?

Can the date format of dataframe and csv file be the same?

The two photos that I've attached below show a dataframe table and a table that was exported out to csv file. I'm wondering if there is any command that can modify the date so that the dates shown on both files would be the same.
On the dataframe: 2017-08-01 -> but after exporting out it becomes 2017/8/1(Instead ->2017/08/01).
Does anyone know how it can be done, or do I can only manually edit the cell format?
[
pandas.DataFrame.to_csv
When you make the call to the to_csv function, you can supply it the parameter date_format='%Y-%m-%d'.
Check out the documentation. One of the parameters that you can pass to_csv is date_format which allows you to control the format of your date like columns. The format is the same as for datetime
df.to_csv(file_path, date_format="%Y-%m-%d")
The form YYYY-MM-DD should be the default output date format for to_csv().
It seems like you are opening the output CSV in a program that may be applying its own style/formatting to the dates. Try opening it in a text editor to confirm.
sometimes, the way others answer(date_format) doesn't work although it is a right way.
you should just change your cell format on Excel in that case.
In that case, follow this way:
right click => Format Cell => Category => Custom => Type: yyyy-mm-dd

Categories