Reputation: 3
I want to fetch data types from dataframe named 'data' which are in 'int64' format. So whenever I am using code(1), it gives me results in boolean format and when I use code(2) I get desired results. Why is it happening?Please explain me the logic behind it
1)data.dtypes=='int64' -->
customer_id True vintage True age True gender False dependents False
2)data.dtypes[data.dtypes=='int64']--->
customer_id int64 vintage int64 age int64
Upvotes: 0
Views: 55
Reputation: 1051
I assume that data
is a Pandas data frame.
The property dtypes
returns a Series containing the data type for each column in data
.
You can learn more about this property here: pandas.DataFrame.dtypes
For example, let us assume your data frame contains two columns, column_1 and column_2, where the values in the first column are floats and the values in the second column are integers and they are 64-bits in length. A Series containing float64 and int64 would be returned when the dtypes
property is invoked on the data frame.
You can verify this by displaying the output of data.dtypes
which will give the following:
float float64
int int64
dtype: object
Now, when you use the ==
operator, you are performing a comparison. You are comparing what is in the Series with the value 'int64'
. The expression will evaluate to True
if the value in the Series matches 'int64'
and False
otherwise. Thus, data.dtypes == ''int64'
will produce the following:
column_1 False
column_2 True
dtype: bool
Where column_1 and column_2 are the names of the columns in the data frame.
We can use the result of data.dtypes == 'int64'
which is a Series containing True
and False
values to filter the original data frame. This is known as boolean indexing since we are using boolean values to select data.
You can learn more about it here: Boolean Indexing (link suggestion by mozway).
Thus, data.dtypes[data.dtypes == 'int64']
will return only those columns where data.dtypes == 'int64'
is True
.
Hence, for the example above you would get the following:
column_2 int64
dtype: object
Upvotes: 1