11
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Pandasで計算式をapplyするときに複数列を参照する

Last updated at Posted at 2022-06-18

(説明不足だったのでタイトル・内容共に修正しました。)

DataFrameのある列に対して、何らかの処理をした値を新しい列に反映したいとき
applyの括弧内にlambda式を書くことがあります。

sample.py
import pandas as pd

df = pd.DataFrame([
    [1,100],
    [2,200],
    [3,300],
    [4,400],
], columns=['A','B'])

df['C'] = df['A'].apply(lambda x: x + 1 if x > 2 else 0)

#   A    B  C
# 0  1  100  0
# 1  2  200  0
# 2  3  300  4
# 3  4  400  5

複数列の値を使って計算したい場合は、DataFrame自体にapplyすることで、
複数列を参照させることができるようになります。
axis=1を忘れずに!

sample.py
df['D'] = df.apply(lambda row: row['A'] + row['B'] if row['A'] > 2 else 0, axis=1)

#   A    B  C    D
# 0  1  100  0    0
# 1  2  200  0    0
# 2  3  300  4  303
# 3  4  400  5  404
11
10
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?