<jupyter_start><jupyter_text>value_counts() method<jupyter_code># reset nba to original import 
nba = pd.read_csv("data/nba.csv")
nba.head()
# count players per team
nba["Team"].value_counts()
nba["Position"].value_counts()
# deal with peky NaN values
# reset nba to original import 
nba = pd.read_csv("data/nba.csv")
nba.head()
nba.tail()
nba.dropna().head()    # drops entire row where any value within the row is NaN 
# the how parameter defaults to "any"
nba.dropna().tail()
# change the how parameter from any to all - only drop rows with all NaN
nba.dropna(how = "all").head() # note partial NaNs come back
nba.dropna(how = "all").tail()    # however last row is removed
# remove all columns with null values 
nba.dropna(how="all", axis=1).head()
# use dropna() subset parameter to narrow rows
nba.dropna(subset=["Salary"]).head()
# fillna() method
# reset nba to original import 
nba = pd.read_csv("data/nba.csv")
nba.head()
nba.fillna(0).head(10)  #    works for some cols but not for string cols
nba["Salary"].fillna(0,inplace = True)
nba.head(10)
nba["College"].head(10)  # note the NaNs
nba["College"].fillna("High Skool",inplace=True)
nba.head(10)
# astype() method 
# reset nba to original import 
nba = pd.read_csv("data/nba.csv").dropna(how="all") # drops final null row 
nba.tail()
nba.info()  # note the presence of null values 
nba["Salary"].fillna(0,inplace=True)
nba["College"].fillna("No College", inplace=True)
nba.head(10)
nba.tail()
nba.dtypes # note: any column with NaN is converted to a float by pandas which remains after removing NaNs
nba.info() # note there are no longer any nulls 
nba["Salary"].astype("int")
# astype() method does not have an inplace parameter, so need to reasign manually
nba["Salary"] = nba["Salary"].astype("int")
nba["Salary"].head()
nba.info()
# note change to Salary dtype
nba["Number"] = nba["Number"].astype("int")
nba["Age"] = nba["Age"].astype("int")
nba.info()
# use category dtype for cols with a few unique values such as Male/Female, or 
nba["Position"].value_counts()
nba["Position"] = nba["Position"].astype("category") # no noticable differance, but memory usage should be reduced 
nba.info()
nba["Team"].value_counts().count()
nba["Team"].nunique()
nba["Team"] = nba["Team"].astype("category")
nba.info()
# reset nba to original import 
nba = pd.read_csv("data/nba.csv")
nba.head()
nba.info()
nba.sort_values("Name").head()
nba.sort_values("Name", ascending = False).head()
nba.sort_values("Age", ascending=False).head()
nba.sort_values("Salary", ascending=False).head()
nba.sort_values("Salary", ascending=False, inplace=True)
nba.head()
# sort by multiple columns
# reset nba to original import 
nba = pd.read_csv("data/nba.csv")
nba.head()
nba.sort_values(["Team", "Salary"])     # sort by Team and then by Name 
nba.sort_values(["Team", "Salary"], ascending = False)   
nba.sort_values(["Team", "Salary"], ascending = [True,False])  # note the list used in ascending parameter 
# rank() method
# reset nba to original import 
nba = pd.read_csv("data/nba.csv")
nba.tail()
nba = nba.dropna(how = "all")    # drop rows where all values are NaN
nba["Salary"] = nba["Salary"].fillna(0).astype(int)
nba.head(5)
# rank salaries
nba["Salary"].rank()
nba["Salary Rank"] = nba["Salary"].rank(ascending = False).astype(int)
nba.head()
nba.sort_values(by = "Salary", ascending=False).head(20)
# note that same values share a rank number 

# filter - extract desired rows<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16329479768786126, 'fraction_numerical': 0.005202312138728324, 'mean_word_length': 5.385608856088561, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '95077', 'license': 'MIT', 'n_tokens_mistral': 1269, 'n_tokens_neox': 1237, 'n_words': 339, 'path': 'UdemyPandas/.ipynb_checkpoints/2018-02-Python-Pandas-Dataframe-checkpoint.ipynb', 'repo_name': 'simonbaier/learn-pandas', 'size': '3460'}