I have a df that I’ve read from sql: I then try and get the average of the last 5 days and add it to a new column: Which gives me the following error: Any ideas what’s going wrong here? Previously this worked and now it’s throwing an error – and I can’t seem to figure out why Ans…
Tag: pandas
Python Dataframe For loop (if i.contains)
I need a for loop on a column specific. With this for loop, I will assign the value ‘Normal’ to a list if the column contains ‘Themes’. But I don’t know how to write it here. I would be glad if you help. Thanks in advance :) Dataset For loops that I try Answer You can use np.wher…
how to convert decimal to integer without rounding the decimal in python?
i am having one DataFrame as shown below,i want to convert the decimal number to integer without rounding. Desired output: Answer Option 1 One way to do that is to cast to the type int as follows Option 2 One can also do it with .applymap() as follows Or using custom lambda functions Option 3 Another way is u…
How to assign values to multiple columns using conditions for values from other multiple columns?
Dataset is something like this (there will be duplicate rows in the original): Code: Output should be this: Code: ‘series1′ column values starts row by row as 0, 1, 2, and so on but resets to 0 when: ’email_ID’ column value changes. ‘screen’ column value == ‘rewardapp…
Python Pandas combinations to build the best team
I can’t simplify my data so I put them entirely. I would like to build the best possible team of 11 players according to the “niveau” column. Each “id” has a “niveau” note for the “statut” column. I think it would be necessary to test all the possible comb…
Python: Calculate max profit by day and after current timestamp
I have a dataframe like this: The df contains a price value for every minute of the day starting from 9:30 through 16:00. I am looking to get the maximum possible profit for each minute of the day. I am currently doing something like this: This gives me the percentage of the profit for each row to the highest…
How to output a new dataframe with mismatched columns from two dataframes
I want to have a function that creates a new dataframe from two dataframes. I want to show the mismatched columns based on id number and a given column. dataframes as input: expected output: Answer STEP 1 // add the table name Prefix on column name STEP 2 // Concat both df STEP 3 // Using lambda function find…
Creating a single-column dataframe in Pandas from a Django queryset
I’m trying to create a dataframe containing the values from field_1 and field_2 in a single column. I haven’t used pandas a whole lot before, so I’m sure this is naive. If I’m working with a fairly large dataset, is there a way I can make this more efficient? I would like to eliminate …
Convert the lists in multiple columns to rows in pandas df
I have df with three columns: Col 0 – sentence row num Col 1 – Sentence converted to list Col 2 – list of annotations Col 0 Col1 Col2 1 [This, is, sentence] [l1, l2, l3] 2 [This, is, sentence, too] [l1, l2, l3, l4] I would like to convert Col1 and Col2 and move each row and its respective
Convert nested dictionary to pandas dataframe
I have a nested dictionary as below: I need to convert it into a dataframe like below I have tired the following code from this answer I am getting the dataframe like below This is the closest I got to the desired output. What changes do I need to make to the code to get the desired dataframe? Answer Change