Basley
Basley

Reputation: 89

DataFrame dtypes to list of dicts

I have a very wide dataframe, and I need the dtypes of all the columns as a list of dicts:

[{'name': 'col1', 'type': 'STRING'},...]

I need to do that to supply this as the schema for a BigQuery table.

How could that be achieved?

Upvotes: 0

Views: 308

Answers (1)

Corralien
Corralien

Reputation: 120519

Use a comprehension:

out = [{'name': col, 'type': dtype.name} for col, dtype in df.dtypes.items()]
print(out)

# Output:
[{'name': 'col1', 'type': 'float64'}, {'name': 'col2', 'type': 'float64'}]

Upvotes: 1

Related Questions