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
Programming Articles
Page 116 of 2547
Write a program in Python to verify camel case string from the user, split camel cases, and store them in a new series
Camel case is a naming convention where the first letter is lowercase and each subsequent word starts with an uppercase letter (e.g., "pandasSeriesDataFrame"). This tutorial shows how to verify if a string is in camel case format and split it into a pandas Series. Understanding Camel Case Validation A valid camel case string must satisfy these conditions: Not all lowercase Not all uppercase Contains no underscores Solution Steps To solve this problem, we follow these steps: Define a function that accepts the input string Check if the string is in camel ...
Read MoreWrite a Python code to combine two given series and convert it to a dataframe
When working with Pandas Series, you often need to combine them into a single DataFrame for analysis. Python provides several methods to achieve this: direct DataFrame creation, concatenation, and joining. Method 1: Using DataFrame Constructor Create a DataFrame from the first series, then add the second series as a new column ? import pandas as pd series1 = pd.Series([1, 2, 3, 4, 5], name='Id') series2 = pd.Series([12, 13, 12, 14, 15], name='Age') df = pd.DataFrame(series1) df['Age'] = series2 print(df) Id Age 0 1 ...
Read MoreWrite a program in Python to split the date column into day, month, year in multiple columns of a given dataframe
When working with date data in a pandas DataFrame, you often need to split a date column into separate day, month, and year columns. This is useful for analysis, filtering, or creating date-based features. Problem Statement Given a DataFrame with a date column in "DD/MM/YYYY" format, we want to extract day, month, and year into separate columns ? date day month year 0 17/05/2002 17 05 2002 1 16/02/1990 16 02 1990 2 25/09/1980 25 09 1980 3 11/05/2000 ...
Read MoreWrite a Python code to convert a given series into a dummy variable and drop any NaN values if they exist
Converting a Pandas Series into dummy variables creates binary columns for each unique value. The pd.get_dummies() function handles this conversion and can automatically drop NaN values by setting dummy_na=False. Understanding Dummy Variables Dummy variables are binary (0 or 1) columns that represent categorical data. For example, a "Gender" series with values "Male" and "Female" becomes two columns: "Male" and "Female", where 1 indicates the presence of that category. Syntax pd.get_dummies(data, dummy_na=False) Parameters The key parameter for handling NaN values ? dummy_na=False : Excludes NaN values from dummy variable creation dummy_na=True ...
Read MoreWrite a program in Python to convert a given dataframe to a LaTex document
Converting a Pandas DataFrame to LaTeX format is useful for creating professional documents and research papers. The to_latex() method generates LaTeX table code that can be directly used in LaTeX documents. Basic DataFrame to LaTeX Conversion Let's start by creating a sample DataFrame and converting it to LaTeX format ? import pandas as pd df = pd.DataFrame({ 'Id': [1, 2, 3, 4, 5], 'Age': [12, 13, 14, 15, 16] }) print("Original DataFrame:") print(df) print("LaTeX output:") print(df.to_latex(index=True, multirow=True)) Original DataFrame: Id ...
Read MoreWrite a program in Python to generate an even (length) series of random four-digit pin. Get the length from user and ask until it's valid
This program generates a series of random four-digit PIN numbers. The user must provide an even number for the series length, and the program will keep asking until a valid even number is entered. Problem Requirements We need to: Get series length from user input Validate that the length is even Generate random four-digit PIN numbers Display the series using pandas Step-by-Step Solution Step 1: Input Validation First, we create a loop to get valid even input from the user ? while(True): size = int(input("enter the ...
Read MoreWrite a program in Python to filter City column elements by removing the unique prefix in a given dataframe
When working with pandas DataFrames, you might need to filter cities that share the same starting letter with other cities. This tutorial shows how to remove cities with unique prefixes (first letters) and keep only those cities whose first letter appears in multiple city names. Understanding the Problem Given a DataFrame with city names, we want to filter out cities that have unique starting letters. For example, if only one city starts with 'C', we exclude it. If multiple cities start with 'K', we keep all of them. Step-by-Step Solution Step 1: Create the DataFrame ...
Read MoreWrite a program in Python Pandas to convert a dataframe Celsius data column into Fahrenheit
In this tutorial, we'll learn how to convert a Celsius column to Fahrenheit in a Pandas DataFrame. The conversion formula is: Fahrenheit = (9/5) × Celsius + 32. We'll explore two common approaches using assign() and apply() methods. Using assign() Method The assign() method creates a new column while keeping the original DataFrame unchanged. It uses a lambda function to apply the conversion formula ? import pandas as pd # Create DataFrame with temperature data df = pd.DataFrame({ 'Id': [1, 2, 3, 4, 5], 'Celsius': [37.5, ...
Read MoreWrite a program to append Magic Numbers from 1 to 100 in a Pandas series
A magic number is a number whose digits sum up to 1 or 10. In this tutorial, we'll create a Pandas series containing all magic numbers from 1 to 100. We'll explore two different approaches to solve this problem. What are Magic Numbers? Magic numbers are numbers where the sum of digits equals 1 or 10. For example: 1 → sum = 1 (magic number) 10 → sum = 1 + 0 = 1 (magic number) 19 → sum = 1 + 9 = 10 (magic number) 28 → sum = 2 + 8 = 10 ...
Read MoreWrite a Python code to filter palindrome names in a given dataframe
A palindrome is a word that reads the same forwards and backwards. In this tutorial, we'll learn how to filter palindrome names from a Pandas DataFrame using different approaches. Using List Comprehension This approach uses list comprehension to identify palindromes by comparing each name with its reverse using slicing [::-1] ? import pandas as pd data = {'Id': [1, 2, 3, 4, 5], 'Name': ['bob', 'peter', 'hannah', 'james', 'david']} df = pd.DataFrame(data) print("DataFrame is:") print(df) # Find palindrome names using list comprehension palindromes = [name for name in df['Name'] if name == name[::-1]] result ...
Read More