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

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

Pandas:將函數(shù)應(yīng)用于行,寫入新列

Pandas:將函數(shù)應(yīng)用于行,寫入新列

素胚勾勒不出你 2021-07-15 14:11:16
將函數(shù)應(yīng)用于數(shù)據(jù)框我目前有以下數(shù)據(jù)框:數(shù)據(jù)url                            visitorshttp://somedomain.com          200000http://someotherdomain.com     150000http://somenewdomain.com       11000對(duì)于數(shù)據(jù)框中的每一行,我喜歡將兩個(gè)函數(shù)應(yīng)用于 url 列,然后將每個(gè)結(jié)果寫入兩個(gè)不同的列“meta”和“content”。職能:def metacrawler(url)    ...    return datadef contentcrawler(url)    ...    return data# Counterprogress = 0環(huán)形for index, row in data.iterrows():    print(str(progress)," out of ",str(len(data)))    print('Starting meta crawling.')    row['meta'] = metacrawler(row["url"])    print('Starting content crawling.')    row['content'] = contentcrawler(row["url"])    print('Complete.')    progress += 1但是,當(dāng)我在幾次迭代后中止該過程時(shí),我發(fā)現(xiàn)沒有數(shù)據(jù)寫入數(shù)據(jù)框中。也沒有創(chuàng)建列。我做錯(cuò)了什么?
查看完整描述

1 回答

?
MMMHUHU

TA貢獻(xiàn)1834條經(jīng)驗(yàn) 獲得超8個(gè)贊

您可以使用該.apply()功能的文檔與result_type='expand'


In [3]: df = pd.DataFrame({'one':[1,2,3,4], 'two':[5,6,7,8]})


In [4]: df.apply(lambda x: (sum(x), sum(x)), axis=1, result_type='expand')

Out[4]:

    0   1

0   6   6

1   8   8

2  10  10

3  12  12


In [5]: df[['new', 'etc']] = df.apply(lambda x: (sum(x), sum(x)), axis=1, result_type='expand')


In [6]: df

Out[6]:

   one  two  new  etc

0    1    5    6    6

1    2    6    8    8

2    3    7   10   10

3    4    8   12   12

編輯:如果要顯示進(jìn)度,請(qǐng)單獨(dú)定義應(yīng)用功能即


def func(row):

    print(row)

    return sum(row), sum(row)



In [3]: df = pd.DataFrame({'one':[1,2,3,4], 'two':[5,6,7,8]})


In [4]: df.apply(func), axis=1, result_type='expand')

Out[4]:

    0   1

0   6   6

1   8   8

2  10  10

3  12  12


In [5]: df[['new', 'etc']] = df.apply(lambda x: (sum(x), sum(x)), axis=1, result_type='expand')


In [6]: df

Out[6]:

   one  two  new  etc

0    1    5    6    6

1    2    6    8    8

2    3    7   10   10

3    4    8   12   12


查看完整回答
反對(duì) 回復(fù) 2021-07-21
  • 1 回答
  • 0 關(guān)注
  • 162 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

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