I have a dataframe and need to insert rows in the middle based on a condition, if the condition is met, I need to add rows based on previous and next row values. So far I have this But when I’m trying to insert row(last line of code), it is throwing an error IndexError: single positional indexer is out-of-bounds for
Tag: pandas
How to reorder pandas dataframe based off list containing column order
Say I have a dataframe ‘df’ that contains a list of files and their contents: How can I reorder this df if I have ordered lists of how the ‘Field’ column should be ordered? So that the resulting df is re ordered like so (I am not trying to just sort ‘Field’ in reverse alphabetical order, this example is just
Apply if else condition in specific pandas column by location
I am trying to apply a condition to a pandas column by location and am not quite sure how. Here is some sample data: The first line works to subtract 1 in the correct locations, however, the remaining cells become NaN. The second line of code does not work – the error is: Answer Instead of selected the first N
how can I find a date with incorrect Syntax and fix it
I am new to python. I have a dataset I converted it to dataframe. all my dates are objects now. I need to convert them into dates in order to find the age of patients. My dimensions are 3400×14 long. there are date values inside which have incorrect syntax. I cannot find them. is there a way to find them?
Spline interpolation on dataframes by row
I have the following data frame: I am trying to apply a spline interpolation on each row to get the values for 2017 and 2018 using the following code: However, I get the following error: ValueError: Index column must be numeric or datetime type when using spline method other than linear. Try setting a numeric or datetime index column before
Add hours to datetime of rows in a specific time interval
Let it be the following Python Panda DataFrame: Given a date and time range (start and end) and a country_ID, I want to add 2 hours to the rows that are in that range: Example: Answer Try your logic with boolean indexing (date must also be a datetime object, not a string):
Change pandas dataframe content in a function
I’m writing a class that does one hot encoding, but it doesn’t work as I expected. On my main code I have this: The class method is the following: Now, with print(data.columns) I can see that the method works correctly, but when train_x_categorical.head() runs I can’t see the effect of the method applyOneHotEncoding. I don’t understand why this is happening
Using DataFrame cross join throw no common columns to perform merge on
I’d like to create a third column as a result of a cross join between my Columns A and B: They have the following unique values: I’d like to have a df[‘C’] with the combination of all cross joins, thus we should have 6 * 4 = 24 unique values in it: Thus we should have the following: Using this
Confuse why my KNN code is throwing a ValueError
I am using sklearn for KNN regressor: I get this error message: Could someone please explain this? My data is in the hundred thousands for target and the thousands for input. And there is no blanks in the data. Answer Before answering the question, Let me refactor the code. You are using a dataframe so you can index single or
how to count data in a certain column in python(pandas)?
hope you’re doing well . i tried counting green color row after another green colored row in the table below In [1]: df = pd.DataFrame([[green], [red], [red]], columns=[‘A’]) the code i tried to count greengreen: but it didn’t work,hope you can help. note: i’m new to data science Answer You can use: As a one-liner (python ≥ 3.8): example input: