I am trying to construct trend following momentum portfolio strategy based on S&P500 index (momthly data) I used Kaufmann’s fractal efficiency ratio to filter out whipsaw signal (http://etfhq.com/blog/2011/02/07/kaufmans-efficiency-ratio/) I succeeded in coding, but it’s very clumsy, so I need advice for better code. Strategy Get data of S&P 500 index from yahoo finance Calculate Kaufmann’s efficiency ratio on lookback period
Tag: pandas
In Pandas, how to delete rows from a Data Frame based on another Data Frame?
I have 2 Data Frames, one named USERS and another named EXCLUDE. Both of them have a field named “email”. Basically, I want to remove every row in USERS that has an email contained in EXCLUDE. How can I do it? Answer You can use boolean indexing and condition with isin, inverting boolean Series is by ~: Another solution with
pandas, apply with args which are dataframe row entries
I have a pandas dataframe ‘df’ with two columns ‘A’ and ‘B’, I have a function with two arguments and I would like to apply it row-by-row to df using the ‘apply’ function but I get the error whats happening here, it seems it takes df[‘A’] as the whole series! not just the row entry from that series as required.
covariance between two columns in pandas groupby pandas
I am trying to calculate the covariance between two columns by group. I am doing doing the following: Ideally, I would like to get the covariance between X and Y and not the whole variance-covariance matrix, since I only have two columns. Thank you, Answer You are almost there, only that you do not clear understand the groupby object, see
How to plot a figure with Chinese Characters in label
When I draw a figure with Chinese Character label in Python 3, it doesn’t work correctly: ] My code: Answer You need to explicitly pass the font properties to legend function using the prop kwag: Source
pandas – Merging on string columns not working (bug?)
I’m trying to do a simple merge between two dataframes. These come from two different SQL tables, where the joining keys are strings: I try to merge them using this: The result of the inner join is empty, which first prompted me that there might not be any entries in the intersection: But when I try to match a single
python pandas: filter out records with null or empty string for a given field
I am trying to filter out records whose field_A is null or empty string in the data frame like below: This gives me error: or This one gave no error but didn’t filter out any None values. I also tried: This one doesn’t give error but doesn’t filter out any None values either. Could anyone please advise how to solve
How can I manage units in pandas data?
I’m trying to figure out if there is a good way to manage units in my pandas data. For example, I have a DataFrame that looks like this: Currently, the measurement units are encoded in column names. Downsides include: column selection is awkward — df[‘width (m)’] vs. df[‘width’] things will likely break if the units of my source data change
Use None instead of np.nan for null values in pandas DataFrame
I have a pandas DataFrame with mixed data types. I would like to replace all null values with None (instead of default np.nan). For some reason, this appears to be nearly impossible. In reality my DataFrame is read in from a csv, but here is a simple DataFrame with mixed data types to illustrate my problem. I can’t do: nor:
How to add custom annotations, from the dataframe, to a stacked bar chart?
I’m plotting a cross-tabulation of various offices within certain categories. I’d like to put together a horizontal stacked bar chart where each office and its value is labeled. Here’s some example code: This gives me a fine starting point: However, what I’d like to have is this: After some research, I came up with the following code that correctly lines