Home > OS >  How to get a value within a list inside a dataframe?
How to get a value within a list inside a dataframe?

Time:12-08

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.

CodePudding user response:

You can do

df['index1] = df['index'].str[0].str['id']
  • Related