Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

You can do

df['index1] = df['index'].str[0].str['id']
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading