Sanket Sathe
Sanket Sathe

Reputation: 45

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?

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

Upvotes: 2

Views: 81

Answers (1)

BENY
BENY

Reputation: 323236

You can do

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

Upvotes: 1

Related Questions