create a dataframe with column names in python

To create a dataframe with column names in Python, you can use the pandas library. Pandas provides the DataFrame() function, which allows you to create a dataframe with specified column names.

Here's an example of how to create a dataframe with column names:

main.py
import pandas as pd

# Create a dictionary with column names as keys and data as values
data = {'Column1': [1, 2, 3],
        'Column2': ['A', 'B', 'C'],
        'Column3': [True, False, True]}

# Create the dataframe
df = pd.DataFrame(data)

# Print the dataframe
print(df)
275 chars
13 lines

This will output:

main.py
   Column1 Column2  Column3
0        1       A     True
1        2       B    False
2        3       C     True
112 chars
5 lines

In the above code, we first create a dictionary with column names as the keys and data as the values. Then, we pass this dictionary to the DataFrame() function to create the dataframe. Finally, we print the dataframe to see the result.

Note that you need to have the pandas library installed in order to use the DataFrame function. You can install it using pip install pandas if you haven't done so already.

related categories

gistlibby LogSnag