LoginSignup
0
0

More than 3 years have passed since last update.

pandasのapplyに複数のSeriesを渡したときの要素の取得方法

Last updated at Posted at 2019-04-13

概要

applyに2つのseriesを渡したときに、一つの変数の中に2つのSeriesが
入った?状態になってどう扱えば一意のSeriesから要素を取れるのかということを
試行錯誤したので記事にして忘れても見返せるようにします。

具体的なシチュエーション

列Dの値を列Aに転記したいが、列Dの値がNaNのときは
列Aの値を保持したい。

どうするのか

def overwrite(row):
    if pd.isnull(row['D']):
        return row['A']
    else:
        return row['D']

comp_df['A'] = comp_df[['D', 'A']].apply(overwrite, axis=1)

もしくは

def overwrite(row):
    A, D = row
    if pd.isnull(A):
        return A
    else:
        return D

comp_df['A'] = comp_df[['D', 'A']].apply(overwrite, axis=1)

振り返って見ると

そりゃそうなるよねというコードなので一体どこで詰まってたんだっけ。。。

0
0
0

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
0
0