<jupyter_start><jupyter_text>Modifying values<jupyter_code>import pandas as pd
hero = pd.read_pickle('hero.pkl')
hero.head()
hero.loc[2, 'race']
hero.loc[2, 'race'] = 'Unknown'
hero.head()
hero.iloc[2, 3]
hero.iloc[2, 3] = '-'
hero.head()
hero['race'] == '-'
hero.loc[hero['race'] == '-', 'race']
hero.loc[hero['race'] == '-', 'race'] = 'Unknown'
hero[hero['race'] == 'Unknown']
hero.loc[533]
hero.shape
hero.loc[10000] = hero.loc[533]
hero.shape
hero.tail()
# hero.iloc[10000] = hero.loc[533] IndexError: iloc cannot enlarge its target object
hero.iloc[536] = hero.loc[533]
hero.tail()
hero['height'] #cm
hero['height']*0.0328084 # convert to feet
hero['height'] = hero['height']*0.0328084
hero['height']
hero.select_dtypes(include='number')
numeric_cols = hero.select_dtypes(include='number').columns
numeric_cols
type(numeric_cols)
hero[['height', 'weight']]
hero[numeric_cols]
hero[numeric_cols] * 100
hero[numeric_cols] = hero[numeric_cols] * 100
hero[numeric_cols]
hero[numeric_cols] = hero[numeric_cols] / 100
hero[numeric_cols] # height in feet, weight in kg
# bmi = weight(kg) / [height(m)]^2
hero['weight']/(hero['height']/3.28084)**2
hero['bmi'] = hero['weight']/(hero['height']/3.28084)**2
hero.columns
hero.head(10)<jupyter_output><empty_output><jupyter_text>Making a copy<jupyter_code>hero_ref = hero
hero_copy = hero.copy()
hero['original_ind'] = True
hero_ref['ref_ind'] = True
hero_copy['copy_ind'] = True
hero.columns
hero_ref.columns
hero_copy.columns
hero_ref is hero
hero_copy is hero<jupyter_output><empty_output><jupyter_text>Sorting data<jupyter_code>hero.head()
hero.sort_values(by='race')
hero.sort_values(by='race', ascending=False)
hero.sort_values(by='bmi', ascending=False).head()
hero[['race', 'height']].sort_values(by=['race', 'height'])
hero[['race', 'height']].sort_values(by=['height', 'race'])
hero[['race', 'height']].sort_values(by=['height', 'race'], ascending=[True, False])
hero.nlargest(n=10, columns='height')
hero.sort_values(by='height', ascending=False).head(10)
hero.nsmallest(10, 'height')
hero[hero['height'] > 0].nsmallest(10, 'height')
# hero.nsmallest(10, 'race') #TypeError: Column 'race' has dtype object, cannot use method 'nsmallest' with this dtype<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.19991055456171736, 'fraction_numerical': 0.03756708407871199, 'mean_word_length': 8.243801652892563, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 14, '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': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '658735', 'license': 'MIT', 'n_tokens_mistral': 913, 'n_tokens_neox': 840, 'n_words': 149, 'path': 'Exploring+data+(manipulation)+(2).ipynb', 'repo_name': 'PacktPublishing/Python-for-Data-Analysis-step-by-step-with-projects-', 'size': '2236'}