I want to see the datatype of all columns stored in my dataframe without iterating over them. What is the way?
To check the data type in pandas DataFrame we can use the “dtype” attribute. The attribute returns a series with the data type of each column. And the column names of the DataFrame are represented as the index of the resultant series object and the corresponding data types are returned as values of the series object.
To select all columns except one column in Pandas DataFrame, we can use df. loc[:, df. columns != <column name>].
You can use the loc and iloc functions to access columns in a Pandas DataFrame. Let's see how. If we wanted to access a certain column in our DataFrame, for example the Grades column, we could simply use the loc function and specify the name of the column in order to retrieve it.
A column in a DataFrame can only have one data type. The data type in a DataFrame's single column can be checked using dtype . Make conscious decisions about how to manage missing data. A DataFrame can be saved to a CSV file using the to_csv function.
10 min to pandas has nice example for DataFrame.dtypes
:
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 (generally, obviously it is string
):
Sample:
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,)
All values have same dtypes
:
print (df.dtypes) dicts object lists object sets object strings object tuples object dtype: object
But type
is different, if need check it by 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
Or first value of 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 love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With