Python Articles - Page 639 of 1048

How to create a simple screen using Tkinter?

Prasad Naik
Updated on 16-Mar-2021 11:10:50

562 Views

 We will create a simple screen using the Tkinter library.AlgorithmStep 1: Import tkinter. Step 2: Create an object of the tkinter class. Step 3: Display the screen.Example Codeimport tkinter as tk window = tk.Tk()Output

Python program to display various datetime formats

Prasad Naik
Updated on 16-Mar-2021 11:09:09

293 Views

The datetime module supplies classes for manipulating dates and time. We will display different formats like day of the week, week number, day of the year, etc.AlgorithmStep 1: Import datetime. Step 2: Print day of the week. Step 3: Print week number. Step 4: Print day of the year.Example CodeLive Demoimport datetime print("Day of the week: ", datetime.date.today().strftime("%A")) print("Week number: ", datetime.date.today().strftime("%W")) print("Day of the year: ", datetime.date.today().strftime("%j"))OutputDay of the week:  Sunday Week number:  06 Day of the year:  045ExplanationThe arguments of the strftime() function are explained below:%A: Weekday's full name (Example: 'Monday')%W: Week number of the year with ... Read More

How to have logarithmic bins in a Python histogram?

SaiKrishna Tavva
Updated on 23-Sep-2024 14:30:32

5K+ Views

In Python to create a logarithmic bin, we can use Numpy library to generate logarithmically spaced bins, and using matplotlib for creating a histogram. Logarithmic bins in a Python histogram refer to bins that are spaced logarithmically rather than linearly. We can set the logarithmic bins while plotting histograms by using plt.hist(bin="") Steps to Create Logarithmic Bins To set logarithmic bins in a Python histogram, the steps are as follows. Import Libraries: Importing 'matplotlib' for plotting and 'numpy' for performing numerical computations. ... Read More

How to use regular expressions (Regex) to filter valid emails in a Pandas series?

Prasad Naik
Updated on 16-Mar-2021 11:00:23

800 Views

A regular expression is a sequence of characters that define a search pattern. In this program, we will use these regular expressions to filter valid and invalid emails.We will define a Pandas series with different emails and check which email is valid. We will also use a python library called re which is used for regex purposes.AlgorithmStep 1: Define a Pandas series of different email ids. Step 2: Define a regex for checking validity of emails. Step 3: Use the re.search() function in the re library for checking the validity of the email.Example Codeimport pandas as pd import re ... Read More

Pandas program to convert a string of date into time

Prasad Naik
Updated on 16-Mar-2021 11:02:13

220 Views

In this program, we will convert a date string like "24 August 2020" to 2020-08-24 00:00:00. We will use the to_datetime() function in pandas library to solve this task.AlgorithmStep 1: Define a Pandas series containing date string. Step 2: Convert these date strings into date time format using the to_datetime format(). Step 3: Print the results.Example Codeimport pandas as pd series = pd.Series(["24 August 2020", "25 December 2020 20:05"]) print("Series: ", series) datetime = pd.to_datetime(series) print("DateTime Format: ", datetime)OutputSeries: 0            24 August 2020 1    25 December 2020 20:05 dtype: object DateTime Format: 0   2020-08-24 00:00:00 1   2020-12-25 20:05:00 dtype: datetime64[ns]

Finding the length of words in a given Pandas series

Prasad Naik
Updated on 16-Mar-2021 11:02:49

959 Views

In this task, we will find the length of strings in a Pandas series. We will use the str.len() function in the Pandas library for this purpose.AlgorithmStep 1: Define a Pandas series of string. Step 2: Find the length of each string using the str.len() function. Step 3: Print the results.Example Codeimport pandas as pd series = pd.Series(["Foo", "bar", "London", "Quarantine"]) print("Series: ", series) length = series.str.len() print("Length:", length)OutputSeries: 0           Foo 1           bar 2        London 3    Quarantine dtype: object Length: 0     3 1     3 2     6 3    10 dtype: int64

Finding the multiples of a number in a given list using NumPy

Prasad Naik
Updated on 16-Mar-2021 10:54:33

2K+ Views

In this program, we will find the index position at which a multiple of a given number exists. We will use both the Numpy and the Pandas library for this task.AlgorithmStep 1: Define a Pandas series. Step 2: Input a number n from the user. Step 3: Find the multiples of that number from the series using argwhere() function in the numpy library.Example Codeimport numpy as np listnum = np.arange(1, 20) multiples = [] print("NumList:", listnum) n = int(input("Enter the number you want to find multiples of: ")) for num in listnum:    if num % n == ... Read More

How to calculate the frequency of each item in a Pandas series?

Prasad Naik
Updated on 16-Mar-2021 10:55:14

495 Views

In this program, we will calculate the frequency of each element in a Pandas series. The function value_counts() in the pandas library helps us to find the frequency of elements.AlgorithmStep 1: Define a Pandas series. Step 2: Print the frequency of each item using the value_counts() function.Example Codeimport pandas as pd series = pd.Series([10,10,20,30,40,30,50,10,60,50,50]) print("Series:", series) frequency = series.value_counts() print("Frequency of elements:", frequency)OutputSeries: 0     10 1     10 2     20 3     30 4     40 5     30 6     50 7     10 8     60 9     50 10    50 dtype: int64 Frequency of elements: 50    3 10    3 30    2 20    1 40    1 60    1 dtype: int64

How to get the nth percentile of a Pandas series?

Prasad Naik
Updated on 16-Mar-2021 10:55:31

1K+ Views

A percentile is a term used in statistics to express how a score compares to other scores in the same set. In this program, we have to find nth percentile of a Pandas series.AlgorithmStep 1: Define a Pandas series. Step 2: Input percentile value. Step 3: Calculate the percentile. Step 4: Print the percentile.Example Codeimport pandas as pd series = pd.Series([10, 20, 30, 40, 50]) print("Series:", series) n = int(input("Enter the percentile you want to calculate: ")) n = n/100 percentile = series.quantile(n) print("The {} percentile of the given series is: {}".format(n*100, percentile))OutputSeries: 0    10 1 ... Read More

Python xticks in subplots

Rishikesh Kumar Rishi
Updated on 16-Mar-2021 10:50:15

5K+ Views

Subplot can split the figure in nrow*ncols parts and plt.xticks could help to plot the xticks for subplots.StepsCreate two lists for line 1 and line 2.Add a subplot to the current figure, nrow = 1, ncols = 2 and index = 1.Draw line 1 with style as dashed.Set or retrieve auto scaling margins(0.2).Place xticks at even places.Set a title for the X-axis.Add a subplot to the current figure, nrow = 1, ncols = 2 and index = 2.Plot line 2.Set or retrieve auto scaling margins(0.2).Place xticks at odd places.Set a title for the X-axis.To show the figure use plt.show() method.Exampleimport ... Read More

Advertisements