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 alpha…
Tag: python
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 …
How to implement multiprocessing in my python script?
So the thing I’m trying to do is to have multiple cores process some stuff…. I have a py script which basically makes a feature vector for my ML and DL models. Now the thing I’m doing is that I take a txt file, I compare it with a list that I have initialized at runtime. So the code takes
Increment of every element in list of lists with keeping the shape of the original list [closed]
Closed. This question needs debugging details. It is not currently accepting answers. Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question. Closed 9 months ago. Improve this question I ha…
Problem with running shell script echo in Python
I am trying to run this shell script in Python: So in Python3 I do this: So I expect when I run the python code on Linux or Unix I get: But instead I get: What am I doing wrong? Answer There are two problem with this: This is not how you set a variable in a shell. Instead, the
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…
Unzip multiple password protected files
I’m a newbie in python. I’m trying to write a simple python script to unzip multiple password protected files in a directory. Passwords are stored in a text file. This is what i have come up with, And below is the content of passwords.txt file, Running above code gives this error, I’m not su…
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…
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):
Converting arguments to custom objects using dataclasses package
I recently discovered the dataclasses Python package. I’m running into an issue when using custom classes in my type annotation. I’ve got a simple example below. When the Entry class gets passed the location argument, the value of that argument should be used to construct a Location object. Simila…