Add source example files
This commit is contained in:
27
source/pandas/01-pandas-manual/03-dataframe-index-demo.py
Normal file
27
source/pandas/01-pandas-manual/03-dataframe-index-demo.py
Normal file
@ -0,0 +1,27 @@
|
||||
"""
|
||||
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)
|
Reference in New Issue
Block a user