How to Use the Apply() Function For a Single Column

How can I use the apply() function for a single column?

Given a sample dataframe df as:

   a  b
0 1 2
1 2 3
2 3 4
3 4 5

what you want is:

df['a'] = df['a'].apply(lambda x: x + 1)

that returns:

   a  b
0 2 2
1 3 3
2 4 4
3 5 5

Apply function to single column of pandas Dataframe

If it is just a single column, you don't need to use apply. Directly divide the column using its max will do.

discrepancies['Volume'] = discrepancies['Volume'] / discrepancies['Volume'].max()

Apply Function Panda Single Column & Groupby

I've user this data in the following way. (I assume that you've meant CAETITE II instead of CAETITE I) in coefficients table.

df = pd.DataFrame(
data=[
["ARIZONA I", 1991, 1, 6.99],
["ARIZONA I", 1991, 2, 7.00],
["ARIZONA I", 1991, 3, 6.41],
["ARIZONA I", 1991, 4, 6.18],
["CAETITE II",1991, 1, 6.69],
["CAETITE II",1991, 2, 6.21]
],
columns=['plant_name', 'year', 'month', 'wind_speed_ms'])
coeff_df = pd.DataFrame(
data=[
['ARIZONA I', 1.033109, -0.353939],
['CAETITE II', 0.967414, 0.194396],
],
columns=['plant_name', 'slopex', 'intercept']
)

You just need simple merge:

df = df.merge(coeff_df, on='plant_name')
df['adjusted_wind_speed'] = df.wind_speed_ms * df.slopex + df.intercept
df
plant_name year month wind_speed_ms slopex intercept adjusted_wind_speed
0 ARIZONA I 1991 1 6.99 1.033109 -0.353939 6.867493
1 ARIZONA I 1991 2 7.00 1.033109 -0.353939 6.877824
2 ARIZONA I 1991 3 6.41 1.033109 -0.353939 6.268290
3 ARIZONA I 1991 4 6.18 1.033109 -0.353939 6.030675
4 CAETITE II 1991 1 6.69 0.967414 0.194396 6.666396
5 CAETITE II 1991 2 6.21 0.967414 0.194396 6.202037

R Apply() function on specific dataframe columns

Using an example data.frame and example function (just +1 to all values)

A <- function(x) x + 1
wifi <- data.frame(replicate(9,1:4))
wifi

# X1 X2 X3 X4 X5 X6 X7 X8 X9
#1 1 1 1 1 1 1 1 1 1
#2 2 2 2 2 2 2 2 2 2
#3 3 3 3 3 3 3 3 3 3
#4 4 4 4 4 4 4 4 4 4

data.frame(wifi[1:3], apply(wifi[4:9],2, A) )
#or
cbind(wifi[1:3], apply(wifi[4:9],2, A) )

# X1 X2 X3 X4 X5 X6 X7 X8 X9
#1 1 1 1 2 2 2 2 2 2
#2 2 2 2 3 3 3 3 3 3
#3 3 3 3 4 4 4 4 4 4
#4 4 4 4 5 5 5 5 5 5

Or even:

data.frame(wifi[1:3], lapply(wifi[4:9], A) )
#or
cbind(wifi[1:3], lapply(wifi[4:9], A) )

# X1 X2 X3 X4 X5 X6 X7 X8 X9
#1 1 1 1 2 2 2 2 2 2
#2 2 2 2 3 3 3 3 3 3
#3 3 3 3 4 4 4 4 4 4
#4 4 4 4 5 5 5 5 5 5

R - Apply custom function to single column row by row

An example data would be helpful but I think you can try sapply :

df$Child_Date_of_Birth <- sapply(df$Child_Date_of_Birth, format_dates)

How to apply a function over rows within a single column of a pandas data frame?

You have to do what you do, but in reverse:
Let's say your dataframe is this:

d = [{'id': '1', 'address': '4998 Stairstep Lane Toronto ON'}, {'id': '2', 'address': '1234 Stairwell Road Toronto ON'}] 
df = pd.DataFrame(d)
df

id address
0 1 4998 Stairstep Lane Toronto ON
1 2 1234 Stairwell Road Toronto ON

Extract these addresses to a list

address_list = df['address'].tolist()

and then process each with pyapp:

for al in address_list:
addresses = pyap.parse(al, country='CA')
for address in addresses:
print(address)
print(address.as_dict())

Let me know if it works.

pandas DataFrame, how to apply function to a specific column?

The answer is,

df['A'] = df['A'].map(addOne)

and maybe you would be better to know about the difference of map, applymap, apply.

but if you insist to use apply, you could try like below.

def addOne(v):
v['A'] += 1
return v

df.apply(addOne, axis=1)


Related Topics



Leave a reply



Submit