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

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

如何阻止 apply() 更改列的順序?

如何阻止 apply() 更改列的順序?

慕的地6264312 2022-05-11 15:11:17
我有一個可重現(xiàn)的例子,玩具數(shù)據(jù)框:df = pd.DataFrame({'my_customers':['John','Foo'],'email':['email@gmail.com','othermail@yahoo.com'],'other_column':['yes','no']})print(df)  my_customers                email other_column0         John      email@gmail.com          yes1          Foo  othermail@yahoo.com           no我apply()對行創(chuàng)建了一個函數(shù),在函數(shù)內部創(chuàng)建了一個新列:def func(row):    # if this column is 'yes'    if row['other_column'] == 'yes':        # create a new column with 'Hello' in it                row['new_column'] = 'Hello'         # return to df        return row     # otherwise    else:         # just return the row        return row然后我將該函數(shù)應用于 df,我們可以看到順序已更改。這些列現(xiàn)在按字母順序排列。有沒有辦法避免這種情況?我想保持原來的順序。df = df.apply(func, axis = 1)print(df)                 email my_customers new_column other_column0      email@gmail.com         John      Hello          yes1  othermail@yahoo.com          Foo        NaN           no為澄清而編輯 - 上面的代碼太簡單了輸入df = pd.DataFrame({'my_customers':['John','Foo'],                   'email':['email@gmail.com','othermail@yahoo.com'],                   'api_status':['data found','no data found'],                   'api_response':['huge json','huge json']})  my_customers                email     api_status api_response0         John      email@gmail.com     data found    huge json1          Foo  othermail@yahoo.com  no data found    huge json預期輸出:  my_customers                email     api_status api_response job_1 job_2  \0         John      email@gmail.com     data found    huge json   xyz  xyz2   1          Foo  othermail@yahoo.com  no data found    huge json   nan  nan  education_1  facebook other api info  0         foo  profile1            etc  1         nan  nan                 nan
查看完整描述

2 回答

?
犯罪嫌疑人X

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

DataFrame運行應用功能后,您可以調整列的順序。例如:


df = df.apply(func, axis = 1)

df = df[['my_customers', 'email', 'other_column', 'new_column']]

為了減少重復的數(shù)量(即必須重新輸入所有列名),您可以在調用 apply 函數(shù)之前獲取現(xiàn)有的列集:


columns = list(df.columns)

df = df.apply(func, axis = 1)

df = df[columns + ['new_column']]

根據(jù)作者對原始問題的編輯進行更新。雖然我不確定選擇的數(shù)據(jù)結構(將 API 結果存儲在數(shù)據(jù)框中)是否是最佳選擇,但一種簡單的解決方案可能是在調用應用函數(shù)后提取新列。


# Store the existing columns before calling apply

existing_columns = list(df.columns)


df = df.apply(func, axis = 1)


all_columns = list(df.columns)

new_columns = [column for column in all_columns if column not in existing_columns]


df = df[columns + new_columns]

對于性能優(yōu)化,您可以將現(xiàn)有列存儲在 aset而不是 alist中,由于 Python 中集合數(shù)據(jù)結構的散列性質,這將在恒定時間內產(chǎn)生查找。這將更existing_columns = list(df.columns)改為existing_columns = set(df.columns).


最后,正如@Parfait 在他們的評論中非常友好地指出的那樣,上面的代碼可能會引發(fā)一些折舊警告。使用pandas.DataFrame.reindex而不是df = df[columns + new_columns]將使警告消失:


new_columns_order = [columns + new_columns]

df = df.reindex(columns=new_columns_order)


查看完整回答
反對 回復 2022-05-11
?
汪汪一只貓

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

發(fā)生這種情況是因為您沒有為新列分配值 if row["other_column"] != 'yes'。試試這個:


def func(row):


    if row['other_column'] == 'yes':


        row['new_column'] = 'Hello' 

        return row 


    else: 


        row['new_column'] = '' 

        return row


df.apply(func, axis = 1)

您可以選擇row["new_column"] == 'no'任何值。我只是把它留空。


查看完整回答
反對 回復 2022-05-11
  • 2 回答
  • 0 關注
  • 179 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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