Posts

Showing posts with the label stats

Recreate PROC FREQ in Python

Image
Mastering Descriptive Tables in Python: A Nod to SAS's PROC FREQ Mastering Descriptive Tables in Python: A Nod to SAS's PROC FREQ The art of data analysis often begins with understanding the landscape of your dataset. In SAS, PROC FREQ has long been the de facto tool for generating descriptive tables. Python, via its Pandas library, offers comparable functionalities albeit with a different approach. This post aims to bridge the gap between SAS's PROC FREQ and Python's Pandas, focusing on generating descriptive tables that are replete with counts, row percentages, column percentages, and overall percentages. A Quick Dive into SAS's PROC FREQ Before we dive into Python, let's understand what PROC FREQ in SAS is capable of. This procedure is immensely powerful for categorical data analysis. It provides easy ways to calculate counts, percentages, and additional statistics with simple syntax. For instance, one might execute: PROC FREQ ...

fillna

Image
Handling Missing Data with Pandas: A Comprehensive Guide Handling Missing Data with Pandas: A Comprehensive Guide Dealing with missing data is an essential part of the data cleaning process in Python programming. The Pandas library provides various methods to fill or drop missing values, depending on the nature of the data and the desired outcome. In this guide, we'll explore different techniques to handle missing data, including hard-coded values, filling specific columns, forward fill, and using rolling averages. 1. Filling All Missing Values with a Fixed Number You can simply use the fillna() function to replace all NaNs with a specific value: import pandas as pd df = pd.DataFrame({'A': [1, 2, None], 'B': [None, 5, 6]}) df.fillna(0) 2. Filling Specific Columns with Different Values df.fillna({1: 0.5, 2: 0}) 3. Forward Fill Method df.fillna(method='ff...

Fun Function Friday!!!

Image
My Random Function: Count and Percent The other day I was working on a project to simply count and give me the total percent count. First I started with the data.  The Data: import pandas as pd from itertools import combinations data = { 'id': list(range(1, 21)), 'course': ['Math', 'Math', 'Bio', 'Chem', 'Bio', 'Math', 'Chem', 'Bio', 'Chem', 'Math', 'Bio', 'Chem', 'Math', 'Math', 'Bio', 'Chem', 'Bio', 'Math', 'Chem', 'Math'], 'building': ['A', 'B', 'A', 'B', 'B', 'A', 'A', 'B', 'B', 'A', 'A', 'B', 'A', 'B', 'A', 'B', 'B', 'A', 'A', 'B'], 'room': [101, 102, 101, 102, 103, 101, 104, 103, 102, 101, 105, 106, 107, 108, 109...

f'{strings}' are so useful!!!

Image
Unraveling Python's String Formatting Syntax Python, an incredibly versatile programming language, offers a rich array of features. One of them is the string formatting syntax. This capability enables us to insert various objects, often other strings, directly into our strings. >>> name = "Andrew" >>> print(f"My name is {name}. What's your name?") My name is Andrew. What's your name? This mechanism doesn't just stop at simple insertions; it also allows the embedding of expressions within these strings. >>> name = "Andrew" >>> print(f"{name}, which starts with {name[-1]}") Andrew, ends with w Beyond this, Python's string formatting syntax provides tools to control the formatting of each inserted string component, lending your code an even greater degree of flexibility and precision. At first glance, Python's string formatting syntax may appear quite complex, with a myriad ...

Aggregation in Series Methods

Image
Introduction to Aggregation Methods on Series Data in Python Python, along with its robust libraries like Pandas, NumPy, and others, provides an efficient and effective platform for handling and manipulating series data. One of the key techniques that is often used when dealing with this type of data is "aggregation". Aggregation refers to any process where values of multiple rows are grouped together to form a single summary value. Today, we are going to explore ten common aggregation methods you can use with Python. Common Aggregation Methods in Python Sum: Adds up all the values in the series. Mean: Calculates the average of the series. Median: Finds the middle value of the series. Mode: Returns the most common value in the series. Min: Returns the smallest value in the series. Max: Returns the largest value in the series. Count: Returns the number of non-null values in the series. Std: Calculates the standard deviation of the series. Var:...

for Loops for Graphics

Image
Your Website Title Grouping Sales Data By Month Let's say you have sales data stored as a list of dictionaries, where each dictionary represents a sale and contains details about the fruit, the quantity sold, and the month of sale. For example: sales_data = [ {"fruit": "apple", "quantity": 5, "month": "January"}, {"fruit": "banana", "quantity": 4, "month": "January"}, {"fruit": "apple", "quantity": 7, "month": "February"}, ... ] To report the sum of sales for each fruit, grouped by month, you can use Python's built-in data types and a for loop. Here's a sample Python code to achieve that: # Initialize an empty dictionary to store the results monthly_sales = {} # Loop over the sales data for sale in sales_data: # Get the month and fruit from the sale month = sale["mont...