ComplexData
ComplexData

Reputation: 1093

Get all the values that start with a number in a data frame

From a column (company_name) in a data frame df1, I want to get all the values which starts with a number. I am writing the code as -

df2 = df1[df1.company_name.str[0].isdigit()].copy()

I receive the following error -

AttributeError: 'Series' object has no attribute 'is digit'

Upvotes: 1

Views: 402

Answers (1)

jezrael
jezrael

Reputation: 862591

You need add str, because str.isdigit. Also I think copy() is not necessary:

df2 = df1[df1.company_name.str[0].str.isdigit()]

Sample:

import pandas as pd

df1 = pd.DataFrame({'company_name': ['aa','1ss','wer']})
print (df1)
  company_name
0           aa
1          1ss
2          wer

df2 = df1[df1.company_name.str[0].str.isdigit()]
print (df2)
  company_name
1          1ss

Upvotes: 1

Related Questions