I have a dataframe containing a transaction list. What I want to do is create something like a P&L per transaction and additionally make it suitable for entry into another software. Basically, I would like to get a “Buy” transaction and find the next row with a “Sell” transaction. …
Tag: python
Why do Pandas dataframe’s data types change after exporting into a CSV file
I did export the following dataframe in Google Colab. Whichever method I used, when I import it later, my dataframe appears as pandas.core.series.Series, not as an array. After importing the dataframe looks like below Note: The first image and second image can be different order in terms of numbers (It can be…
select non-NaN rows with multiple conditions from a pandas dataframe
Assume there is a dataframe such as I would like to select non-NaN rows based on multiple conditions such as (1) col1 < 4 and (2) non-nan in col2. The following is my code but I have no idea why I did not get the 1st two rows. Any idea? Thanks Answer Because of the operator precedence (bitwise operators, e…
split the string in dataframe in python
I have a data-frame and one of its columns are a string which separated with dash. I want to get the part before the dash. Could you help me with that? The desire output is: Answer You could use str.replace to remove the – and all characters after it: Output:
How do I format a date and also pad it with spaces?
Formatting appears to work differently if the object you’re formatting is a date. returns i.e. it has the padding. If I now do this: then the response is Which is obviously not what I want. I’ve tried various other combinations where I put in the date format as well, but all it does is draw the da…
unit test of function in different directory gives AttributeError: module has no attribute
What specific syntax must be changed below in order for a unit test running in one part of a file system to successfully test a function in a class that is located in a completely different part of a file system? The test file is located at C:pathtosome-test-classestest_an_example.py The class being tested is…
Merging segments from the same trips into a single trip for analysis
In the MWE below, I show my attempt to line-plot trips (from my df aggregated per month): I realised in my df, some trips contains jump (maybe due to data log), so they should be merged into single trip before aggregation. In the given df example above (before grouping). User 154 does undertake 2-trips, not 3…
Python Tkinter widgets not showing when parent is self only root why?
I am making a GUI and this is the same exact code from another project were the widgets show up when self is their parent. But for some reason in this script they only will show if root is set as parent why? The Image Label does not show but the frame does and vice versa if I switch the
How Can I create an event that run a function each time the event is set?
I would like to set up a system with python3 that would launch a function each time an event is called. I would like to rewrite this javascript code in python I wrote tried this code but the function runs only once Answer First I will post the right code to solve your problem and then I will explain. An
Django test with fixtures gives ForeignKeyViolation or IntegrityError
I am trying to write test cases for the Django RestAPI that we have but I have an issue with the fixtures loading. Everything works correctly when I have only one TestCase but when I add a second TestCase in a second django app I get django.db.utils.IntegrityError. My original intention was to create a genera…