Linked Questions
72 questions linked to/from How to apply a function to two columns of Pandas dataframe
44
votes
4
answers
150k
views
How to apply custom function to pandas data frame for each row [duplicate]
I want to apply a custom function and create a derived column called population2050 that is based on two columns already present in my data frame.
import pandas as pd
import sqlite3
conn = sqlite3....
2
votes
4
answers
542
views
How to apply a method to a Pandas Dataframe [duplicate]
I have this dataframe
Col1 Col2
0 A (1000 EUR) C ( 3000 USD)
1 B (2000 CHF) D ( 4000 GBP)
I would like to convert it to
Col1 Col2
0 1000 3000
1 2000 4000
I know how ...
0
votes
3
answers
1k
views
Apply function to two columns and map the output to a new column [duplicate]
I am new to Pandas. Would like to know how to apply a function to two columns in a dataframe and map the output from the function to a new column in the dataframe. Is this at all possible with pandas ...
0
votes
2
answers
260
views
Apply function using multiple Pandas columns? [duplicate]
I need to make a column in my pandas dataframe that relies on other items in that same row. For example, here's my dataframe.
df = pd.DataFrame(
[['a',],['a',1],['a',1],['a',2],['b',2],['...
-1
votes
1
answer
80
views
Unable to get Output of Custom Function [duplicate]
My Data Table is as below
Serial No
Academic
Year
Current year
0
0.0
0
2022
1
0.0
0
2022
2
0.0
0
2022
3
0.0
0
2022
4
0.0
0
2022
...
...
...
...
5298
0.0
0
2019
5299
0.0
0
2019
5300
0.0
0
2019
5301
3.0
...
0
votes
0
answers
81
views
Python create a new column with if condition [duplicate]
I tried to create a new column for my dataset with conditions, however, my jupyter shows the erroe:ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a....
0
votes
0
answers
43
views
Apply function in separate column in Pandas dataframe [duplicate]
I am trying to apply own function into Pandas dataframe. Below you can see my function:
def ssc_function(gross_i1,ssc):
conditions = [
(ssc == 0 ),
(gross_i1 <= gross_i1*0.10)]
...
0
votes
0
answers
40
views
Trying to apply a datetime to a function in Pandas [duplicate]
I have a dataFrame (df) which looks like the follow:
index ticker start_date
0 CRON 2020-10-27
1 DS 2020-10-27
I would like to create a new column return a 3 day return ...
0
votes
0
answers
21
views
How can I correctly use ATR from pandas_ta? [duplicate]
I tried the follow code but It didn't work correctly and it return "None" in the Atr field. I think this isn't a code problem because it doesn't appear an error message.
Can someone help me ...
647
votes
8
answers
1.4m
views
Create new column based on values from other columns / apply a function of multiple columns, row-wise in Pandas
I want to apply my custom function (it uses an if-else ladder) to these six columns (ERI_Hispanic, ERI_AmerInd_AKNatv, ERI_Asian, ERI_Black_Afr.Amer, ERI_HI_PacIsl, ERI_White) in each row of my ...
587
votes
8
answers
998k
views
How can I use the apply() function for a single column?
I have a pandas dataframe with multiple columns. I want to change the values of the only the first column without affecting the other columns. How can I do that using apply() in pandas?
420
votes
10
answers
744k
views
Get column index from column name in python pandas
In R when you need to retrieve a column index based on the name of the column you could do
idx <- which(names(my_data)==my_colum_name)
Is there a way to do the same with pandas dataframes?
156
votes
2
answers
104k
views
Performance of Pandas apply vs np.vectorize to create new column from existing columns
I am using Pandas dataframes and want to create a new column as a function of existing columns. I have not seen a good discussion of the speed difference between df.apply() and np.vectorize(), so I ...
114
votes
3
answers
142k
views
Pandas: create two new columns in a dataframe with values calculated from a pre-existing column
I am working with the pandas library and I want to add two new columns to a dataframe df with n columns (n > 0).
These new columns result from the application of a function to one of the columns in ...
41
votes
7
answers
51k
views
Pandas rolling apply using multiple columns
I am trying to use a pandas.DataFrame.rolling.apply() rolling function on multiple columns.
Python version is 3.7, pandas is 1.0.2.
import pandas as pd
#function to calculate
def masscenter(x):
...