Pandas Flashcards
How to create a DataFrame and a Series?
pd. DataFrame({“Bob”:[0,3,9], “Tim”:[1,2,3]}, index=[“Product A, Product B])
pd. Series([3,5,6,7], index=[“Product A, Product B])
How to let a column become the index?
object.set_index(“column name”)
Read in csv if csv has index itself:
…, index_col=0
Acess column:
object[“column”]
Acess with loc and iloc:
loc (needs column name) and iloc (both row, column): object.iloc[3,6] or object.loc[5, “houses”]
How large is DF:
object.shape
Assigning data
reviews[“critics]=”everyone”
Acess specific value:
object[“column”] [2]
Set up condition:
2 Conditions:
reviews. loc[reviews.country==”Italy”]
reviews. loc[(reviews.country==”Italy”) & reviews.points < 90)]
Multi Index: which p.e. occurs when you have filtered with 2 columns
object.reset_index()
How to sort by values and how to sort by index?
object.sort_values(by=”column name”, ascending=False). For 2 or more: list with column names.
Object.sort_index()= to sort by index values
How to ask for a datatype and how to transform one?
Object.column.dtype= to ask Object.column.astype(“…”) = to transform
How to rename a column or an index?
object. rename(columns={“points”: “score”})
object. rename(index={0: “first”, 1: “second”})
Combine DataFrame and/or Series?
How if index in common?
pd.concat([object 1, object 2]) if same columns
left=canada_yt.set_index([“title”, “trending”])
right=usa_yt.set_index([“title”, “trending”])
left.join(right, lsuffix=”_can”, rsuffix=”_usa”)
->suffixe are only necessary because there are same column names in bith datasets