You can also create a Series using a key/value object, such as a dictionary.
You can create a simple Pandas Series from a dictionary like this:
import pandas as pd calories = {“day1”: 420, “day2”: 380, “day3”: 390} myvar = pd.Series(calories) print(myvar) |
To select specific items from the dictionary, use the index
argument and specify the items you want to include in the Series.
Create a Series using data only from “day1” and “day2”:
import pandas as pd calories = {“day1”: 420, “day2”: 380, “day3”: 390} myvar = pd.Series(calories, index = [“day1”, “day2”]) print(myvar) |
In Pandas, datasets are typically represented as multi-dimensional tables called DataFrames. While a Series is similar to a column, a DataFrame represents the entire table.
You can create a DataFrame by combining two Series:
import pandas as pd data = { “calories”: [420, 380, 390], “duration”: [50, 40, 45] } myvar = pd.DataFrame(data) print(myvar) |