Home > Back-end >  Pandas column sometimes has one value sometimes list. Extract first element
Pandas column sometimes has one value sometimes list. Extract first element

Time:03-03

I have a pandas column that looks like this:

0                      [email protected]
1                             [email protected]
2                                                  NaN
3                                                  NaN
4                                                  NaN
5                                                  NaN
6    [email protected], support@sourcefurni...
7                                                  NaN
8                                                  NaN
9                           [email protected]

I am trying to extract the email if there is only one and the first email if of the list if there is more than one. I can't get it to work:

Here is what I have so far and I don't know why it is giving this error: ValueError: Columns must be same length as key

df1['Alternate_Email_1_Explorium__c'] = df1['Contact email address'].fillna('~')
df1['Alternate_Email_1_Explorium__c'] = df1['Alternate_Email_1_Explorium__c'].str.extract(r'(. )|^(. ?),')

Any help would be appreciated on what I'm doing wrong.

CodePudding user response:

This code should give you find the first email address from your column as long as the e-mails are separated consistently with a ','

df.fillna('~', inplace = True)
df['first_email_list'] = df[0].apply(lambda x : x.split(',')[0])

You would need to simply replace the df[0] with the name of the column that houses the email address you are currently working with and create a new column called 'first_email_list', but you could change that to whatever you would need

  • Related