<jupyter_start><jupyter_text>Pandas 101> almost every operation doesn't effect the original object returns new DataFrame object--- Loading Data<jupyter_code>import pandas as pd
df = pd.read_csv('pokemon_data.csv')

# data with a delimiter
# df = pd.read_csv('pokemon_data.txt', delimiter='\t')<jupyter_output><empty_output><jupyter_text>Head & Tail<jupyter_code>df.head(10)
# returns first 10 rows as DataFrame
# returns last 5 rows
df.tail()<jupyter_output><empty_output><jupyter_text>Reading the data<jupyter_code># Reading headers
df.columns
# Read one column
df['Name'][:10]
# df[['Name']][:10] # this one also works
# Read multiple columns
df[['Name', 'Type 1', 'HP']]
df.head(5)
# specific indexed row
# iloc: integer location
df.iloc[2]
df.iloc[1:4]
# Specific cell value
df.iloc[2, 1]
df.iloc[2]['Name']
# iter through rows
for index, row in df.iterrows():
    print(f'{index}: {row}')
    if index == 4: break
# get the specific rows based on conditions
df.loc[df['HP'] > 150]

# > type(df.loc[df['HP'] > 150])
# > pandas.core.frame.DataFrame<jupyter_output><empty_output><jupyter_text>Describing Data<jupyter_code>df.describe()
# all numerical/statistical data description<jupyter_output><empty_output><jupyter_text>Sorting Data Single Column<jupyter_code># ascending sort
df_name = df.sort_values('Name')
df_name.head(10)
# descending sort
df_name = df.sort_values('Name', ascending=False)
df_name.head(10)<jupyter_output><empty_output><jupyter_text>Multiple Columns all ascending<jupyter_code>df.head(10)
# sort by type and then HP ascending
df_type_hp = df.sort_values(['Type 1', 'HP'])
df_type_hp.head(10)<jupyter_output><empty_output><jupyter_text>⬆ Both **`Type 1`** and **`HP`** columns are sorted ascending Multiple Columns mixed sortingSort **`Type 1`** ascending and **`HP`** descending<jupyter_code>df_type_hp_mixed = df.sort_values(['Type 1', 'HP'], ascending=[True, False])

# ^ Both statements are same
# 1: True
# 0: False
# df_type_hp_mixed = df.sort_values(['Type 1', 'HP'], ascending=[1, 0])

df_type_hp_mixed.head(10)<jupyter_output><empty_output><jupyter_text>⬆ **`Type 1`** sorted ascending and **`HP`** sorted descending Making changes to data  Adding new column<jupyter_code>df['Total'] = df['HP'] + df['Attack'] + df['Defense'] + df['Speed']<jupyter_output><empty_output><jupyter_text>**New column `Total` is added**<jupyter_code>df.head(3)<jupyter_output><empty_output><jupyter_text>**Adding a column using `iloc`**<jupyter_code># df['SpTotal'] = df['Sp. Atk'] + df['Sp. Def']

df['SpTotal'] = df.iloc[:, 7:9].sum(axis=1)<jupyter_output><empty_output><jupyter_text>* **axis=0** : Vertical/Column* **axis=1** : Horizontal/Row```pydf['SpTotal']  New column to createdf.iloc[:, 7:9].sum(axis=1)  df.iloc[all_rows/row_range, 7 and 8 columns] sum horizontally```<jupyter_code>df.head()
df['Spam'] = df.iloc[0:5, 5:7].sum(axis=1)
df.head(10)<jupyter_output><empty_output><jupyter_text>**`NaN` for column `Spam` from 5th row**  Drop a column(s)<jupyter_code># it doesn't drop in df instered it returns new df
df = df.drop('Total', axis=1)
df.head()<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup