Concept Flow - Series vs DataFrame relationship
Create Series
Single column data
Create DataFrame
Multiple columns of Series
DataFrame holds Series as columns
A Series is like one column of data. A DataFrame is many Series combined as columns.
import pandas as pd s = pd.Series([10, 20, 30]) df = pd.DataFrame({'A': s, 'B': [1, 2, 3]}) print(s) print(df)
| Step | Action | Variable | Value/Output |
|---|---|---|---|
| 1 | Create Series s | s | 0 10 1 20 2 30 dtype: int64 |
| 2 | Create DataFrame df with columns 'A' as s and 'B' as list | df | A B 0 10 1 1 20 2 2 30 3 |
| 3 | Print Series s | print(s) | 0 10 1 20 2 30 dtype: int64 |
| 4 | Print DataFrame df | print(df) | A B 0 10 1 1 20 2 2 30 3 |
| 5 | Access df['A'] (a Series) | df['A'] | 0 10 1 20 2 30 dtype: int64 |
| 6 | Access df['B'] (a Series) | df['B'] | 0 1 1 2 2 3 dtype: int64 |
| 7 | Exit | All steps executed |
| Variable | Start | After Step 1 | After Step 2 | Final |
|---|---|---|---|---|
| s | undefined | [10, 20, 30] | [10, 20, 30] | [10, 20, 30] |
| df | undefined | undefined | DataFrame with columns A and B | DataFrame with columns A and B |
Series: 1D labeled data (like one column) DataFrame: 2D labeled data (many columns) DataFrame columns are Series Create DataFrame from dict of Series or lists Each column can have different data types Access columns as df['col'] returns a Series
What is a pandas.Series?
Which of the following is the correct way to get a Series from a DataFrame named df?
df = pd.DataFrame({"A": [1, 2], "B": [3, 4]})What is the output of this code?
import pandas as pd
df = pd.DataFrame({"X": [10, 20], "Y": [30, 40]})
series = df["Y"]
print(type(series))Find the error in this code snippet:
import pandas as pd
df = pd.DataFrame({"A": [1, 2], "B": [3, 4]})
series = df.loc[:, "A"]
print(type(series))You have a DataFrame df with columns ['A', 'B', 'C']. You want to create a new DataFrame with only columns 'A' and 'C' as Series combined. Which code correctly does this?
Pass the test and this pattern moves unknown