If I have a DataFrame as below: Letter Time 0 x 2021-01-01 14:00:00 1 y 2021-01-01 18:00:00 2 y 2021-01-03 14:00:00 How would I delete a row if a value in the Time column(datetime) is within say 14 hours from the time in the row above? I’ve tried using: but I get KeyError 1 in relation to the line if
Tag: pandas
Create a customised pandas dataframe from items in a dictionary
I want to create a customised pandas DataFrame from items in dictionary. For example I can convert a dictionary to a DataFrame using pandas’ from_dict() function: To produce a DataFrame such as below: However what I want is to have only 2 columns, such as below, where the word column returns the diction…
How to Eliminate FOR Loop with the extraction of each value from
Unable to Extract correct values from Pandas Series to use in np.select Function as default value. # With For Loop: Getting Perfect values in column “C” [“No”,”No”,4,4,4,4,4,4] ”’ ”’ Answer You code is equivalent to: output: NB. If you want to keep t…
Creating DataFrame of groups by pixel values in Python (number, size, etc.)
I have the following data (simple representation of black particles on a white filter): And I have counted the number of particles (groups) and assigned them each a number using the following code: With the Output: I then have four (4) particles (groups) of different sizes. I am looking to create a DataFrame …
Pandas Average of row ignoring 0
I have a DataFrame that looks like this: I need to find the mean of each row, ignoring instances of 0. My initial plan was to replace 0 with NaN and then get the mean excluding NaN. I tried to replace 0 with NaN, however this didn’t work, and the DataFrame still contained 0. I tried: The second issue is
How to write a csv file via pandas and read it in R at regular intervals?
Background A driving simulator PC in my lab generates data that I receive via python socket. The data is generated every 1/60th of a second. I continuously save it to a csv file called position.csv. I also want to read position.csv in R to use in a shiny app. I read it every 0.2 seconds. Problem When I run th…
Add pivot table in excel with python
I am trying to add a pivot table in excel using python script with pandas but not able to do so. I won’t to count number of missed and met entries for each priority. Excel script: Excel data: Priority SLA p1 Met p2 Missed p3 Missed p2 Missed p3 Missed desired output: Priority Met Missed p1 1 0 p2 1
how to ignore null values in DataFrame when comparing columns
I am new to Pandas and learning. I am reading excel to DataFrame and comparing columns and highlight the column that’s not same. For example if Column A is not same as Column B then highlight the Column B. However I have some null values in Column A and Column B. When I execute the code, I don’t w…
Try to replace the nan values by pandas , but Error: Columns must be same length as key
It is a simple project in Kaggle, just imitating one blog, but failed. enter image description here train_inf[‘Age’]=train_inf.fillna(train_inf[‘Age’].median()) ValueError: Columns must be same length as key just this code I am searching for a long time on net. But no use. Please help …
Create new columns based on previous columns with multiplication
I want to create a list of columns where the new columns are based on previous columns times 1.5. It will roll until Year 2020. I tried to use previous and current but it didn’t work as expected. How can I make it work as expected? Answer IIUC you can fix you code with: Another, vectorial, approach with…