Pandas
DataFrame Creation
Build DataFrames from dicts, lists, and files.
By EZ4Code Team
dataframecreation
Code
import pandas as pd
# From dict of columns
df = pd.DataFrame({
"name": ["Alice", "Bob", "Carol"],
"age": [30, 25, 35],
"score": [85.5, 92.0, 78.5],
})
# From list of dicts
df2 = pd.DataFrame([{"name": "Dan", "age": 40}, {"name": "Eve", "age": 28}])
# From list of lists with explicit columns
df3 = pd.DataFrame([[1, 2], [3, 4]], columns=["a", "b"])
# Inspect
print(df.head(), df.columns, df.dtypes)
print(df.info(), df.describe())Explanation
A DataFrame is a tabular structure that can be built from a dict of columns, a list of row dicts, or a 2D list with column names. head(), info(), and describe() give a quick overview of shape, types, and summary statistics. Columns are pandas Series and can hold mixed dtypes.
More Pandas Snippets
Indexing and Selecting
Select rows and columns with loc, iloc, and masks.
GroupBy Operations
Split, aggregate, and transform with groupby.
Merge and Join
Combine frames with merge and concat.
Pivot Tables
Reshape data with pivot, melt, and pivot_table.
Time Series
Resample, shift, and roll windows over time data.
Missing Data
Detect, fill, and drop NaN values.