shift

Frequency rolling count with groupby, Pandas

Frequency rolling count with groupby, Pandas Question: I’m trying to get the frequency count of a groupby which is grouped by name and date. I am having trouble figuring out how to do a 3 days roll count prior to the current day. example: on 2022-01-05, John’s 3 days range are 2022-01-05 and 2022-01-01 with …

Total answers: 1

Conditional Shift in Pandas [calculate last win date]

Conditional Shift in Pandas [calculate last win date] Question: Help with conditional shifting in Pandas I have the following dataframe: rng = pd.date_range(‘2015-02-24′, periods=10, freq=’D’) win_list = [‘Won’, ‘Lost’, ‘Won’, ‘Won’, ‘Lost’, ‘Won’, ‘Lost’, ‘Lost’, ‘Lost’, ‘Won’] cust_list = [‘A’, ‘A’, ‘B’, ‘C’, ‘C’, ‘C’, ‘B’, ‘A’, ‘A’, ‘B’] output = [‘2015-02-24’, ‘2015-02-24’, ‘2015-02-26’, ‘2015-02-27’, …

Total answers: 1

Accessing a specific date and got the previous close

Accessing a specific date and got the previous close Question: Here is my dataframe: timestamp open high low close volume 0 2023-01-03 3.5000 3.5972 3.3700 3.500 25231.0 1 2023-01-04 3.5000 3.5500 3.3000 3.505 40815.0 2 2023-01-05 3.5800 3.5800 3.4200 3.450 23833.0 3 2023-01-06 3.4500 3.5400 3.4300 3.500 12298.0 4 2023-01-09 3.5200 4.1500 3.4500 4.150 84826.0 …

Total answers: 1

How to shift a column by 1 year in Python

How to shift a column by 1 year in Python Question: With the python shift function, you are able to offset values by the number of rows. I’m looking to offset values by a specified time, which is 1 year in this case. Here is my sample data frame. The value_py column is what I’m …

Total answers: 1

Groupby and shift in pandas dataframe

Groupby and shift in pandas dataframe Question: Suppose I have a data frame Sym C O R 01.01.2020 AAPL 100 115 0.2 01.01.2020 AA 200 205 0.4 02.01.2020 AAPL 101 116 0.3 02.01.2020 AA 201 206 0.2 02.01.2020 MM 298 300 0.5 03.01.2020 AAPL 110 105 0.3 03.01.2020 AA 203 204 0.1 03.01.2020 MM 301 …

Total answers: 3

Rename a column if there is duplicate based on value in another column

Rename a column if there is duplicate based on value in another column Question: I have a dataframe like the following: df = pd.DataFrame({"Col1": ["AA", "AB", "AA", "CC", "FF"], "Col2": [18, 23, 13, 33, 48], "Col3": [17, 27, 22, 37, 52]}) My goal is if there are duplicated values in Col1, I would then sort …

Total answers: 2

How to do a Circular left shift for a byte in python

How to do a Circular left shift for a byte in python Question: I am trying to do a left-byte shift on a list. But it is not working properly. def left_byte_shift(w3): temp = w3 x=0 a = (len(w3)-1) print("a=",a) for i in reversed(range(len(w3))): if i == a: #3 w3[x] = temp[a] else: #2,1,0 print(w3[i],temp[i+1]) …

Total answers: 2

Sum two columns in a grouped data frame using shift()

Sum two columns in a grouped data frame using shift() Question: I have a data frame df where I would like to create new column ID which is a diagonal combination of two other columns ID1 & ID2. This is the data frame: import pandas as pd df = pd.DataFrame({‘Employee’:[5,5,5,20,20], ‘Department’:[4,4,4,6,6], ‘ID’:[‘AB’,’CD’,’EF’,’XY’,’AA’], ‘ID2’:[‘CD’,’EF’,’GH’,’AA’,’ZW’]},) This is …

Total answers: 2