Help us understand the problem. What is going on with this article?

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

概要

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)

振り返って見ると

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

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした