Found 10476 Articles for Python

How can you select data from a table based on some criteria using MySQL in Python?

Pawandeep Kaur
Updated on 10-Jun-2021 12:18:26

764 Views

It is not frequently required to select all the data from the table. Instead , we need to select data or rows from the table based on some condition or criteria.Suppose, we have a table which includes names and marks of many students. Now, we need to get names of the students whose marks are above 90. This basically requires us to select data based on some criteria ,which is(marks>=90).Therefore, we are provided with “WHERE” statement in MySQL which allows us to select data from the table based on some specified criteria.SyntaxSELECT * FROM table_name WHERE conditionHere, table_name specifies the ... Read More

What is the fetchone() method? Explain its use in MySQL Python?

Pawandeep Kaur
Updated on 10-Jun-2021 12:12:58

9K+ Views

Fetchone() methodFetchone() method is used when you want to select only the first row from the table. This method only returns the first row from the MySQL table.Use of fetchone() methodThe fetchone() is not used as a query to be used to the cursor object. The query passed is “SELECT *” which fetches all the rows from the table.Later , we operate fetchone() method on the result returned by “SELECT *”. The fetchone() method then fetches the first row from that result.Steps you need to follow to fetch first row from a table using MySQL in pythonimport MySQL connectorestablish connection ... Read More

How to select all the data from a table using MySQL in Python?

Pawandeep Kaur
Updated on 10-Jun-2021 12:10:16

3K+ Views

The tables in MySQL consist of rows and columns. The columns specify the fields and the rows specify the records of data. The data in the tables needs to be fetched to use. We may at times need to fetch all the data from the MySQL table.All the rows can be fetched from the table using the SELECT * statement.SyntaxSELECT * FROM table_nameThe * in the above syntax means to fetch all the rows from the table.Steps you need to follow to select all the data from a table using MySQL in pythonimport MySQL connectorestablish connection with the connector using ... Read More

How to customize X-axis ticks in Matplotlib?

Rishikesh Kumar Rishi
Updated on 10-Jun-2021 12:04:07

4K+ Views

To customize X-axis ticks in Matplotlib, we can change the ticks length and width.StepsSet the figure size and adjust the padding between and around the subplots.Create lists for height, bars and y_pos data points.Make a bar plot using bar() method.To customize X-axis ticks, we can use tick_params() method, with color=red, direction=outward, length=7, and width=2.To display the figure, use show() method.Exampleimport numpy as np import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True height = [3, 12, 5, 18, 45] bars = ('A', 'B', 'C', 'D', 'E') y_pos = np.arange(len(bars)) plt.bar(y_pos, height, color='yellow') plt.tick_params(axis='x', colors='red', direction='out', ... Read More

How to remove grid lines from an image in Python Matplotlib?

Rishikesh Kumar Rishi
Updated on 10-Jun-2021 12:11:11

7K+ Views

To remove grid lines from an image, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Load an image from a file.Convert the image from one color space to another.To remove grid lines, use ax.grid(False).Display the data as an image, i.e., on a 2D regular raster.To display the figure, use show() method.Examplefrom matplotlib import pyplot as plt import cv2 plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True img = cv2.imread('bird.jpg') img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) plt.grid(False) plt.imshow(img) plt.show()OutputRead More

How to save a plot in Seaborn with Python (Matplotlib)?

Rishikesh Kumar Rishi
Updated on 10-Jun-2021 12:11:59

4K+ Views

To save a plot in Seaborn, we can use the savefig() method.StepsSet the figure size and adjust the padding between and around the subplots.Make a two-dimensional, size-mutable, potentially heterogeneous tabular data.Plot pairwise relationships in a dataset.Save the plot into a file using savefig() method.To display the figure, use show() method.Exampleimport seaborn as sns import pandas as pd import numpy as np import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True df = pd.DataFrame(np.random.random((5, 5)), columns=["a", "b", "c", "d", "e"]) sns_pp = sns.pairplot(df) sns_pp.savefig("sns-heatmap.png")OutputWhen we execute the code, it will create the following plot and save it ... Read More

How to customize the X-axis in Matplotlib?

Rishikesh Kumar Rishi
Updated on 10-Jun-2021 12:10:45

3K+ Views

To customize the X-axis label, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Initialize a variable, N, to get the number of sample data.Create x and y data points using numpyPlot x and y data points using plot() method.Customize the X-axis labels with fontweight, color, fontsize, and alignment.To display the figure, use show() method.Exampleimport numpy as np import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True N = 100 x = np.random.rand(N) y = np.random.rand(N) plt.plot(x, y, 'r*') plt.xlabel('X-axis Label', fontweight='bold', color='orange', ... Read More

How to align axis label to the right or top in Matplotlib?

Rishikesh Kumar Rishi
Updated on 10-Jun-2021 12:01:43

2K+ Views

To align axis label to the right (X-axis label) or top (Y-axis label), we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create a figure and a set of subplots.Initialize a variable, N, for number data samples.Plot x and y data points using plot() method.Set xlabel and ylabel at the right and top locations, respectively.To display the figure, use show() method.Exampleimport numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True fig, ax = plt.subplots() N = 10 x = np.random.rand(N) y = ... Read More

Create a legend with Pandas and Matplotlib.pyplot

Rishikesh Kumar Rishi
Updated on 10-Jun-2021 12:00:33

1K+ Views

To create a legend with Pandas and matplotib.pyplot(), we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Make a two-dimensional, size-mutable, potentially heterogeneous tabular data.Plot the dataframe instance with bar class by name and legend is True.To display the figure, use show() method.Exampleimport pandas as pd from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True fig, ax = plt.subplots() df = pd.DataFrame({'Numbers': [3, 4, 1, 7, 8, 5], 'Frequency': [2, 4, 1, 4, 3, 2]}) df.plot(ax=ax, kind='bar', legend=True) plt.show()Output

Frequency plot in Python/Pandas DataFrame using Matplotlib

Rishikesh Kumar Rishi
Updated on 10-Jun-2021 12:00:11

16K+ Views

To show a frequency plot in Python/Pandas dataframe using Matplotlib, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create a figure and a set of subplots.Make a two-dimensional, size-mutable, potentially heterogeneous tabular data.Return a Series containing the counts of unique values.To display the figure, use show() method.Exampleimport pandas as pd from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True fig, ax = plt.subplots() df = pd.DataFrame({'numbers': [2, 4, 1, 4, 3, 2, 1, 3, 2, 4]}) df['numbers'].value_counts().plot(ax=ax, kind='bar', xlabel='numbers', ylabel='frequency') plt.show()OutputRead More

Advertisements