df
is a dataframe, created using the Faker
library (used to generate datasets).
I want to assess df
, storing the names of columns as string in one list; and their appropriate data types in a second list.
So far, I have:
columns = []
dtypes = []
for col_name, values in df.iteritems():
columns.append(col_name)
print(col_name)
I am stuck on the solution to detecting the data type of a given column. Could be stored as the data type class itself or as string literal.
Note: assessing the entire list df.column.values()
is not necessary, as each instance/ record has to obey the same format Faker provides. Thus, assessing the very first column value suffices here.
My own proposed solution, using df.types
as advised.
The crux of this being str(type(values[0]))[7:-1]
, where I cast the class output as string
and perform slicing
of characters before and after my desired dtype (with single quotes kept).
columns = []
dtypes = []
for col_name, values in df.iteritems():
columns.append(col_name)
print(col_name)
dtypes.append(type(values[0]))
print(str(type(values[0]))[7:-1])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.