<jupyter_start><jupyter_text>______Copyright <NAME>For more information, visit us at www.pieriandata.com  DataFramesDataFrames are the workhorse of pandas and are directly inspired by the R programming language. We can think of a DataFrame as a bunch of Series objects put together to share the same index. Let's use pandas to explore this topic!<jupyter_code>import pandas as pd
import numpy as np
from numpy.random import randn
np.random.seed(101)
df = pd.DataFrame(randn(5,4),index='A B C D E'.split(),columns='W X Y Z'.split())
df<jupyter_output><empty_output><jupyter_text>Selection and IndexingLet's learn the various methods to grab data from a DataFrame<jupyter_code>df['W']
# Pass a list of column names
df[['W','Z']]
# SQL Syntax (NOT RECOMMENDED!)
df.W<jupyter_output><empty_output><jupyter_text>DataFrame Columns are just Series<jupyter_code>type(df['W'])<jupyter_output><empty_output><jupyter_text>Creating a new column:<jupyter_code>df['new'] = df['W'] + df['Y']
df<jupyter_output><empty_output><jupyter_text>Removing Columns<jupyter_code>df.drop('new',axis=1)
# Not inplace unless specified!
df
df.drop('new',axis=1,inplace=True)
df<jupyter_output><empty_output><jupyter_text>Can also drop rows this way:<jupyter_code>df.drop('E',axis=0)<jupyter_output><empty_output><jupyter_text>Selecting Rows<jupyter_code>df.loc['A']<jupyter_output><empty_output><jupyter_text>Or select based off of position instead of label<jupyter_code>df.iloc[2]<jupyter_output><empty_output><jupyter_text>Selecting subset of rows and columns<jupyter_code>df.loc['B','Y']
df.loc[['A','B'],['W','Y']]<jupyter_output><empty_output><jupyter_text>Conditional SelectionAn important feature of pandas is conditional selection using bracket notation, very similar to numpy:<jupyter_code>df
df>0
df[df>0]
df[df['W']>0]
df[df['W']>0]['Y']
df[df['W']>0][['Y','X']]<jupyter_output><empty_output><jupyter_text>For two conditions you can use | and & with parenthesis:<jupyter_code>df[(df['W']>0) & (df['Y'] > 1)]<jupyter_output><empty_output><jupyter_text>More Index DetailsLet's discuss some more features of indexing, including resetting the index or setting it something else. We'll also talk about index hierarchy!<jupyter_code>df
# Reset to default 0,1...n index
df.reset_index()
newind = 'CA NY WY OR CO'.split()
df['States'] = newind
df
df.set_index('States')
df
df.set_index('States',inplace=True)
df<jupyter_output><empty_output><jupyter_text>DataFrame SummariesThere are a couple of ways to obtain summary data on DataFrames.df.describe() provides summary statistics on all numerical columns.df.info and df.dtypes displays the data type of all columns.<jupyter_code>df.describe()
df.dtypes
df.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
Index: 5 entries, CA to CO
Data columns (total 4 columns):
W    5 non-null float64
X    5 non-null float64
Y    5 non-null float64
Z    5 non-null float64
dtypes: float64(4)
memory usage: 200.0+ bytes
StarcoderdataJupyterStructuredCleanDedup