Check if a data row is a row

I want to check if a column in a dataframe contains rows. I would have thought it could be done just by checking dtype, but that is not the case. The pandas series containing strings only has a dtype object, which is also used for other data structures (e.g. lists):

df = pd.DataFrame({'a': [1,2,3], 'b': ['Hello', '1', '2'], 'c': [[1],[2],[3]]}) df = pd.DataFrame({'a': [1,2,3], 'b': ['Hello', '1', '2'], 'c': [[1],[2],[3]]}) print(df['a'].dtype) print(df['b'].dtype) print(df['c'].dtype) 

It produces:

 int64 object object 

Is there a way to check if a column contains only rows?

+5
source share
2 answers

You can use this to see if all elements in a column are rows

 df.applymap(type).eq(str).all() a False b True c False dtype: bool 

To check if any lines are

 df.applymap(type).eq(str).any() 
+7
source

You can match the data using a function that converts all elements to True or False if they are equal to str type or not, and then just check if the list contains any False elements

In the example below, a list is checked containing a different item than str. It will tell you True if another type of data is present.

 test = [1, 2, '3'] False in map((lambda x: type(x) == str), test) 

Output: True

0
source

Source: https://habr.com/ru/post/1266271/


All Articles