check pandas variable type - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: check pandas variable type (/thread-27590.html) |
check pandas variable type - cools0607 - Jun-12-2020 I am trying to check variable's data type is list or dataframe. When I got type(df_c) is Out[155]: pandas.core.frame.DataFrame. Checking it again by type(df_c) == 'pandas.core.frame.DataFrame', it showed False, not True. <-- I think it should print "True" rather than "False". import pandas as pd c=[1,2,3] df_c=pd.DataFrame(c) type(df_c) Out[155]: pandas.core.frame.DataFrame type(df_c) == 'pandas.core.frame.DataFrame' Out[156]: False RE: check pandas variable type - buran - Jun-12-2020 you compare type of the object with str object - that's why it's false. As per the docs and also PEP8 recommendation, the isinstance() built-in function is recommended for testing the type of an object, because it takes subclasses into account.import pandas as pd spam = [1,2,3] df = pd.DataFrame(spam) # better use isinstance() print(isinstance(df, pd.DataFrame)) print(isinstance(spam, list)) RE: check pandas variable type - cools0607 - Jun-12-2020 Oh! I just got a lesson by isinstance this function. Thank for your assistance. (Jun-12-2020, 08:47 AM)buran Wrote: you compare type of the object with str object - that's why it's false. RE: check pandas variable type - buran - Jun-12-2020 just for brevity, although not recommended using type will work like this print(type(df_c) == pd.DataFrame) |