从列列表中的元素获取字典,其中键值对是pandas中的元素数量和元素值



我有一个数据框架

df = pd.DataFrame([["X",["A","B","C","D"]],["Y",["E","F","G"]],["Z",["H","I","J","K","L"]]],columns=["type","id"])
type    id
X   [A, B, C, D]
Y   [E, F, G]
Z   [H, I, J, K, L]

我想要2个新列id_len它告诉id列和id_dict列表中的元素个数。这使得包含列id_len和id的字典成为键值。

预期输出

df_new = pd.DataFrame([["X",["A","B","C","D"],[1,2,3,4],{1:"A",2:"B",3:"C",4:"D"}],["Y",["E","F","G"],[1,2,3],{1:"E",2:"F",3:"G"}],["Z",["H","I","J","K","L"],[1,2,3,4,5],{1:"H",2:"I",3:"J",4:"K",5:"L"}]],columns=["type","id","id_len","id_dict"])

type    id             id_len                    id_dict
X   [A, B, C, D]     [1, 2, 3, 4]      {1: 'A', 2: 'B', 3: 'C', 4: 'D'}
Y   [E, F, G]        [1, 2, 3]         {1: 'E', 2: 'F', 3: 'G'}
Z   [H, I, J, K, L]  [1, 2, 3, 4, 5]   {1: 'H', 2: 'I', 3: 'J', 4: 'K', 5: 'L'}

怎么做?

我希望这对你有帮助:

df.assign(id_len=df['id'].map(lambda x: list(range(1,len(x)+1))), 
id_dict=df['id'].map(lambda x: dict(enumerate(x,1))))
>>>
'''
type                           id            id_len                                  id_dict
0      X         ['A', 'B', 'C', 'D']      [1, 2, 3, 4]         {1: 'A', 2: 'B', 3: 'C', 4: 'D'}
1      Y              ['E', 'F', 'G']         [1, 2, 3]                 {1: 'E', 2: 'F', 3: 'G'}
2      Z    ['H', 'I', 'J', 'K', 'L']   [1, 2, 3, 4, 5] {1: 'H', 2: 'I', 3: 'J', 4: 'K', 5: 'L'}

相关内容

  • 没有找到相关文章

最新更新