How to get a value within a list inside a dataframe?

Question:

I’ve a dataframe which has a column of list –

print(df)
Index Name
[{‘cadence’: ‘2022-12-07 14:02:28.012’, ‘id’: ‘RJASTD’}] Sarah
[{‘cadence’: ‘2022-12-07 14:02:28.012’, ‘id’: ‘NMRFAS’}] James
print(f"value: {df['Index'][0]}")  # value: [{'cadence': '2022-12-07 14:02:28.012', 'id': 'RJASTD'}]
print(f"length: {len(df['Index'][0])}")  # length: 1
print(f"type: {type(df['Index'][0])}")  # type: <class 'list'>

I want to extract a part of the values from lists and put them in another column –

Index1 Name
RJASTD Sarah
NMRFAS James

How to get such derived column?

I tried with lstrip() and replace() but I must be missing out on something.

Asked By: Sanket Sathe

||

Answers:

You can do

df['index1] = df['index'].str[0].str['id']
Answered By: BENY
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.