亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

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

循環遍歷 DataFrame 中的行子集

循環遍歷 DataFrame 中的行子集

呼喚遠方 2021-06-11 14:51:56
我嘗試使用函數計算系列中最頻繁的元素來循環 DataFrame 的低谷行。當我手動向其中提供一個系列時,該功能可以完美運行:# Create DataFramedf = pd.DataFrame({'a' : [1, 2, 1, 2, 1, 2, 1, 1],              'b' : [1, 1, 2, 1, 1, 1, 2, 2],              'c' : [1, 2, 2, 1, 2, 2, 2, 1]})# Create function calculating most frequent elementfrom collections import Counterdef freq_value(series):    return Counter(series).most_common()[0][0]# Test function on one rowfreq_value(df.iloc[1])# Another testfreq_value((df.iloc[1, 0], df.iloc[1, 1], df.iloc[1, 2]))通過這兩個測試,我得到了想要的結果。但是,當我嘗試通過 DataFrame 行在循環中應用此函數并將結果保存到新列中時,出現錯誤"'Series' object is not callable", 'occurred at index 0'。產生錯誤的行如下:# Loop trough rows of a dataframe and write the result into new columndf['result'] = df.apply(lambda row: freq_value((row('a'), row('b'), row('c'))), axis = 1)row()在apply()函數中究竟是如何工作的?它不應該freq_value()從“a”、“b”、“c”列提供給我的函數值嗎?
查看完整描述

3 回答

?
一只名叫tom的貓

TA貢獻1906條經驗 獲得超3個贊

row不是您的 函數lambda,因此括號不合適,相反,您應該使用__getitem__方法或loc訪問器來訪問值。前者的語法糖是[]:


df['result'] = df.apply(lambda row: freq_value((row['a'], row['b'], row['c'])), axis=1)

使用loc替代方案:


def freq_value_calc(row):

    return freq_value((row.loc['a'], row.loc['b'], row.loc['c']))

要準確理解為什么會出現這種情況,將您lambda的函數重寫為命名函數會有所幫助:


def freq_value_calc(row):

    print(type(row))  # useful for debugging

    return freq_value((row['a'], row['b'], row['c']))


df['result'] = df.apply(freq_value_calc, axis=1)

運行這個,你會發現它row的類型是<class 'pandas.core.series.Series'>,即如果你使用axis=1. 要訪問給定標簽的系列中的值,您可以使用__getitem__/[]語法或loc.


查看完整回答
反對 回復 2021-06-15
?
眼眸繁星

TA貢獻1873條經驗 獲得超9個贊

您也可以使用df.mode, 和來獲得所需的結果axis=1。這將避免使用apply, 并且仍然會為您提供每行最常見值的列。


df['result'] = df.mode(1)


>>> df

   a  b  c  result

0  1  1  1       1

1  2  1  2       2

2  1  2  2       2

3  2  1  1       1

4  1  1  2       1

5  2  1  2       2

6  1  2  2       2

7  1  2  1       1


查看完整回答
反對 回復 2021-06-15
?
繁花如伊

TA貢獻2012條經驗 獲得超12個贊

df['CommonValue'] = df.apply(lambda x: x.mode()[0], axis = 1)


查看完整回答
反對 回復 2021-06-15
  • 3 回答
  • 0 關注
  • 187 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號