I need to calculate Total Hours and Hours by Status per Week using Python / Pandas GROUP BY. I can get Total Hours by each Week: But I don’t know how to also group by Status, so it will be 2 additional columns (On Status Hours and Off Status Hours) If I add Status column just to the groupby part,
Tag: dataframe
Processing multiple modes in pandas
I’m obviously dealing with slightly more complex and realistic data, but to showcase my trouble, let’s assume we have these data: I want to find modal values of purchases by date: agg_mode will show that for user_id 100 we have two modal values: [cookies, jam]. This is totally fine with me, when i…
I am getting ValueError: invalid literal for int() with base 10 with np.where function
I want to change ‘not available’ value in a df column into 0, and for the rest of the values to change them into integers. Unique values in the column are: I run the following code to change values to integers: I expect that the above would turn all values into integers, yet I get the value error …
Python pandas group non repeating values
Hi I have a data frame which looks like this I would like to groupby and sum for non repeating values in col1 for e.g. Is there any way I can do this via pandas functions? Answer IIUC, you could create groups using groupby + cumcount (where the nth occurrences of each col1 value will be grouped the same); the…
How to add a new column rank on based on increasing value of other column in Pandas
I have this dataframe with which i am trying to create a new column rank on basis of increasing values of column Opportunity with pandas required output — Answer You can use rank function:
Copy and split row by if cell condition it met – Pandas Python
I am trying to overcome the issue when I have a cell with specific char(‘;’) which I would like to copy the same line with the amount if splitters that specific cell in specific col got. For example: Index Name Age Car 1 David 45 Honda;Subaru 2 Oshir 32 BMW The result that I am trying to get is th…
How to replace all the non-first values of columns with NaN based on date?
I have this dataframe: I want to replace the non-First values of the columns with NaN, for each day. This is how should the dataframe look like: This is what i tried: #i’m trying to use replace, but this does not consider the date Answer groupby + rank First create boolean mask with isna, then use group…
How to map list of string to existing list of integer?
I have this string vocab file: https://drive.google.com/file/d/1mL461QGC5KcA3M1r8AESaPjZ3D_ufgPA/view?usp=sharing. I have this sentences file, made from all vocab file above: https://drive.google.com/file/d/1w5ma4ROjyp6xmZfvnIQjsdH2I_K7lHoo/view?usp=sharing. I want to map every sentences into its correspondin…
DataFrame contains a column of dates which are having these types: “‘5-15-2019′” and 05152021.I want to extract pattern of it
DataFrame contains dates which are having these types: “21-10-2021” and 29052021.I want to extract pattern of it. for example ‘5-15-2019’,it needs to produce ‘%d-%m-%Y’ ‘05152021’ it needs to produce ‘%d%m%Y’ i tried in this way: output: i got a list…
pandas dataframe moving certain headers to index
I have the following dataframe: Desired output: I have tried: The real dictionary is very large with over 30 versions so simply typing out the version numbers into a list is not an option. thanks Answer Try this: Output: