Lesson 7 of 23
โฎPrev Nextโฏ

PY-401 ยท pandas basics

pandas DataFrames

โฑ 8 min ๐Ÿ Real Python 3 ๐ŸŽฌ Animated

7of 23

Your note

My notes for this lesson

Select any text in the lesson to highlight it or add a note. Saved in this browser.

    No highlights yet. Select a sentence in the lesson, then pick a colour or add a note.

    All my bookmarks and notes

    Reading settings

    For every lesson, saved in this browser.

    Text size
    Line spacing
    Font

    With IaaS you rent the virtual machine. Microsoft looks after the building, power and hardware; you patch the operating system.

    โšก The 30-second answer

    A DataFrame is a table: rows of records and named columns, like a spreadsheet inside Python. You can build one from a dictionary where each key is a column name and each value is a list. Every column is a Series, so staff["salary"].max() works on one column at a time.

    • Build a DataFrame from a dictionary
    • Read shape and column names
    • Pull out one column and summarise it

    Try it

    Watch it come alive, run it yourself, then prove it. Real Python, right on this page.

    Watch Python run the example. Every variable is a jar; watch the values change and the output appear.

    pandas DataFrames, running
    1. import pandas as pd
    2. staff = pd.DataFrame({
    3. "name": ["Asha", "Ben", "Chen", "Dina"],
    4. "team": ["Data", "Web", "Data", "Ops"],
    5. "salary": [72000, 58000, 81000, 64000],
    6. })
    7. print(staff)
    8. print(staff.shape)
    9. print(staff.columns.tolist())
    10. print(staff["salary"].max())
    Press โ–ถ to watch Python run this, one line at a time.
    1. The problemHR sends you a staff list: names, teams and salaries. You need the highest salary and how many people there are. In Excel you would scroll and click. In pandas it is two short lines.
    2. The Python ideadf = pd.DataFrame({"col": [...], "col2": [...]})A DataFrame is a table: rows of records and named columns, like a spreadsheet inside Python.
    3. In real lifeA delivery company loads every order of the day into one DataFrame: order id, driver, distance, minutes late. The morning dashboard is built from that table.

    Start with a question

    HR sends you a staff list: names, teams and salaries. You need the highest salary and how many people there are. In Excel you would scroll and click. In pandas it is two short lines.

    Almost all real data arrives as a table: CSV files, Excel sheets, database query results. The DataFrame is how Python holds them.

    Explain it like I'm 10

    A DataFrame is a class register. Each row is one pupil, each column is one thing about them: name, class, marks. You can read down a column or across a row.

    The technical explanation

    pd.DataFrame({"name": [...], "salary": [...]}) builds a table. Keys become column names, lists become columns.

    df.shape is (rows, columns). df.columns lists the column names and df.index the row labels.

    df["salary"] returns that column as a Series, so .max(), .mean() and .sum() work on it.

    df.head() shows the first 5 rows and df.info() shows each column's type and how many values are filled in.

    Syntax

    df = pd.DataFrame({"col": [...], "col2": [...]})
    df.shape
    df.columns
    df["col"].mean()
    df.head()

    โ–ถ Run the example ๐ŸŽฌ Watch it come alive

    Real-world scenario

    A delivery company loads every order of the day into one DataFrame: order id, driver, distance, minutes late. The morning dashboard is built from that table.

    Ready for more?The full lesson adds the technical explanation, interview questions and quick revision.

    Test yourself

    5 questions. Get 4 right and this lesson is marked complete.

    1. 1. df has 200 rows and 6 columns. What is df.shape?
    2. 2. What does df["price"] return?
    3. 3. In pd.DataFrame({"a": [1, 2]}), what does the key "a" become?
    4. 4. What does df.head() show?
    5. 5. Lists of length 3 and 4 in one dictionary giveโ€ฆ

    Questions people ask

    How do I create a pandas DataFrame from a dictionary?

    Pass a dictionary of equal-length lists to pd.DataFrame. Each key becomes a column name and each list becomes that column's values.

    How do I see the column types of a DataFrame?

    Use df.dtypes for the type of each column, or df.info() for the types plus how many non-empty values each column has.

    Interview questions

    How this topic comes up in Python interviews. Answer out loud first, then open the model answer.

    beginner How do I create a pandas DataFrame from a dictionary?

    Pass a dictionary of equal-length lists to pd.DataFrame. Each key becomes a column name and each list becomes that column's values.

    beginner How do I see the column types of a DataFrame?

    Use df.dtypes for the type of each column, or df.info() for the types plus how many non-empty values each column has.

    intermediate df has 200 rows and 6 columns. What is df.shape?

    (200, 6). shape is (rows, columns).

    intermediate What does df["price"] return?

    A Series (one column). One column of a DataFrame is a Series.

    Quick revision

    Remember

    • Build a DataFrame from a dictionary
    • Read shape and column names
    • Pull out one column and summarise it
    • Every list in the dictionary must have the same length. If one list is shorter, pandas raises "All arrays must be of the same length".

    Syntax

    df = pd.DataFrame({"col": [...], "col2": [...]})
    df.shape
    df.columns
    df["col"].mean()
    df.head()

    Official documentation