import pandas as pd
df = pd.read_csv("https://raw.githubusercontent.com/mwaskom/seaborn-data/master/mpg.csv")
df.head()pandas is a Python library for tables of data. Its DataFrame holds
rows and named columns, like a spreadsheet, and each column has its own
type. People use it to load CSV files, clean and filter data, and
summarise it before charting or modelling. It is built on
NumPy, and df.plot() draws charts with
Matplotlib. This page is an online pandas
compiler: the code runs in your browser, so you can try it without
installing anything.
Run the example first, then paste any snippet below into a new cell to try it.
pd.read_csv() reads a CSV file into a DataFrame, from a file name or
a web address. Here it downloads seaborn's mpg dataset from GitHub:
398 cars from the 1970 to 1982 model years, with 9 columns including
miles per gallon, horsepower, weight and origin. df.head() returns the
first five rows, and because it is the last line of the cell, they are
shown as a table. In a new cell, df.info() lists each column's type
and count of values, which shows that 6 cars have no horsepower, and
df.describe() summarises the numeric columns.
Pass a dictionary to pd.DataFrame(): each key becomes a column name
and each list a column. Arithmetic on columns works row by row:
import pandas as pd
sales = pd.DataFrame({
"region": ["North", "South", "North", "East", "South", "East"],
"product": ["Tea", "Tea", "Coffee", "Coffee", "Coffee", "Tea"],
"units": [120, 80, 95, 60, 150, 40],
"price": [2.5, 2.5, 4.0, 4.0, 4.0, 2.5],
})
sales["revenue"] = sales["units"] * sales["price"]
print(sales.dtypes)
sales
dtypes shows each column's type: int64 for whole numbers, float64
for decimals and object for text.
A comparison on a column gives one True or False per row, and
indexing with it keeps the True rows. This table is built from a list
of rows:
import pandas as pd
rows = [("North", "Tea", 120, 2.5), ("South", "Tea", 80, 2.5),
("North", "Coffee", 95, 4.0), ("East", "Coffee", 60, 4.0),
("South", "Coffee", 150, 4.0), ("East", "Tea", 40, 2.5)]
sales = pd.DataFrame(rows, columns=["region", "product", "units", "price"])
print(sales[sales["units"] > 90])
print(sales[(sales["product"] == "Coffee") & (sales["region"] != "East")])
print(sales.query("units > 50 and price < 3")) # conditions as a string
# .loc[rows, columns] picks rows and columns at once
print(sales.loc[sales["region"].isin(["North", "South"]), ["region", "units"]])
# By product A to Z, then by units, largest first
sales.sort_values(["product", "units"], ascending=[True, False])
groupby() splits the rows by a column's values, and sum() or
mean() reduces each group to one value. agg() computes several named
results at once, and pivot_table() puts one grouping in the rows and
another in the columns:
import pandas as pd
rows = [("North", "Tea", 120, 2.5), ("South", "Tea", 80, 2.5),
("North", "Coffee", 95, 4.0), ("East", "Coffee", 60, 4.0),
("South", "Coffee", 150, 4.0), ("East", "Tea", 40, 2.5)]
sales = pd.DataFrame(rows, columns=["region", "product", "units", "price"])
sales["revenue"] = sales["units"] * sales["price"]
print(sales.groupby("region")["revenue"].sum())
print(sales.groupby("product").agg(
orders=("units", "count"),
total_units=("units", "sum"),
average_price=("price", "mean"),
))
sales.pivot_table(index="region", columns="product", values="units", aggfunc="sum")
to_csv() writes a file, which appears in the sidebar's Data tab, and
read_csv() reads it back. CSV stores dates as text, so pass
parse_dates to get dates back:
import pandas as pd
orders = pd.DataFrame({
"date": ["2024-01-05", "2024-01-19", "2024-02-02", "2024-02-20", "2024-03-11"],
"amount": [250, 120, 310, 90, 180],
})
orders.to_csv("orders.csv", index=False) # index=False: no row numbers
df = pd.read_csv("orders.csv", parse_dates=["date"])
print(df.dtypes)
df["weekday"] = df["date"].dt.day_name()
df["month"] = df["date"].dt.month
print(df)
df.resample("MS", on="date")["amount"].sum() # total per calendar month
Without parse_dates, the column stays text and .dt raises an error.
To open your own CSV file, log in, upload it with Add files in the
Data tab and read it by name.
& (and), | (or) and ~ (not), and put
each one in parentheses. Python's and raises
ValueError: The truth value of a Series is ambiguous..loc on the original:
sales.loc[sales["units"] > 100, "price"] = 3.0. Assigning to a
column of a filtered result such as sales[mask] leaves sales
unchanged and shows a SettingWithCopyWarning.print(df) shows
plain text, and ending a cell with df.head(), df.tail() shows both
as tables.