I have a dataframe with a column of integers that symbolise birthyears. Each row has 20xx or 19xx in it but some rows have only the xx part. What I wanna do is add 19 in front of those numbers with only 2 “elemets” if the integer is bigger than 22(starting from 0), or/and add 20 infront of those t…
Tag: pandas
pandas: append rows to another dataframe under the similar row based on multiple columns
I asked quite a similar question here but was wondering if there is a way to tackle the issue if one has to rely on multiple columns to perform the append. So the dataframes look as follows, so this time, i would like to append the rows from df2 under similar rows in df1 only if the rows are similar
Python – Unable to export sql result to Excel
I want to export sql query result to excel file using Python. I queried the DB and able to retrieve the result set.Currently what im facing is like.Not able write the query result to Excel, Here is my Code, When im running this code,its skipping to the except block.Is there anyway to figure out the issue? …
How do I find first and last value of each day in pandas dataframe
I have a pandas DataFrame like the below: Price Date 25149.570 2/5/2017 14:22 24799.680 2/5/2017 14:22 24799.680 2/5/2017 14:22 14570.000 2/5/2017 14:47 14570.001 2/5/2017 14:47 14570.001 2/5/2017 14:47 14570.000 2/5/2017 15:01 14570.001 2/5/2017 15:01 14570.001 2/5/2017 15:01 14600.000 2/6/2017 17:49 14600.0…
Using groupby on already grouped data in Pandas
I would like to achieve the result below in Python using Pandas. I tried groupby and sum on the id and Group columns using the below: I got the first two columns, but I’m not sure how to get the third column (Overall_Total). How can I do it? Initial data (before grouping) id Group Time 1 a 2 1 a
Get cumulative sum in pandas
Context Datetime Campaign_name Status Open_time 2022-03-15 00:00 Funny_campaign Open 2022-03-15 01:00 Funny_campaign Continue 2022-03-15 02:00 Funny_campaign Continue 2022-03-15 03:00 Funny_campaign Continue 2022-03-15 04:00 Funny_campaign Close 2022-03-15 08:00 Funny_campaign Open 2022-03-15 09:00 Funny_camp…
How to search a dataframe value based on another dataframe value?
I have two DFs: Now I would like to compare the two dfs and put a column ‘True’ in df2 when the color column of df2 is residing in df1. desired output: I came up with the following: However got the following error: And tried the following ValueError: Length of values (5) does not match length of i…
all the possible combinations between the values that have the same ID value
I have an input pd dataframe with two columns, one is the sequence and the second is an ID (it is a number between 1-1000). I want to get all the possible combinations between the sequences that have the same ID. Input: desired output I have been reading into itertools but this only gives me all possible comb…
Pandas Column Split but ignore splitting on specific pattern
I have a Pandas Series containing Several strings Patterns as below: I would like to split and explode it such that the result would be as follows: In order to do the explode I need to first split. However, if I use split(‘,’) that also splits the items between [] which I do not want. I have tried…
Extract data from pandas dataframe columns with duplicate names
I have a dataframe which has duplicate column names: …and want to convert it into two dataframes; one only of “Accepted” columns and other for “Reject” Columns: df1: df2: Tried: … but this only gives the first column matching this name. Answer If select one column with same…