第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將兩個數(shù)據(jù)框中的兩列與另一列上的條件進行比較

將兩個數(shù)據(jù)框中的兩列與另一列上的條件進行比較

九州編程 2023-10-11 16:07:40
我有一個多級數(shù)據(jù)框,我想將 column 中的值secret與 column 上的條件進行比較group。如果 group = A,我們允許另一個數(shù)據(jù)幀中的值為空或 na。否則,對于不匹配的,輸出 INVALID。多級數(shù)據(jù)框:        name               secret              group        df1     df2     df1     df2        df1     df2id                       1       Tim     Tim     random  na          A        A2       Tom     Tom     tree                A        A3       Alex    Alex    apple   apple       B        B4       May     May     file    cheese      C        C秘密的預期輸出id     name    secret    group1      Tim       na        A2      Tom                 A3      Alex     apple      B4      May     INVALID     C到目前為止我有:result_df['result'] = multilevel_df.groupby(level=0, axis=0).apply(lambda x: secret_check(x)) #take care of the rest by compare column by columnresult_df = multilevel_df.groupby(level=0, axis=1).apply(lambda x: validate(x))def validate(x):  if x[0] == x[1]:        return x[1]    else:        return 'INVALID'def secret_check(x):    if (x['group'] == 'A' and pd.isnull(['secret']):  #this line is off      return x[1]    elif x[0] == x[1]:      return x[1]   else:      return 'INVALID'
查看完整描述

1 回答

?
守候你守候我

TA貢獻1802條經(jīng)驗 獲得超10個贊

假設(shè)我們有以下數(shù)據(jù)框:


df = pd.DataFrame({0: {0: 1, 1: 2, 2: 3, 3: 4},

?1: {0: 'Tim', 1: 'Tom', 2: 'Alex', 3: 'May'},

?2: {0: 'Tim', 1: 'Tom', 2: 'Alex', 3: 'May'},

?3: {0: 'random', 1: 'tree', 2: 'apple', 3: 'file'},

?4: {0: 'na', 1: '', 2: 'apple', 3: 'cheese'},

?5: {0: 'A', 1: 'A', 2: 'B', 3: 'C'},

?6: {0: 'A', 1: 'A', 2: 'B', 3: 'C'}})

df

df.columns = pd.MultiIndex.from_tuples([('id',''), ('name', 'df1'), ('name', 'df2'),

? ? ? ? ? ? ? ? ? ? ? ? ? ? ('secret', 'df1'), ('secret', 'df2'), ('group', 'df1'), ('group', 'df2')])

df

In[1]:?

? id? name? ? ? ? secret? ? ? ? ?group? ??

? ? ? ?df1? ?df2? ? ?df1? ? ?df2? ?df1 df2

0? 1? ?Tim? ?Tim? random? ? ? na? ? ?A? ?A

1? 2? ?Tom? ?Tom? ? tree? ? ? ? ? ? ?A? ?A

2? 3? Alex? Alex? ?apple? ?apple? ? ?B? ?B

3? 4? ?May? ?May? ? file? cheese? ? ?C? ?C

  1. 您可以用于np.select()根據(jù)條件返回結(jié)果。

  2. .droplevel()退出多索引數(shù)據(jù)框

  3. df.loc[:,~df.columns.duplicated()]刪除重復的列。由于我們將答案設(shè)置為df1列,df2因此不需要列。

df[('secret', 'df1')] = np.select([(df[('group', 'df2')] != 'A') &?

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?(df[('secret', 'df1')] != df[('secret', 'df2')])], #condition 1

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? [df[('secret', 'df1')] + ' > ' + df[('secret', 'df2')]], #result 1

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?df[('secret', 'df2')]) #alterantive if conditions not met

df.columns = df.columns.droplevel(1)

df = df.loc[:,~df.columns.duplicated()]

df

Out[1]:?

? ?id? name? ? ? ? ?secret group

0? ?1? ?Tim? ? ? ? ? ? ?na? ? ?A

1? ?2? ?Tom? ? ? ? ? ? ? ? ? ? A

2? ?3? Alex? ? ? ? ? apple? ? ?B

3? ?4? ?May? file > cheese? ? ?C


查看完整回答
反對 回復 2023-10-11
  • 1 回答
  • 0 關(guān)注
  • 98 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號