簡體   English   中英

將特定的熊貓數據框分為兩個列表

[英]split a specific pandas dataframe into two lists

我有一個這樣的熊貓數據框:

df = pd.DataFrame(data={'a': [True, True, False, True, True, False,False], 'b': range(7)} , index = range(7))

...(索引並不重要),我想獲取一個元組:

([[0, 1], [3, 4]], [[2],[5, 6]])

...或更籠統地說,我想將df拆分為元組,其中第一個元素是b列的值列表,其中a == True,第二個元素是...,其中a == False。 列表中的順序應與數據框中的順序匹配。 什么是最pythonic(或“ pandastic”)方式來獲得此? 我對我的解決方案不滿意。

這是一個方法:

df = pd.DataFrame(data={'a': [True, True, False, True, True, False,False], 'b': range(7)} , index = range(7))

def splitOnDiscontinuities(elements):
    lists = [[]]
    lastE = None
    for e in elements:
        if lastE is None or e == lastE + 1:
            lists[-1].append(e)
        else:
            lists.append([e])
        lastE = e
    return lists

def weirdThing(df, col1, col2):
    trueElements = splitOnDiscontinuities(list(df[col1][df[col2] == True]))
    falseElements = splitOnDiscontinuities(list(df[col1][df[col2] == False]))
    return (trueElements, falseElements)

print weirdThing(df, 'b', 'a')

輸出:

([[0, 1], [3, 4]], [[2], [5, 6]])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM