28 lines
665 B
Python
28 lines
665 B
Python
"""
|
|
Create a DataFrame and use indexes to
|
|
"""
|
|
import pandas as pd
|
|
|
|
# Create a dataframe and associate an index to it
|
|
df = pd.DataFrame(
|
|
data={"name": ["Mac", "Ann", "Rob"], "age": [33, 44, 55]},
|
|
index=["u1", "u2", "u3"] # as many values as rows
|
|
)
|
|
|
|
# Show normal DataFrame
|
|
print(df)
|
|
|
|
# Access one row using an index value
|
|
print(df.loc["u1"])
|
|
|
|
# Access the same row using a numerical index
|
|
print(df.iloc[0])
|
|
|
|
# Get a DataFrame with a selection of lines
|
|
# To extract this, the selection of lines **must** be a list and not a tuple;
|
|
# the tuple is used to select or slice in the other axis.
|
|
print(df.loc[["u1", "u3", "u2"]])
|
|
|
|
# Show the index0
|
|
print(df.index)
|