I have two columns, fromdate and todate, in a dataframe. I add a new column, diff, to find the difference between the two dates using I get the diff column, but it contains days, when there’s more than 24 hours. How do I convert my results to only hours and minutes (i.e. days are converted to hours)? Answer Pandas timestamp
Tag: pandas
How to unpack a Series of tuples in Pandas?
Sometimes I end up with a series of tuples/lists when using Pandas. This is common when, for example, doing a group-by and passing a function that has multiple return values: What is the correct way to “unpack” this structure so that I get a DataFrame with two columns? A related question is how I can unpack either this structure or
How to have clusters of stacked bars
So here is how my data set looks like : I want to have stacked bar plot for each dataframe but since they have same index, I’d like to have 2 stacked bars per index. I’ve tried to plot both on the same axes : But it overlaps. Then I tried to concat the two dataset first : but here
Python: Random selection per group
Say that I have a dataframe that looks like: How could I randomly select one (or more) row for each Group_Id? Say that I want one random draw per Group_Id, I would get: Answer
Python: datetime64 issues with range
I am trying to have a vector of seconds between two time intervals: For some reason when I print range I get: So the length is 23401 which is what I want but definitely not the correct time interval. Why is that? Also, if I have a DataFrame df with a column of datetime64 format that looks like: Once I
How can I subclass a Pandas DataFrame?
Subclassing Pandas classes seems a common need, but I could not find references on the subject. (It seems that Pandas developers are still working on it: Easier subclassing #60.) There are some SO questions on the subject, but I am hoping that someone here can provide a more systematic account on the current best way to subclass pandas.DataFrame that satisfies
What does axis in pandas mean?
Here is my code to generate a dataframe: then I got the dataframe: When I type the commmand : I got : According to the reference of pandas, axis=1 stands for columns and I expect the result of the command to be So here is my question: what does axis in pandas mean? Answer It specifies the axis along which
Add x and y labels to a pandas plot
Suppose I have the following code that plots something very simple using pandas: How do I easily set x and y-labels while preserving my ability to use specific colormaps? I noticed that the plot() wrapper for pandas DataFrames doesn’t take any parameters specific for that. Answer In Pandas version 1.10 you can use parameters xlabel and ylabel in the method
How to split a pandas time-series by NAN values
I have a pandas TimeSeries which looks like this: I would like split the pandas TimeSeries everytime there occurs one or more NaN values in a row. The goal is that I have separated events. I could loop through every row but is there also a smart way of doing that??? Answer You can use numpy.split and then filter the
Convert floats to ints in Pandas?
I’ve been working with data imported from a CSV. Pandas changed some columns to float, so now the numbers in these columns get displayed as floating points! However, I need them to be displayed as integers or without comma. Is there a way to convert them to integers or not display the comma? Answer To modify the float output do