dataframe

Python pandas rolling sum positive number with duplicate timestamps

Python pandas rolling sum positive number with duplicate timestamps Question: have a dataframe with two columns like below. One column is datetime and another is purely numbers. I’d like to sum all positive numbers of last 5 minutes.Tried df[‘positive’] = df[‘number’].rolling(‘5T’).sum() but didn’t work. Somehow, getting a ValueError: window must be an integer 0 or …

Total answers: 2

Duplicate pairs of rows side by side in pandas given certain condition

Duplicate pairs of rows side by side in pandas given certain condition Question: I have the following code: import pandas as pd data = { ‘Col1’: [‘John 1’, ‘John 2’, ‘John 3’, ‘Kyle 1’, ‘Kyle 3’, ‘Kyle 2’], ‘Col2’: [‘B’, ‘C’, ‘E’, ‘F’, ‘F’, ‘S’], ‘Col3’: [‘1’, ‘1’, ‘1’, ‘1’, ‘1’, ‘2’] } df = …

Total answers: 4

Rearrange the columns of a pandas DataFrame based on row number

Rearrange the columns of a pandas DataFrame based on row number Question: There is a dataframe: import pandas as pd df = pd.DataFrame(data = {‘a’:[3,0,2,1],’b’:[4,3,2,1],’c’:[3,2,1,0],’d’:[4,3,2,0]}) print(df) >>> df a b c d 0 3 4 3 4 1 0 3 2 3 2 2 2 1 2 3 1 1 0 0 How to rearrange(sort?) …

Total answers: 3

Iterate through chunks of a pandas Dataframe

Iterate through chunks of a pandas Dataframe Question: I have a pandas.DataFrame that looks like the following: Week Monday Tuesday Wednesday Thursday Friday City A 100 300 x z w City B 200 400 y q p None None None None None None Week Monday Tuesday Wednesday Thursday Friday City A 150 320 a c …

Total answers: 2

Find the min/max in a pandas data column wth nans in between

Find the min/max in a pandas data column wth nans in between Question: I have a pandas data-frame with a column named "Outside Dead Band", the data is recorded at 32Hz (32 data points per second). I want to follow the follwing algorithm. Between 2 nans in the data column, check The duration of no …

Total answers: 1

pandas dataframe remove square brackets from list string when writing to xlsx

pandas dataframe remove square brackets from list string when writing to xlsx Question: First off I have referred to so many posts on this. I am following one of the approaches listed in here to remove the square brackets around Location column when writing to XLSX. But no matter what I try, I can’t get …

Total answers: 1

Pandas apply casts None dtype to object or float depending on other outputs

Pandas apply casts None dtype to object or float depending on other outputs Question: I would like to control the output dtypes for apply on a row. foo and bar below have multiple outputs. import pandas as pd def foo(x): return x[‘a’] * x[‘b’], None, x[‘a’] > x[‘b’] def bar(x): return x[‘a’] * x[‘b’], None …

Total answers: 1

Splitting a column with delimiter and place a value in the right column

Splitting a column with delimiter and place a value in the right column Question: I have a data frame with a column that potentially can be filled with 3 options (a,b, and/or c) with a comma delimiter. import pandas as pd df = pd.DataFrame({‘col1’:[‘a,b,c’, ‘b’, ‘a,c’, ‘b,c’, ‘a,b’]}) I want to split this column based …

Total answers: 3

Solving incompatible dtype warning for pandas DataFrame when setting new column iteratively

Solving incompatible dtype warning for pandas DataFrame when setting new column iteratively Question: Setting the value of a new dataframe column: df.loc[df["Measure] == metric.label, "source_data_url"] = metric.source_data_url now (as of Pandas version 2.1.0) gives a warning, FutureWarning: Setting an item of incompatible dtype is deprecated and will raise in a future error of pandas. Value …

Total answers: 3

setting a multi index does not allow access using .loc?

setting a multi index does not allow access using .loc? Question: import pandas as pd df = pd.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]}) empty_row_index = ("zero_scan") empty_row_df = pd.DataFrame({c: [None] for c in df.columns}) empty_row_df.index = [empty_row_index] df2 = pd.concat([empty_row_df, df])result = df2.loc[df2.index[0], :] works import pandas as pd df = pd.DataFrame({"a": [1, …

Total answers: 2