想象一下,我有一個由一堆串列組成的列。
Column A
List A of userids
List B of userids
List C of userids
List D of userids
我想通過索引訪問串列 A。通常,我會做
df['Column A'][0]
最終結果應該是:
['ELEMENT OF LIST A', 'ELEMENT OF LIST A']
但我得到一個奇怪的鍵錯誤:0。關于我能做什么的任何想法,以便我得到那個特定的最終結果?我想知道,因為我需要根據回圈的索引來訪問串列值。
這里問的是單行的dict輸出。考慮到后續的大小,這是我能做的最好的事情。
{'Steps': {0: '1-Onboarding Retorno'},
'CampaignSource': {0: 'abd-ecomm-sms'},
'UserId': {0: ['[email protected]',
'[email protected]',
'[email protected]']}}
uj5u.com熱心網友回復:
假設你有:
df = pd.DataFrame({'Column A': [['a', 'b', 'c'], ['d', 'e']]}, index=['i', 'j'])
# Column A
# i [a, b, c]
# j [d, e]
這樣做df['Column A'][0]會嘗試訪問0系列的索引,該索引不存在并引發KeyError.
如果要獲取串列的所有第一項(作為系列),請使用:
df['Column A'].str[0]
輸出:
i a
j d
Name: Column A, dtype: object
要在索引i/處獲取串列的第一個元素Column A:
df.loc['i', 'Column A'][0]
# 'a'
對于 的第一個串列的第一個元素Column A:
df['Column A'].iloc[0][0]
# 'a'
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/444543.html
上一篇:替換熊貓中的反引號
