Brainless
Brainless

Reputation: 1758

Create a pandas DataFrame consisting of a single row of types

I have a DataFrame df, consisting of columns Order, PID, subclass, zoning. I got their types using df.dtypes. How do I obtain a DataFrame consisting of the same columns, and a single row consisting of the type of each column?

For example, if the types are:

Order: int64

PID: int64

SubClass: int64

Zoning: object

I want my output DataFrame to be

Order    PID      SubClass   Zoning   <--- column
int64    int64    int64      object   <--- row

I've tried applying pivot or unstack on something like df.dtypes.to_frame().reset_index() (with various arguments), but it didn't work.

Upvotes: 1

Views: 174

Answers (1)

Harpal Singh
Harpal Singh

Reputation: 91

df.dtypes gives you a series object. So all you needed to figure was to create a DataFrame from a series object

Try this It should help

df_types=pd.DataFrame(df.dtypes)
df_types.transpose()

Upvotes: 1

Related Questions