Programming Funda

Programming Funda

Share

Programming Funda is an educational site that provides content on many programming languages to all p

Python While Loop Questions and Answers ( September, 2024 ) 29/09/2024

25+ Python While Loop Questions and Answers:

1. Simple Countdown
Write a Python program that counts from 10 to 1 and print each number with the help of the while loop.

count = 10
while count > 0:
print(count)
count -= 1
print("Blast off!")

2. The sum of Positive Numbers

Create a Python while loop program that continuously asks the user for a positive number and keeps a running total. The loop must stop when the user enters a negative number.

total = 0
while True:
number = float(input("Enter a positive number (or a negative number to stop): "))
if number < 0:
break
total += number
print("Total sum of positive numbers:", total)

3. Password Validation

correct_password = "ProgrammingFunda@1"
user_input = ""

while user_input != correct_password:
user_input = input("Enter the password: ")
print("Access granted!")

4. Fibonacci Sequence

The Fibonacci Sequence in Python is one of the most asked questions during the Python interviews. I faced this question so many times in Python interviews.

Fibonacci Sequence:- The Fibonacci Sequence is a sequence of some numbers where one is the sum of two preceding numbers like 0, 1, 1, 2, 3, etc

Let’s write a Python code to generate the Fibonacci series in Python.

num = int(input("Enter a number:- "))

if num > 0:
i = 0
a = 0
b = 1
while i < num:
print(a, end=' ')
a, b = b, a + b
i = i + 1

Learn more about :- https://www.programmingfunda.com/python-while-loop-questions-and-answers/

💯Follow Programming Funda for more Python's content.

Thanks

Python While Loop Questions and Answers ( September, 2024 ) Hi, In this article we are about to explore Python while loop questions and answers with the help of the examples.

Top 30 PySpark DataFrame Methods with Example 08/09/2024

✅ 30+ PySpark DataFrame Methods Crash Course for Data Engineers
==============================================

Hello PySpark Developers, Here I have listed some of the PySpark useful DataFrame methods that are very helpful in real-life PySpark applications.

Let's start! 👇

1. show()

The show() method is used to display the contents of the DataFrame. By default, it shows the top 20 rows.

df.show()

2. select():- The select() method allows you to select specific columns from a DataFrame.

new_df = df.select("first_name", "last_name", "age")
new_df.show()

3. filter() or where(): The filter() or where() method is used to filter rows that meet certain conditions.

from pyspark.sql.functions import col
new_df = df.filter(col("age") > 25)
new_df.show()

from pyspark.sql.functions import col
new_df = df.where(col("age") > 25)
new_df.show()

4. groupBy() and agg():- The groupBy() method is used to group data based on one or more columns, and agg() allows you to perform aggregation functions on grouped data.

from pyspark.sql.functions import avg
new_df = df.groupBy("department").agg(avg("salary").alias("average_salary"))
new_df.show()

5. withColumn(): The withColumn() method is used to add or modify a column in the DataFrame. For example, I want to add 5 to each employee’s age value.

from pyspark.sql.functions import col
new_df = df.withColumn("modified_age", col("age") + 5).select(
"first_name", "last_name", "modified_age"
)
new_df.show()

These are some Methods but you can get all 30+ PySpark DataFrame methods in the below tutorial.

💯Access this tutorial:- https://www.programmingfunda.com/top-30-pyspark-dataframe-methods-with-example/

Leave your suggestions in the comment 💬

💯Join Python | Big Data | Data Engineering | Data Science | Django | Programming for more Free Data Engineering and Data Analysis content.

Thanks

Happy Learning ... 🙏

Top 30 PySpark DataFrame Methods with Example In this article, We will see the Top 30 PySpark DataFrame methods with example. Being a Data Engineer, Data Analyst, or PySpark Developer you must know the

30/08/2024

If you are a data analyst or a data engineer, Then you must have knowledge of Pandas data selection. Pandas provide two ways to select data from DataFrame.

In this article, I have explained all about Pandas loc and iloc along with proper examples.

👉 Click Here to learn:- https://www.programmingfunda.com/loc-and-iloc-in-pandas/

A Comprehensive Guide to Pandas Data Structures 25/08/2024

If you are a beginner in Python Pandas then this tutorial is going to be very helpful for you because throughout this article I have explained all about the Pandas data structures with examples.

A Comprehensive Guide to Pandas Data Structures Hi Pandas lovers, In today's article I will talk about Pandas data structures which are essential to the Pandas. You can say data structures in Pandas are the

How to Find the Nth Highest Salary Using PySpark 02/06/2024

Find the Nth Highest Salary Using PySpark ✅❤️

================================

Without Partition:

from pyspark.sql import SparkSession

from pyspark.sql.window import Window

from pyspark.sql.functions import desc, row_number

# creating spark session

spark = (

SparkSession.builder.master("local[*]")

.appName("www.programmingfunda.com")

.getOrCreate()

)

# creating DataFrame from csv file

df = spark.read.option("header", "true").csv("./sample_data.csv")

# creating a window specification

# windowFunction = Window.orderBy(desc("salary"))

# applying window function

df = df.withColumn("rank", row_number().over(windowFunction))

# getting 2nd highest salaried employee

df = df.filter(df["rank"] == 2)

df.show()

---------------------------------

With Partition:

from pyspark.sql import SparkSession

from pyspark.sql.window import Window

from pyspark.sql.functions import desc, row_number

# creating spark session

spark = (

SparkSession.builder.master("local[*]")

.appName("www.programmingfunda.com")

.getOrCreate()

)

# creating DataFrame from csv file

df = spark.read.option("header", "true").csv("./sample_data.csv")

# creating a window specification

windowFunction = Window.partitionBy("department").orderBy(desc("salary"))

# applying window function

df = df.withColumn("rank", row_number().over(windowFunction))

# getting 2nd highest salaried employee

df = df.filter(df["rank"] == 2)

df.show()

--------------------------------------

Let's wrap up this article here

👉 Visit here to read the complete article:- https://www.programmingfunda.com/how-to-find-the-nth-highest-salary-using-pyspark/

💯Follow Programming Funda for more free Data analysis content.

How to Find the Nth Highest Salary Using PySpark In this article, we will see how to find the Nth highest salary using PySpark with the help of the examples. Throughout this article, we will explore two

How to Convert a Python List to a CSV File 30/03/2024

✅Attention Python Developers!
======================

➡️Convert Python List to CSV File:
---------------------------------------

import csv
language = ['Python', 'Java', 'PHP', 'C', 'C++', 'R']
header = ['Name']

filename = "languages.csv"

with open(filename, "w", newline="") as file:
writer = csv.writer(file)
writer.writerow(header)

for item in languages:
writer.writerow([item])

➡️Convert List of Lists to CSV File:
---------------------------------------

import csv
student = [
["Vishvajit", "IT", 20000],
["Harsh", "Digital Marketing", 15000],
["Vinay", "IT", 12000],
["Mohak", "Marketing", 18000],
["Natasha", "IT", 220000],
]
header = ['Name', 'Department', 'Salary']

filename = "students.csv"

with open(filename, "w", newline="") as file:
writer = csv.writer(file)
writer.writerow(header)
writer.writerows(students)

➡️Convert List of Python Dictionaries to CSV File
----------------------------------------------------------
👉 Visit here to read the complete article:- https://www.programmingfunda.com/how-to-convert-a-python-list-to-a-csv-file/

💯Follow Programming Funda for more free Data analysis content.

How to Convert a Python List to a CSV File So, throughout this article, we have seen how we can convert a Python list to a CSV file with the help of the Python built-in module called CSV.You can

PySpark RDD Actions with Examples » Programming Funda 16/03/2024

PySpark RDD Actions with Examples ✅

===========================

What are PySpark RDD Actions?

PySpark RDD actions trigger the ex*****on of computations on the RDDs and return values to the driver program or write data to an external storage system.

PySpark RDD count():-

from pyspark.sql import SparkSession

spark = (

SparkSession.builder.appName("ProgrammingFunda.com")

.master("local[*]")

.getOrCreate()

)

sc = spark.sparkContext

key_value_dataset = [

("Math", 40),

("Physics", 71),

("Math", 60),

("English", 80),

("Chemistry", 75),

("English", 65),

("Physics", 62),

("Chemistry", 55),

("Social Science", 75),

("Math", 73),

]

rdd = sc.parallelize(key_value_dataset, 3)

print(rdd.count())

-------------------------------------------------

PySpark RDD first():-

from pyspark.sql import SparkSession

spark = (

SparkSession.builder.appName("ProgrammingFunda.com")

.master("local[*]")

.getOrCreate()

)

sc = spark.sparkContext

key_value_dataset = [

("Math", 40),

("Physics", 71),

("Math", 60),

("English", 80),

("Chemistry", 75),

("English", 65),

("Physics", 62),

("Chemistry", 55),

("Social Science", 75),

("Math", 73),

]

rdd = sc.parallelize(key_value_dataset, 3)

print(rdd.first())

=================================

💯Follow Programming Funda for more free Data analysis content.

👉 Visit here for more information: https://www.programmingfunda.com/pyspark-rdd-actions-with-examples/

PySpark RDD Actions with Examples » Programming Funda In today's article, we will see PySpark RDD Actions and all useful PySpark RDD Actions methods with the help of examples.

PySpark RDD ( Resilient Distributed Datasets ) Tutorial 25/02/2024

PySpark RDD ( Resilient Distributed Datasets ) Tutorial:

💯Follow Programming Funda for more free Data analysis content.
👉 Visit here for more content:- https://www.programmingfunda.com/pyspark-rdd-tutorial/

PySpark RDD ( Resilient Distributed Datasets ) Tutorial Hi, In this tutorial, you will learn everything about the PySpark RDD ( Resilient Distributed Datasets ) with the help of the examples. By the end of this

How to Format a String in PySpark DataFrame using Column Values 24/02/2024

How to Format a String in PySpark DataFrame using Column Values

💯Follow Programming Funda for more free Data analysis content.

👉 Visit here for more content:- https://www.programmingfunda.com/how-to-format-a-string-in-pyspark-dataframe-using-column-values/

How to Format a String in PySpark DataFrame using Column Values In this PySpark article, we will see how to format a string in PySpark DataFrame using column values with the help of an example. PySpark provides a string

How to Explode Multiple Columns in Pandas 14/01/2024

Read this complete tutorial to explode single or multiple columns in Pandas DataFrame.

How to Explode Multiple Columns in Pandas In this article, we will see How to explode multiple columns in Pandas with the help of the example. The explode() method is the Pandas DataFrame method that

Want your business to be the top-listed Computer & Electronics Service in Noida?
Click here to claim your Sponsored Listing.

Address


Noida
201301