check column has the same string

lucky1928

I would like to check if a dataframe column has the same string. for example, below dataframe in columns AAA, I want to check if all string is Jack, so the function call should return true!

import pandas as pd

data = {
    'AAA' :
    ['Jack','Jack','Jack','Jack','Jack'],
    'BBB' :
    ['January', 'February', 'February', 'April', 'January'],
    'CCC' :
    [85, 96, 55, 64,60]
}

df = pd.DataFrame(data)

print(len(df['AAA'].unique()) == 1)

unique can be used to check value is the same, but I need to check the string is Jack as well.

Reza K Ghazi

You can use the following modification:

data = {
    'AAA' :
    ['Jack','Jack','Jack','Jack','Jack'],
    'BBB' :
    ['January', 'February', 'February', 'April', 'January'],
    'CCC' :
    [85, 96, 55, 64,60]
}

df = pd.DataFrame(data)

is_all_jack = len(df['AAA'].unique()) == 1 and df['AAA'].unique()[0] == 'Jack'
print(is_all_jack)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Check if there is a similar string in the same column

Check column if has the same value on all the rows

How to Check if String Has the same characters in Python

Check if Column Has String Object Then Convert to Numeric

How to check if a string has same characters in same order, but other string has extra characters in Java

Check if string in a column, then return value from another column at the same index

How can I check if a string has multiple matching groups that are the same?

Check if string in one column is contained in string of another column in the same row and add new column with matching column name

How to check if Pandas column has value from list of string?

Check if column containing json string has specific value

Same column in dynamic object list has string as well as integer

(PHP) How to check column A and column B's value that has same ID simultaneously

Check column and copy if it is the same

Check if a filename has a string in it

Python Pandas: Check if string in one column is contained in string of another column in the same row

python: how to check if string has the same characters / the probability of repeating them is the same

check if rows has the same status

How can I check whether a integer data in dataframe column has same number of digits?

Check if two rows in pandas DataFrame has same set of values regard & regardless of column order

Check column data has spaces in it

Pandas dataframe: Check if regex contained in a column matches a string in another column in the same row

Pandas / Check if a string is in a column

Checking if a column has the same values if the other column has the same value

pivotting in pandas index and column has same column

Create new column with data that has same column

Check if string has substring and remove it?

Check if a string has white space

ksh - check if String has a pattern

Check if a string has a particular format?