1

I Need to know how many time a string appears in my dataframe, I used the follow sentence:

print(df['Mobile Register' == df.col1].shape[0])

But my problem is, I need to find all registers where contains Mobile Register, because in my data frame this string can be Mobile Register 1, Mobile Register 2, Mobile Register 3, Mobile Register n ...

So I understand my command will not be used, therefore, what a need to do to find the count?

2 Answers 2

1

Use series.str.contains():

df.loc[df['col_name'].str.contains('Mobile Register',na=False)]

This will give you all the rows satisfying the condition.

To find count of a specific column which meets this condition , use:

df.loc[df['col_name'].str.contains('Mobile Register',na=False),'column_name'].count()

If you need count of all columns satisfying this condition:

df.loc[df['col_name'].str.contains('Mobile Register',na=False)].count()
Sign up to request clarification or add additional context in comments.

Comments

0

I don´t know it could be helpful or if it is the best solution, but I found what I want with the follow sentence

names = df.col1.tolist()
count=0
for colname in enumerate(names):
    print(colname[1])
    if ('Mobile Register' in str(colname[1])):
        count=count+1

2 Comments

names = df.col1.tolist() will show me all registers from col1,
I agree with you, I can delete this answer, I thought in this before read about series.str.contains():

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.