Pandas Empty DataFrame with Specific Column Types

Sometimes you would be required to create an empty DataFrame with column-specific types and names, In this article, I will explain how to do this with several examples. In my last article, I have explained Different ways to create pandas DataFrame.

1. Quick Examples

If you are in hurry, below is a quick example.


#Create empty DataFrame with specific column types
df = pd.DataFrame({'Courses': pd.Series(dtype='str'),
                   'Fee': pd.Series(dtype='int'),
                   'Duration': pd.Series(dtype='str'),
                   'Discount': pd.Series(dtype='float')})

2. Empty DataFrame with Specific Column Types

When you create an empty pandas DataFrame with columns, by default it creates all column types as String/object.


import pandas as pd

# Create empty DataFrame
df = pd.DataFrame(columns = ["Courses", "Fee", "Duration","Discount"])
print(df)
# Outputs
#Empty DataFrame
#Columns: [Courses, Fee, Duration, Discount]
#Index: []

print(df.dtypes)
# Outputs
#Courses     object
#Fee         object
#Duration    object
#Discount    object
#dtype: object

To assign specific column types to empty DataFrame, use the below example.


#Create empty DataFrame with specific column types
df = pd.DataFrame({'Courses': pd.Series(dtype='str'),
                   'Fee': pd.Series(dtype='int'),
                   'Duration': pd.Series(dtype='str'),
                   'Discount': pd.Series(dtype='float')})
print(df.dtypes)
# Outputs
#Courses      object
#Fee           int32
#Duration     object
#Discount    float64
#dtype: object

3. Empty DataFrame with Specific Column Types using Numpy

If you are using pandas numpy, use the below approach to get an empty DataFrame with custom column types.


import pandas as pd
import numpy as np
dtypes = np.dtype(
    [
        ("Courses", str),
        ("Fee", int),
        ("Duration", str),
        ("Discount", float),
        ('date',np.datetime64)
    ]
)
df = pd.DataFrame(np.empty(0, dtype=dtypes))
print(df.dtypes)

This yields the same output as above.

Conclusion

In summary, you have learned how to get a DataFrame with column names and specific data types. If you have not assigned the types, by default pandas assign objects to all columns in DataFrame.

Happy Learning !!

You May Also Like

References

NNK

SparkByExamples.com is a Big Data and Spark examples community page, all examples are simple and easy to understand and well tested in our development environment Read more ..

Leave a Reply

Pandas Empty DataFrame with Specific Column Types