I have 4 data frames, which are df, df1, df2 and df3.I have plotted these data into a scatter plot. To plot trend line, I use the below code found in stackoverflow. However, this only gives the trendline from df1. I would like to plot a trendline which includes the data from df1, df2 and df3. Answer It uses d…
Tag: pandas
Python Pandas: How to get previous dates with a condition [closed]
Closed. This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 2 years ago. Improve this question I have dataset with the following columns: encounterDate FormName PersonID…
Pandas split group into first and last values
I have multiple dataframes with a structure that starts with 10 and then goes from 0 to 10: So far I’ve been using this code to group the dataframe by type: It works fine for types 0-9, but I’d also like to split type 10 into 2 groups to distinguish first and last values instead of having it all i…
Drop duplicated rows based on multiple columns if other column(s) is NaNs in Pandas
Given a test dataset as follows: I would like to drop duplicated rows based on city and district, then drop rows if its quantity is NaN, but if city and district are not duplicated, then even if quantity is NaN, it’s not necessary to drop rows. Code based on link from here: Out: But I want to keep the l…
Converting datetime only to time in pandas
I would like to ask a question regarding converting datetime only to time. I have values ‘Date Created” that include Dates and Times in one column and I would like to create two columns: one with only Date and one with Time. I wrote a code but in Time column I got a default date of 1900-01-01 alon…
Using pct_change on grouped multiindex dataframe with a datestamp
I have a following dataframe df: And I want to know percentage change of users per datestamp device country columns. I tried: But it ignores the grouping and checks it simply row by row. Desired result would look like this: Answer It looks like you want the percent change for each device / country combination…
How to merge two rows of a pandas dataframe depending on a condition in Python?
I have a dataframe : Index 2 and 3 have the same product id’s, hence its the same order, so i am trying to combine the rows into one single row, to get : the final df being : I have tried using df.groupby function : But it throws datatype error Answer The output is:
How can I handle “Reindexing only valid with uniquely valued Index objects”
I have a dataframe of names (df): And a schedule dataframe (df1): I pivoted df1 (df1_pivoted) to put assignments in the df1 columns: I then try to add the names back in, but I can not figure out how to deal with the “Reindexing only valid with uniquely valued Index objects” error. I presume it is …
How to Drop rows in DataFrame by conditions on column values
I created code to drop some rows according to certain condition : but I got this error: AttributeError: ‘NoneType’ object has no attribute ‘head’ Answer Assign df_clean to df2 without the inplace Or leave the inplace without assigning, here df_clean will be your output
Transposing and Creating Flat files Using Pandas
I have the following table For each top level 3, there are sub levels 4,5,6, etc. For some level 3, there are no sub-level. I need to transpose levels and create a meaningful flat file like this using pandas. Answer I’ll probably be lynched for this but since there isn’t any better answer – hope that he…