I’m using the .apply() method in pandas. I get the same results when using axis=0 and axis=1. When using axis=0 I’d expect a series with four elements (indexed A, B, C, D) as a result. Can anyone tell me why the axis argument doesn’t work in this case? I’m adding a reproducible example below. NOTE: I know that this is
Tag: apply
Alternative to apply function in pandas
I would like to execute this simple transformation in a more efficient way. Any ideas? Answer You can use pandas.Series.clip: or numpy.clip:
How to vectorize groupby and apply in pandas?
I’m trying to calculate (x-x.mean()) / (x.std +0.01) on several columns of a dataframe based on groups. My original dataframe is very large. Although I’ve splitted the original file into several chunks and I’m using multiprocessing to run the script on each chunk of the file, but still every chunk of the dataframe is very large and this process never
How to print the current row number when using .apply on DataFrame
I’ve seen this question for R, but not for python. Basically, I have a large DataFrame where I apply a function row-wise. It takes a very long time to run and I hoped to put a print statement to show where I am. I put together an example of what I would like to do. I know an alternative, but
Pandas rolling apply function to entire window dataframe
I want to apply a function to a rolling window. All the answers I saw here are focused on applying to a single row / column, but I would like to apply my function to the entire window. Here is a simplified example: This is df: Take some function to apply to the entire window: In this example, I would
AttributeError: ‘PandasExprVisitor’ object has no attribute ‘visit_Ellipsis’, using pandas eval
I have a series of the form: Note that its elements are strings: I’m trying to use pd.eval to parse this string into a column of lists. This works for this sample data. However, on much larger data (order of 10K), this fails miserably! What am I missing here? Is there something wrong with the function or my data? Answer
Python/Pandas: If Column has multiple values, convert to single row with multiples values in list
In my DataFrame, I have many instances of same AutoNumber having different KeyValue_String. I would like to convert these instances to a single row where the KeyValue_String is a list comprised of the multiple unique values. The desired output would look like this, except I want to keep all of the other columns Answer If I understand correctly, you could
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.
Python pandas calculate rolling stock beta using rolling apply to groupby object in vectorized fashion
I have a large data frame, df, containing 4 columns: etc. I am attempting to calculate a common financial measure, known as beta, using a function, that takes two of the columns, ret_1m, the monthly stock_return, and ret_1m_mkt, the market 1 month return for the same period (period_id). I want to apply a function (calc_beta) to calculate the 12-month result
Python pandas apply function if a column value is not NULL
I have a dataframe (in Python 2.7, pandas 0.15.0): I want to apply a simple function for rows that does not contain NULL values in a specific column. My function is as simple as possible: And my apply code is the following: It works perfectly. If I want to check column ‘B’ for NULL values the pd.notnull() works perfectly as