I am working with the following dataframe which I created from a much larger csv file with additional information in columns not needed: df_avg_tot_purch = df_purchase_data.groupby([“SN”, “Gender”])[“Price”].agg(lambda x: x.unique().mean()) df_avg_tot_purch.head() This code results in the following: SN Gender Adairialis76 Male 2.28 Adastirin33 Female 4.48 Aeda94 Male 4.91 Aela59 Male 4.32 Aelaria33 Male 1.79 Name: Price, dtype: float64 I now need
Tag: python
How to change the for loop to list comprehension?
In the following code snippet: The output is: I wonder how to change the for loop to a list comprehension? Answer This should work, here is a basic example Would instead be
While loop with missing data handling
I would like to loop over a file where there is an date index and values. Looping backward would help to prevent checking a date where there is no data. So it should stop as it find the first date. I set a today date, change into the appropriate format that correspond to the date in the file, try to
TKinter doesnt execute command based on answer
Introduction Im making custom Dialogue usign TKinter Templates. somewhere’s in the script, there’s MessageBox so users can determine wether its should aborted or continued. Here’s the code : import …
Saving a new list from a a larger list using a loop
I have a large list, which I am pulling out every nth value from the first value up to the mth value to create new lists and I am using a for loop. My question is, how do I create a new list variable each time through the for loop? By way of simple example, I have the list: Which
Search word in word documents and print out the file name that contains that word?
Hey so I am new to Python and I wanted to make a script that retrieves the file name from a list of docx documents in a large directory if a file contains a certain word inside the word document. Here is my code below so far My thought process is to convert all these docx documents to txt files
How do I get a list of all the roles my discord bot has?
I am trying to get a list of all the roles my bot has from top to bottom so I can get the highest role’s color. Answer Not sure what you mean by the “highest role’s color”, also your post is missing your code. One way to get the roles the bot has in the guild (via a command) is
emptying JSON file
I want to completely empty a JSON file, while still keeping the file. How do I do that? I tried: , etc but none of them work. Please help me out! Ive seen answers on how to delete keys in the JSON, or delete the entire file, but not how to just replace it with nothing. Answer You can just
Python change Exception printable output, eg overload __builtins__
I am searching for a way to change the printable output of an Exception to a silly message in order to learn more about python internals (and mess with a friend ;), so far without success. Consider the following code The code shall output name ‘x’ is not defined I would like the change that output to the name ‘x’
Pandas add missing weeks from range to dataframe
I am computing a DataFrame with weekly amounts and now I need to fill it with missing weeks from a provided date range. This is how I’m generating the dataframe with the weekly amounts: Which outputs: If a date range is given as start=’2020-08-30′ and end=’2020-10-30′, then I would expect the following dataframe: So far, I have managed to just