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

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

在循環(huán)中使用pandas .append

在循環(huán)中使用pandas .append

慕容3067478 2019-12-03 10:46:35
我在for循環(huán)內(nèi)將行追加到pandas DataFrame,但最后該數(shù)據(jù)幀始終為空。我不想將行添加到數(shù)組中,然后再調(diào)用DataFrame構(gòu)造函數(shù),因為我的實際for循環(huán)可以處理大量數(shù)據(jù)。我也嘗試pd.concat沒有成功。誰能強調(diào)我缺少使append語句起作用的內(nèi)容?這是一個虛擬的例子:import pandas as pdimport numpy as npdata = pd.DataFrame([])for i in np.arange(0, 4):    if i % 2 == 0:        data.append(pd.DataFrame({'A': i, 'B': i + 1}, index=[0]), ignore_index=True)    else:        data.append(pd.DataFrame({'A': i}, index=[0]), ignore_index=True)print data.head()Empty DataFrameColumns: []Index: [][Finished in 0.676s]
查看完整描述

3 回答

?
呼如林

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

您需要將變量設(shè)置為data等于附加數(shù)據(jù)框。與appendpython list上的方法不同,pandas append不會在原地發(fā)生


import pandas as pd

import numpy as np


data = pd.DataFrame([])


for i in np.arange(0, 4):

    if i % 2 == 0:

        data = data.append(pd.DataFrame({'A': i, 'B': i + 1}, index=[0]), ignore_index=True)

    else:

        data = data.append(pd.DataFrame({'A': i}, index=[0]), ignore_index=True)


print(data.head())


   A    B

0  0  1.0

1  2  3.0

2  3  NaN


查看完整回答
反對 回復(fù) 2019-12-03
?
梵蒂岡之花

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

每次調(diào)用append時,Pandas都會返回原始數(shù)據(jù)框的副本以及新行。這稱為二次復(fù)制,它是一個O(N ^ 2)操作,它將很快變得非常慢(特別是因為您有大量數(shù)據(jù))。


對于您的情況,我建議使用列表,將其追加到列表中,然后調(diào)用數(shù)據(jù)框構(gòu)造函數(shù)。


a_list = []

b_list = []

for data in my_data:

    a, b = process_data(data)

    a_list.append(a)

    b_list.append(b)

df = pd.DataFrame({'A': a_list, 'B': b_list})

del a_list, b_list

時機


%%timeit

data = pd.DataFrame([])

for i in np.arange(0, 10000):

    if i % 2 == 0:

        data = data.append(pd.DataFrame({'A': i, 'B': i + 1}, index=[0]), ignore_index=True)

else:

    data = data.append(pd.DataFrame({'A': i}, index=[0]), ignore_index=True)

1 loops, best of 3: 6.8 s per loop


%%timeit

a_list = []

b_list = []

for i in np.arange(0, 10000):

    if i % 2 == 0:

        a_list.append(i)

        b_list.append(i + 1)

    else:

        a_list.append(i)

        b_list.append(None)

data = pd.DataFrame({'A': a_list, 'B': b_list})

100 loops, best of 3: 8.54 ms per loop


查看完整回答
反對 回復(fù) 2019-12-03
?
一只名叫tom的貓

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

您可以構(gòu)建數(shù)據(jù)框架而無需循環(huán):


n = 4

data = pd.DataFrame({'A': np.arange(n)})

data['B'] = np.NaN

data.loc[data['A'] % 2 == 0, 'B'] = data['A'] + 1

對于:


n = 10000

這有點快:


%%timeit

data = pd.DataFrame({'A': np.arange(n)})

data['B'] = np.NaN

data.loc[data['A'] % 2 == 0, 'B'] = data['A'] + 1


100 loops, best of 3: 3.3 ms per loop


%%timeit

a_list = []

b_list = []

for i in np.arange(n):

    if i % 2 == 0:

        a_list.append(i)

        b_list.append(i + 1)

    else:

        a_list.append(i)

        b_list.append(None)

data1 = pd.DataFrame({'A': a_list, 'B': b_list})


100 loops, best of 3: 12.4 ms per loop


查看完整回答
反對 回復(fù) 2019-12-03
  • 3 回答
  • 0 關(guān)注
  • 2478 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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