Skip to content
  • Series & DataFrames · Data Science

Series & DataFrames

LearnViaNotes @learnvianotes· 2 min read

Pandas gives Python two labelled data structures — the one-dimensional Series and the two-dimensional DataFrame — the everyday tools for tabular data.

Series

Python
import numpy as np
import pandas as pd
s = pd.Series(np.array(["g", "e", "e", "k", "s"]))
print(s.tolist(), s.index.tolist())      # ['g', 'e', 'e', 'k', 's'] [0, 1, 2, 3, 4]
prices = pd.Series([40, 25, 60], index=["tea", "milk", "coffee"])
print(prices["coffee"], prices.max())    # 60 60
print(prices[prices > 30].to_dict())     # {'tea': 40, 'coffee': 60}

Creating DataFrames

Python
import pandas as pd
from_rows = pd.DataFrame([["a", 10], ["b", 15], ["c", 14]], columns=["Name", "Age"])
from_dict = pd.DataFrame({"Name": ["Tom", "Jack", "Steve", "Ricky"], "Age": [28, 34, 29, 42]})
print(from_rows.shape, from_dict.shape)          # (3, 2) (4, 2)
print(from_dict)
Output
(3, 2) (4, 2)
    Name  Age
0    Tom   28
1   Jack   34
2  Steve   29
3  Ricky   42
Python
import numpy as np
import pandas as pd
bad = pd.DataFrame(np.array([["a", 10], ["b", 15]]), columns=["Name", "Age"])
good = pd.DataFrame([["a", 10], ["b", 15]], columns=["Name", "Age"])
print(bad["Age"].tolist(), good["Age"].tolist())    # ['10', '15'] [10, 15]

Selecting columns and rows

Python
import pandas as pd
from io import StringIO

csv = """species,size,fragrance,height_cm
rose,medium,mild,48.55
shoeblack plant,medium,mild,147.07
shoeblack plant,medium,none,102.93
hibiscus,large,none,184.00
shoeblack plant,large,mild,83.07
rose,small,strong,35.20
hibiscus,large,,126.69
rose,medium,mild,88.11
"""
df = pd.read_csv(StringIO(csv))       # same call you'd use with a file path

print(df.columns.tolist())             # ['species', 'size', 'fragrance', 'height_cm']
print(type(df["species"]).__name__)    # Series
print(df[["species", "fragrance"]].shape)   # (8, 2)
print(df.loc[3, "species"])            # hibiscus      ← by label
print(df.iloc[0]["species"], df.iloc[0]["height_cm"])   # rose 48.55     ← by position
SyntaxSelects
df["col"]One column → Series
df[["a", "b"]]Several columns → DataFrame
df.loc[row_label, col_label]By labels (slices include the end)
df.iloc[i, j]By integer position (slices exclude the end)
df[df["x"] > 5]Rows matching a condition

Reading and writing files

Python
import pandas as pd
df = pd.DataFrame({"Names": ["Geeks", "For", "Geeks"]}, index=["a", "b", "c"])
df.to_csv("names.csv")                         # writes the index too
back = pd.read_csv("names.csv", index_col=0)   # read it back, using column 0 as the index
print(back.equals(df))                         # True
df.to_csv("names_no_index.csv", index=False)   # usually what you want for exports

Free for students and teachers

Write notes like this one

Cards for every idea, quizzes and flashcards, read aloud — on the web and your phone.

Something wrong with this note? Open it to report