检查DataFrame或ndrray是否包含数字

rob*_*aro 7 python numpy dataframe pandas

这几个小时我坚持这个:我有一个DataFrame包含一个电子邮件地址列表,从那些我想检查邮件中是否包含数字IE的电子邮件地址roberto123@example.com,如果是,我想要附加这个号码到一个数组:

我已经尝试过使用DataFrame,还有ndarray woth numpy,但它不起作用.这就是我想要做的:

mail_addresses = pd.DataFrame(customers_df.iloc[:,0].values)
mail_addresses = mail_addresses.dropna(axis = 0, how= 'all')
mail_addresses_toArray = mail_addresses.values

for i in mail_addresses:
dates =[]
if any(i.isdigit()) == True:
    dates.append(i)
    print(dates)
Run Code Online (Sandbox Code Playgroud)

我认为我的问题是我不知道如何将此数组中的所有元素转换为字符串,以便该方法isdigit()可以工作并遍历内部的所有元素(825邮件地址).

运行上面的代码时,这是我得到的错误:

AttributeError: 'numpy.int64' object has no attribute 'isdigit'
Run Code Online (Sandbox Code Playgroud)

同时,如果我尝试使用numpy数组(mail_addresses_toArray),这就是错误:

AttributeError: 'numpy.ndarray' object has no attribute 'isdigit'
Run Code Online (Sandbox Code Playgroud)

jez*_*ael 3

extract如果每封邮件仅包含一封numberfindall可能有多封邮件,请使用:

customers_df = pd.DataFrame({'A':['roberto123@example.com','foo123@foo.com',
                                  'bar@bar.com','23re55@re.com'],
                   'B':[4,5,4,5],
                   'C':[7,8,9,4]})

print (customers_df)
                        A  B  C
0  roberto123@example.com  4  7
1          foo123@foo.com  5  8
2             bar@bar.com  4  9
3           23re55@re.com  5  4

L = customers_df.iloc[:,0].str.extract('(\d+)', expand=False).dropna().astype(int).tolist()
print (L)
[123, 123, 23]

L = np.concatenate(customers_df.iloc[:,0].str.findall('(\d+)')).astype(int).tolist()
print (L)
[123, 123, 23, 55]
Run Code Online (Sandbox Code Playgroud)