emma
emma

Reputation: 353

how to remove brackets form column in a dataframe?

I have a dataframe like this :

0     [plantes milieu xxe siècle l grands groupes êt...]
1     [cours moyen âge apparaissent usages botanique...]
2     [naturaliste anglais propose établir nouveau s...]
3     [certain nombre espèces anciennement considéré...]
4     [premières classifications semi phylogénétique...]
5     [classifications modernes prémoléculaires angi...]

And I would like to remove those brackets but trying so failed and display nan values like below :

0     nan
1     nan
2     nan
3     nan
4     nan
5     nan
6     nan

What I tried :

pd_feature['text_AAVN'] = pd_feature['text_AAVN'].str.strip('[]').astype(str)
    print(pd_feature)

I tried others solutions mentionned in stackoverflow but all failed what I expect

0     plantes milieu xxe siècle l grands groupes êt..
1     cours moyen âge apparaissent usages botanique...
2     naturaliste anglais propose établir nouveau s...

Upvotes: 0

Views: 122

Answers (1)

Anurag Dabas
Anurag Dabas

Reputation: 24314

Try via astype():

pd_feature['text_AAVN']=pd_feature['text_AAVN'].astype(str).str.strip('[]')

OR

If column 'text_AAVN' has only 1 list element then you can also do:

pd_feature['text_AAVN']=pd_feature['text_AAVN'].map(lambda x:x[0])

Upvotes: 1

Related Questions