<jupyter_start><jupyter_text>___ ___<jupyter_code>import numpy as np
import pandas as pd
from numpy.random import randn
np.random.seed(101)
df = pd.DataFrame(randn(5,4), ["A", "B", "C", "D", "E"], ["W", "X", "Y", "Z"]) # there will be 5 rows & 4 columns as per randn
df<jupyter_output><empty_output><jupyter_text>Conditional Selection**Conditional Selection** is the ability to form conditional selection (choosing data based on a condition) using bracket notation. This will return a DataFrame of boolean values. Single Conditional Statements<jupyter_code>booldf = df < 0
booldf<jupyter_output><empty_output><jupyter_text>What do you think will happen if we pass this boolean DataFrame in to **df**?<jupyter_code>df[booldf]
# Condense the above two lines into:
df[df < 0]<jupyter_output><empty_output><jupyter_text>Operation with a dataframe against a comparison operator is not common. It is more likely you will pass in a row or column. And instead of returning **null** or **NaN** it will return a subset of the DF where conditions are True.<jupyter_code># When doing comparison on a column (Series) you will get a series back
print(df)
df['X'] > 0<jupyter_output>W         X         Y         Z
A  2.706850  0.628133  0.907969  0.503826
B  0.651118 -0.319318 -0.848077  0.605965
C -2.018168  0.740122  0.528813 -0.589001
D  0.188695 -0.758872 -0.933237  0.955057
E  0.190794  1.978757  2.605967  0.683509<jupyter_text>You can now use this series of boolean values corresponding to rows based on a column's value.<jupyter_code># Pass the series into a DF using bracket notation
df[df['X'] > 0]<jupyter_output><empty_output><jupyter_text>You will notice that it only returned rows that were True in the original `df['X'] > 0` series.The nulls or **NaN** do not show because you're not even returning them.You can now call commands as well, since it is returning a DataFrame.<jupyter_code># return only the rows where Z is > 0
print(df)
resultdf = df[df['W'] > 0]
resultdf
resultdf['X']
# You can do all the above 2 commands in one step!
#     resultdf = df[df['W'] > 0]
#     resultdf['X']
df[df['W'] > 0]['X']
# can also get back a DF
#     boolSer = df['W'] > 0
#     result = df[boolSer]
#     my_cols = ['Y', 'X']
#     result[my_cols]
df[df['W'] > 0][['Y','X']]<jupyter_output><empty_output><jupyter_text>Multiple Conditional StatementsPython's normal **and** and **or** operator cannot take into account a series & compare it to another. It can only compare booleans. When comparing boolean Series, you will use the **&** or **|** operator.`df[(comparison1) & (comparison2) ...]` or `df[(comparison1) | (comparison2) ...]`<jupyter_code>df['W'] > 0
df['Y'] < 0
(df['W'] > 0) & (df['Y'] < 0)
print(df)
df[(df['W'] > 0) & (df['Y'] < 0)]
(df['W'] > 0) | (df['Y'] < 0)
print(df)
df[(df['W'] > 0) | (df['Y'] < 0)]<jupyter_output>W         X         Y         Z
A  2.706850  0.628133  0.907969  0.503826
B  0.651118 -0.319318 -0.848077  0.605965
C -2.018168  0.740122  0.528813 -0.589001
D  0.188695 -0.758872 -0.933237  0.955057
E  0.190794  1.978757  2.605967  0.683509<jupyter_text>Index Resetting IndexReset index back to default from 0 up to number of rows -1 ... simply call:  `df.reset_index()`Take note of what happened in the DataFrame!<jupyter_code>df
df.reset_index() # does not occur in place unless you specify it to do so using inplace=True<jupyter_output><empty_output><jupyter_text>Changing Index To Something ElseIf you wish to change the index, you need to ensure the list you utilize has the same amount of indexes. Have A Column Already In DataFrame You Want As Index`df.set_index(col_name)` - this is not done in place as you must set **inplace** to True.<jupyter_code>new_idx = "TX AL DE GA MI".split()
new_idx
df['States'] = new_idx
df
df.set_index('States')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12850773669027013, 'fraction_numerical': 0.08261211644374508, 'mean_word_length': 4.349228611500701, 'pattern_counts': {'":': 0, '<': 42, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '346531', 'license': 'MIT', 'n_tokens_mistral': 1542, 'n_tokens_neox': 1363, 'n_words': 473, 'path': 'pandas/Pandas 04 - DataFrames pt2.ipynb', 'repo_name': 'ProsperousHeart/TrainingUsingJupyter', 'size': '3813'}