I have a dataframe that discretize the customers into different Q’s, which looks like: What I want to do is adding a new column, Q, to the dataframe which shows which sector this customer is in, so it looks like: The only way I can think about is using for loop but it will give me a mess. Any other
Tag: pandas
iterrows() , if statement and assigning string to new column
I want to add some data to a dataframe to analyse stock price movements. I want to check, if a row is the narrowst range in 7 bars. Unfortunately I seem to be able to run through the df array and perform a basic check. But I can´t assign a string to a column when the If-Statement fulfilled.Any ideas what´s
Merge Dataframe rows based on the date
I have a dataframe that looks like this, It has the name of the company, the date and the title of a headline that was published regarding that company on that day. There are multiple headlines published on that single day and every single one of those headlines take up a different row even for the same date.…
How to extract the the key and values from list of dictionary?
How to extract the the key and values from list of dictionary? Below is my data, i want to extract the key and values from list of dictionary. Answer output
Increasing performance for search pandas df, Count occurrences of starting string grouped by unique identifier
Current dataframe is as follows: Expected Output: Question: How do I get the counts of the ‘starting string’, only when uniqueID is occurring for the first time with increased performance. Thus far, I’m doing with a simple for loop of the data and checking with if/else statements. But this i…
Groupby several columns, summing them up based on the presence of a sub-string
Context: I’m trying to sum all values based in a list only if they start with or contain a string So with a config file like this: And a dataframe like this: How can I group by if they all start by a given substring present on the granularity_suffix_list? Desired output: Attempts: I was trying this: But…
How to discriminate between graphs in python?
I need to discriminate between 2 different graphs in python. Graphs are show below. So, I want to divide these graphs into two categories, A or B. I want to use a property that can be used to discriminate between the two. If the user gives data set for GRAPH A or similar to GRAPH A, the output should be
How to speed up pandas transform function?
I’m trying to speed up or vectorise the following slow code The intent of the code is to remove any values that are beyond the upper standard deviation and replace with NaN. I’ve tried to understand the following article about vectorisation but I can’t manage to apply it How to make your Pan…
Python Upper function
The response I get : The response I expected : What can be the issue here? Answer To get the expected output, consider try this: Your df: to get the dictionary: Block Code:
Filter table and copy to new file in Pandas (VBA-like) dynamically
I have a file (CSV) containing data that looks something like this in a table format: ID employee date value1 value2 1 a 2022-01-01 123 456 2 b 2022-01-01 123 456 3 a 2022-01-01 123 456 4 c 2022-01-01 123 456 5 d 2022-01-01 123 456 6 b 2022-01-01 123 456 7 e 2022-01-01 123 456 8 e 2022-01-01 123