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.
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.
Comments