<jupyter_start><jupyter_text>101 Pandas Exercises for Data Analysis  Index 41. How to count the number of missing values in each column? 42. How to replace missing values of multiple numeric columns with the mean? 43. How to use apply function on existing columns with global variables as additional arguments? 44. How to select a specific column from a dataframe as a dataframe instead of a series? 45. How to change the order of columns of a dataframe? 46. How to set the number of rows and columns displayed in the output? 47. How to format or suppress scientific notations in a pandas dataframe? 48. How to format all the values in a dataframe as percentages? 39. How to rename a specific columns in a dataframe? 50. How to create a primary key index by combining relevant columns?  41. How to count the number of missing values in each column?<jupyter_code>import pandas as pd
import numpy as np
df = pd.read_csv('Cars93_miss.csv')
df.head()
# Solution
df.isnull().sum().sort_values(ascending = False)

# Alternatives

n_missings_each_col = df.apply(lambda x: x.isnull().sum())
n_missings_each_col.argmax()<jupyter_output>D:\Program_Files\lib\site-packages\ipykernel_launcher.py:4: FutureWarning: 'argmax' is deprecated, use 'idxmax' instead. The behavior of 'argmax'
will be corrected to return the positional maximum in the future.
Use 'series.values.argmax' to get the position of the maximum now.
  after removing the cwd from sys.path.<jupyter_text>42. How to replace missing values of multiple numeric columns with the mean?<jupyter_code># Replace missing values in Min.Price and Max.Price columns with their respective mean.
df = pd.read_csv('Cars93_miss.csv')
df.head()
# Solution
df_out = df[['Min.Price', 'Max.Price']] 
df_out = df[['Min.Price', 'Max.Price']].apply(lambda x: x.fillna(x.mean()))
print(df_out.head())<jupyter_output>Min.Price  Max.Price
0  12.900000  18.800000
1  29.200000  38.700000
2  25.900000  32.300000
3  17.118605  44.600000
4  17.118605  21.459091<jupyter_text>43. How to use apply function on existing columns with global variables as additional arguments?<jupyter_code>df = pd.read_csv('Cars93_miss.csv')
df.head()
df['Max.Price'].median()
df['Min.Price'].mean()
# Solution
d = {'Min.Price': np.nanmean, 'Max.Price': np.nanmedian}
DD = df[['Min.Price', 'Max.Price']]
DD.head(10)
D = DD.apply(lambda x, d: x.fillna(d[x.name](x)), args=(d, ))
D.head(10)<jupyter_output><empty_output><jupyter_text>44. How to select a specific column from a dataframe as a dataframe instead of a series?<jupyter_code>df = pd.read_csv('Cars93_miss.csv')
df.head()
df1 = df.iloc[:, 3:6]
df1.head()
df2 = df.loc[:, ['Manufacturer','Model','Price']]
df2.head()
df3 = df[['Model','Price','Min.Price',]]
df3.head()
df = pd.DataFrame(np.arange(20).reshape(-1, 5), columns=list('abcde'))
df
# Solution
type(df[['a']])
type(df.loc[:, ['a']])
type(df.iloc[:, [0]])<jupyter_output><empty_output><jupyter_text>45. How to change the order of columns of a dataframe?<jupyter_code>Actually 3 questions.

- In df, interchange columns 'a' and 'c'.

- Create a generic function to interchange two columns, without hardcoding column names.

- Sort the columns in reverse alphabetical order, that is colume 'e' first through column 'a' last.<jupyter_output><empty_output><jupyter_text>df = pd.DataFrame(np.arange(20).reshape(-1, 5), columns=list('abcde'))df  Solution Q1df[list('cbade')] df1 = df[['b','a','d',]]df1.head()  Solution Q2 - No hard codingdef switch_columns(df, col1=None, col2=None):    colnames = df.columns.tolist()    i1, i2 = colnames.index(col1), colnames.index(col2)    colnames[i2], colnames[i1] = colnames[i1], colnames[i2]    return df[colnames] switch_columns(df, col1='c', col2='e')  Solution Q3df[sorted(df.columns, reverse = True)] Sort the columns in reverse alphabetical order, that is colume 'e' first through column 'a' last.df.sort_index(axis=1, ascending=False, inplace=True)df<jupyter_code>## 46. How to set the number of rows and columns displayed in the output?<jupyter_output><empty_output><jupyter_text>Change the pandas display settings on printing the dataframe df it shows a maximum of 10 rows and 10 columns.df = pd.read_csv('Cars93_miss.csv')df.head()  Solutionpd.set_option('display.max_columns', 10)df.head()  Solutionpd.set_option('display.max_rows', 10)df pd.describe_option()<jupyter_code>## 47. How to format or suppress scientific notations in a pandas dataframe?<jupyter_output><empty_output><jupyter_text>Suppress scientific notations like ‘e-03’ in df and print upto 4 numbers after decimal.df = pd.DataFrame(np.random.random(10)**10, columns=['random'])df  Solution 1: Roundingdf.round(4)  Solution 2: Use apply to change formatdf.apply(lambda x: '%.4f' % x, axis=1)  ordf.applymap(lambda x: '%.4f' % x)<jupyter_code>## 48. How to format all the values in a dataframe as percentages?<jupyter_output><empty_output><jupyter_text>df = pd.DataFrame(np.random.random(7), columns=['random'])df  Solutionout = df.style.format({'random': '{0:.3%}'.format})out<jupyter_code>## 49. How to filter every nth row in a dataframe?<jupyter_output><empty_output><jupyter_text>From df, filter the 'Manufacturer', 'Model' and 'Type' for every 20th row starting from 1st (row 0).df = pd.read_csv('Cars93_miss.csv')df.head()  Solutiondf.loc[::20,['Manufacturer','Model','Type']].head()  Solutiondf.iloc[::20, :][['Manufacturer', 'Model', 'Type']]<jupyter_code>## 50. How to create a primary key index by combining relevant columns?<jupyter_output><empty_output><jupyter_text>In df, Replace NaNs with ‘missing’ in columns 'Manufacturer', 'Model' and 'Type' and create a index as a combination of these three columns and check if the index is a primary key.<jupyter_code>df = pd.read_csv('Cars93_miss.csv')
df.head()
# Solution
df[['Manufacturer', 'Model', 'Type']] = df[['Manufacturer', 'Model', 'Type']].fillna('missing')
df[['Manufacturer', 'Model', 'Type']].head()
df.index = df.Manufacturer + '_' + df.Model + '_' + df.Type
print(df.index)

print(df.index.is_unique)<jupyter_output>True
{'fraction_non_alphanumeric': 0.13662018592297476, 'fraction_numerical': 0.03569057104913679, 'mean_word_length': 5.792559188275084, 'pattern_counts': {'":': 0, '<': 42, '<?xml version=': 0, '>': 42, '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': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '28765', 'license': 'MIT', 'n_tokens_mistral': 2182, 'n_tokens_neox': 2034, 'n_words': 625, 'path': '1_Pandas_Tips/Pandas_Exercises_for_Data_Analysis_5.ipynb', 'repo_name': 'sudevansujit/5_Pandas', 'size': '6024'}