Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles by Vani Nalliappan
Page 7 of 13
Write a program in Python to read sample data from an SQL Database
Reading data from SQL databases is a common task in data analysis. Python's pandas library provides the read_sql_query() function to easily fetch data from databases and convert it into a DataFrame. Prerequisites You need a SQLite database file with sample data. For this example, we assume you have a db.sqlite3 file containing a student table with the following structure ? Id Name 0 1 stud1 1 2 stud2 2 3 stud3 3 4 stud4 4 5 stud5 Steps to Read ...
Read MoreWrite a Pyton program to perform Boolean logical AND, OR, Ex-OR operations for a given series
Boolean logical operations on Pandas Series allow you to perform element-wise AND, OR, and XOR operations using bitwise operators &, |, and ^. These operations are useful for filtering data and creating conditional logic. Creating a Boolean Series First, let's create a Pandas Series with boolean values including np.nan ? import pandas as pd import numpy as np # Create a boolean series with True, NaN, and False series = pd.Series([True, np.nan, False], dtype="bool") print("Original series:") print(series) Original series: 0 True 1 True 2 ...
Read MoreWrite a program in Python to transpose the index and columns in a given DataFrame
Transposing a DataFrame means swapping its rows and columns. Python provides several methods to transpose DataFrames, from using Pandas built-in methods to implementing custom solutions with list comprehensions or zip operations. Using DataFrame.transpose() or .T The most straightforward approach is using Pandas' built-in transpose() method or its shorthand .T property ? import pandas as pd data = [[1, 2, 3], [4, 5, 6]] df = pd.DataFrame(data) print("Original DataFrame:") print(df) print("Transposed DataFrame:") print(df.transpose()) Original DataFrame: 0 1 2 0 1 2 3 1 ...
Read MoreWrite a program in Python to shift the first column and get the value from the user, if the input is divisible by both 3 and 5 and then fill the missing value
This article demonstrates how to shift DataFrame columns and conditionally fill missing values based on user input. We'll shift the first column to the right and fill the new empty column with a user-provided value only if it's divisible by both 3 and 5. Understanding Column Shifting The shift() method with axis=1 shifts columns horizontally. When shifting right, the first column becomes empty and needs to be filled. Creating the DataFrame import pandas as pd data = pd.DataFrame({ 'one': [1, 2, 3], 'two': [10, 20, 30], ...
Read MoreWrite a program in Python to calculate the default float quantile value for all the element in a Series
A quantile represents the value below which a certain percentage of data falls. In pandas, the quantile() method calculates quantile values for a Series, with 0.5 being the default (median). Understanding Quantiles The quantile value of 0.5 represents the median − the middle value when data is sorted. For a Series with values [10, 20, 30, 40, 50], the 0.5 quantile (median) is 30.0. Syntax Series.quantile(q=0.5, interpolation='linear') Parameters: q − Float between 0 and 1 (default is 0.5) interpolation − Method to use when quantile lies between two data points ...
Read MoreWrite a program in Python to count the records based on the designation in a given DataFrame
To count records based on designation in a pandas DataFrame, we use the groupby() method combined with count(). This groups rows by designation and counts occurrences in each group. Creating the DataFrame Let's start by creating a sample DataFrame with employee data ? import pandas as pd data = { 'Id': [1, 2, 3, 4, 5], 'Designation': ['architect', 'scientist', 'programmer', 'scientist', 'programmer'] } df = pd.DataFrame(data) print("DataFrame is:") print(df) DataFrame is: Id Designation 0 1 architect 1 ...
Read MoreWrite a program in Python to store the city and state names that start with 'k' in a given DataFrame into a new CSV file
When working with pandas DataFrames, you often need to filter data based on specific criteria and save the results to a file. This example demonstrates how to filter cities and states that start with 'K' and export them to a CSV file. Problem Statement Given a DataFrame with City and State columns, we need to find rows where both the city name and state name start with 'K', then save these filtered results to a new CSV file. Solution Approach To solve this problem, we will follow these steps: Create a DataFrame with city ...
Read MoreWrite a Python code to select any one random row from a given DataFrame
Sometimes you need to select a random row from a Pandas DataFrame for sampling or testing purposes. Python provides several approaches to accomplish this task using iloc with random index generation or the sample() method. Sample DataFrame Let's start with a sample DataFrame to demonstrate the methods ? import pandas as pd data = {'Id': [1, 2, 3, 4, 5], 'Name': ['Adam', 'Michael', 'David', 'Jack', 'Peter']} df = pd.DataFrame(data) print("DataFrame is") print(df) DataFrame is Id Name 0 1 Adam ...
Read MoreWrite a Python program to sort a given DataFrame by name column in descending order
Sorting a DataFrame by a specific column is a common task in data analysis. Pandas provides the sort_values() method to sort DataFrames by one or more columns in ascending or descending order. Input DataFrame Let's start with a sample DataFrame containing Id and Name columns ? Id Name 0 1 Adam 1 2 Michael 2 3 David 3 4 Jack 4 5 Peter ...
Read MoreWrite a Python function which accepts DataFrame Age, Salary columns second, third and fourth rows as input and find the mean, product of values
This tutorial shows how to write a Python function that accepts a DataFrame and calculates the mean and product of specific rows from Age and Salary columns using iloc for row slicing. Sample DataFrame Let's start with a sample DataFrame containing employee data ? import pandas as pd data = [[1, 27, 40000], [2, 22, 25000], [3, 25, 40000], [4, 23, 35000], [5, 24, 30000], [6, 32, 30000], [7, 30, 50000], [8, 28, 20000], [9, 29, 32000], [10, 27, 23000]] df = pd.DataFrame(data, columns=('Id', 'Age', 'salary')) print(df) ...
Read More