Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)
I wanted to know the data type of each column which is stored in my data frame. How do I code that?

1 Answer

0 votes
by (36.8k points)

df2 = pd.DataFrame({ 

    'A' : 1.,

    'B' : pd.Timestamp('20130102'),

    'C' : pd.Series(1,index=list(range(4)),dtype='float32'),

    'D' : np.array([3] * 4,dtype='int32'),

    'E' : pd.Categorical(["test","train","test","train"]),

    'F' : 'foo' })

print (df2)

     A B C D E F

0 1.0 2013-01-02 1.0 3 test foo

1 1.0 2013-01-02 1.0 3 train foo

2 1.0 2013-01-02 1.0 3 test foo

3 1.0 2013-01-02 1.0 3 train foo

print (df2.dtypes)

A float64

B datetime64[ns]

C float32

D int32

E category

F object

dtype: object

But with dtypes=object it is a bit complicated:

df = pd.DataFrame({'strings':['a','d','f'],

                   'dicts':[{'a':4}, {'c':8}, {'e':9}],

                   'lists':[[4,8],[7,8],[3]],

                   'tuples':[(4,8),(7,8),(3,)],

                   'sets':[set([1,8]), set([7,3]), set([0,1])] })

print (df)

      dicts lists sets strings tuples

0 {'a': 4} [4, 8] {8, 1} a (4, 8)

1 {'c': 8} [7, 8] {3, 7} d (7, 8)

2 {'e': 9} [3] {0, 1} f (3,)

If you see all data types are the same:

print (df.dtypes)

dicts object

lists object

sets object

strings object

tuples object

dtype: object

But the type is different, you can check it in the below loop:

for col in df:

    print (df[col].apply(type))

0 <class 'dict'>

1 <class 'dict'>

2 <class 'dict'>

Name: dicts, dtype: object

0 <class 'list'>

1 <class 'list'>

2 <class 'list'>

Name: lists, dtype: object

0 <class 'set'>

1 <class 'set'>

2 <class 'set'>

Name: sets, dtype: object

0 <class 'str'>

1 <class 'str'>

2 <class 'str'>

Name: strings, dtype: object

0 <class 'tuple'>

1 <class 'tuple'>

2 <class 'tuple'>

Name: tuples, dtype: object

The first value in the columns with iat:

print (type(df['strings'].iat[0]))

<class 'str'>

print (type(df['dicts'].iat[0]))

<class 'dict'>

print (type(df['lists'].iat[0]))

<class 'list'>

print (type(df['tuples'].iat[0]))

<class 'tuple'>

print (type(df['sets'].iat[0]))

<class 'set'>

If you want to know more about the Data Science then do check out the following Data Science which will help you in understanding Data Science from scratch 

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...