I have been trying to plot mean of ‘Sales’ for 7 divisions (blue, green, grey, orange, purple, red, yellow) for each response month. Here is a code snippet: graph image How can I ensure that colour of plot line for each division matches the name of the division i.e. ‘blue’ colour plotl…
Tag: pandas
Converting dataframe to dictionary with country by continent
I have a .csv and dataframe which has 2 columns (country, continent). I want to create a dictionary, carrying the continent as key and a list of all countries as values. The .csv has the following format: country continent Algeria Africa Angola Africa and so on. I tried using: But this gave me the following o…
Elimination of outliers with z-score method in Python
I am cleaning a dataset using the z-score with a threshold >3. Below is the code that I am using. As you can, I first calculate the mean and std. After the code goes in a loop and checks for every value the z-score and if it is greater than 3 and, if yes, the value is treated as an
Plotting multiple dataframes in one chart
In the following code, in each iteration a dataframe is read from a dictionary and is plotted. My intention is see all plots in one chart, but I see multiple charts in separate windows. I see some tutorials about that, e.g. here, but it seems that they work when I want to call df.plot() with different columns…
isin only returning first line from csv
I’m reading from a sqlite3 db into a df: Then I have a symbols.csv file which I want to use to filter the above df: Here’s how I’ve tried to do it: But for some reason I only get the first line returned from the csv: Where am I going wrong here? Answer You need convert csv file to Series,
How to append two rows from dataframe in a single row?
I have dataframe like below: import pandas as pd Output: i want to append two rows into single row by using Gender (both F and M should be in one row). i dont bother on increasing the columns My expected output should be: Any suggestions how to do this would be helpful. Answer Split the dataframe into two dat…
Grouping all the rows with close timestamps in pandas dataframe
I have a df that looks like this, it contains frequencies recorded at some specific time and place. I want to group all the rows which are just 2 seconds apart (like there are 3 rows index 5-7 which have a time difference of just 2 seconds). Similarly, index 8-10 also have the same difference and I want to pl…
Dataframe – for each row, compare values of two columns, get value of third column on match
I have a pandas dataframe in Python that contains a list of different stocks by ticker symbol, and for each one, it also records current price and a low and high price alert threshold value. Below shows a sample of the dataframe: TICKER CURRENT PRICE($) ALERT PRICE HIGH ($) ALERT PRICE LOW ($) AMZN 114 180 10…
Pandas Set Index Based On Column Value
I’m new to pandas, I have a Dataframe read from excel file, like this screenshot below where Products is the header and Google and Meta is the group (index) Using pandas I would like the dataframe to be like this Thank You Answer Using pd.assign and np.where
How to calculate cumulative subtraction with a threshold and reset the subtraction after threshold within groups in pandas dataframe in python?
This is a dataframe, with 4 columns. The primary dataframe contains two columns, trip and timestamps, and I calculated ‘TimeDistance’ which is the difference between rows of timestamps, and ‘cum’ which is the cumulative sum over TimeDistance column. in order to reach my goal, but I cou…