Files
training.python.datascience/source/pandas/01-pandas-manual/02-dataframe-demo.py

36 lines
1.3 KiB
Python

"""Define simple DataFrame objects by hand."""
import pandas as pd
if __name__ == '__main__':
# Creating a series with coherent value type
df1 = pd.DataFrame({"age": [25, 45, 65], "prenom": ["Pierre", "Paul", "Jacques"]})
# Get the column names of the dataframe
print("Columns:", df1.columns.tolist())
# Get the number of rows in the dataframe
print(f"Size of the dataframe in rows: {len(df1)}")
# Show the type of the columns
print("Data type of columns (autodetected):")
type_dict = df1.dtypes.to_dict()
for column, dtype in type_dict.items():
print(f"{column:<20} : {str(dtype):<20}")
print("_" * 80)
# Display the contents of the dataframe
print(df1)
# Creating a series with coherent value type
df2 = pd.DataFrame([[25, "Pierre"], [45, "Paul"], [65, "Jacques"]])
# Get the column names of the dataframe
print("Columns:", df2.columns.tolist())
# Get the number of rows in the dataframe
print(f"Size of the dataframe in rows: {len(df2)}")
# Show the type of the columns
print("Data type of columns (autodetected):")
type_dict = df2.dtypes.to_dict()
for column, dtype in type_dict.items():
print(f"{column:<20} : {str(dtype):<20}")
print("_" * 80)
# Display the contents of the dataframe
print(df2)