Programming Articles - Page 1049 of 3363

Python Pandas – Check for Null values using notnull()

AmitDiwan
Updated on 28-Sep-2021 12:30:34

6K+ Views

The notnull() method returns a Boolean value i.e. if the DataFrame is having null value(s), then False is returned, else True.Let’s say the following is our CSV file with some NaN i.e. null values −Let us first read the CSV file −dataFrame = pd.read_csv("C:\Users\amit_\Desktop\CarRecords.csv")Checking for not null values −res = dataFrame.notnull()Now, on displaying the DataFrame, the CSV data will be displayed in the form of True and False i.e. boolean values because notnull() returns boolean. For Null values, False will get displayed. For Not-Null values, True will get displayed.ExampleFollowing is the complete code −import pandas as pd # reading ... Read More

Python - How to drop the null rows from a Pandas DataFrame

AmitDiwan
Updated on 28-Sep-2021 12:19:21

2K+ Views

To drop the null rows in a Pandas DataFrame, use the dropna() method. Let’s say the following is our CSV file with some NaN i.e. null values −Let us read the CSV file using read_csv(). Our CSV is on the Desktop −dataFrame = pd.read_csv("C:\Users\amit_\Desktop\CarRecords.csv")Remove the null values using dropna() −dataFrame = dataFrame.dropna() ExampleFollowing is the complete code −import pandas as pd # reading csv file dataFrame = pd.read_csv("C:\Users\amit_\Desktop\CarRecords.csv") print("DataFrame...", dataFrame) # count the rows and columns in a DataFrame print("Number of rows and column in our DataFrame = ", dataFrame.shape) dataFrame = dataFrame.dropna() print("DataFrame after removing null ... Read More

Python Pandas – How to skip initial space from a DataFrame

AmitDiwan
Updated on 28-Sep-2021 12:11:30

6K+ Views

To skip initial space from a Pandas DataFrame, use the skipinitialspace parameter of the read_csv() method. Set the parameter to True to remove extra space.Let’s say the following is our csv file −We should get the following output i.e. skipping initial whitespace and displaying the DataFrame from the CSV −ExampleFollowing is the complete code −import pandas as pd # reading csv file dataFrame = pd.read_csv("C:\Users\amit_\Desktop\CarRecords.csv") print("DataFrame...", dataFrame) # reading csv file and removing initial space dataFrame = pd.read_csv("C:\Users\amit_\Desktop\CarRecords.csv", skipinitialspace = True) print("DataFrame...", dataFrame)At first, read the CSV. Our CSV file is on the Desktop −dataFrame = pd.read_csv("C:\Users\amit_\Desktop\CarRecords.csv")While reading, ... Read More

Python Pandas - Draw a boxplot and display the datapoints on top of boxes by plotting Swarm plot with Seaborn

AmitDiwan
Updated on 28-Sep-2021 11:52:27

482 Views

To plot swarm plot on top of box plot, at first, set boxplot() and then the swarmplot() with the same x and y values. Box Plot in Seaborn is used to draw a box plot to show distributions with respect to categories. The seaborn.boxplot() is used for this.Swarm Plot in Seaborn is used to draw a categorical scatterplot with non-overlapping points. The seaborn.swarmplot() is used for this.Let’s say the following is our dataset in the form of a CSV file − Cricketers.csvAt first, import the required libraries −import seaborn as sb import pandas as pd import matplotlib.pyplot as pltLoad data from ... Read More

Python Pandas - Fill NaN values using an interpolation method

AmitDiwan
Updated on 28-Sep-2021 11:47:43

2K+ Views

Use the interpolate() method to fill NaN values. Let’s say the following is our CSV file opened in Microsoft Excel with some NaN values −Load data from a CSV file into a Pandas DataFrame −dataFrame = pd.read_csv("C:\Users\amit_\Desktop\SalesData.csv")Fill NaN values with interpolate() −dataFrame.interpolate()ExampleFollowing is the code −import pandas as pd # Load data from a CSV file into a Pandas DataFrame dataFrame = pd.read_csv("C:\Users\amit_\Desktop\SalesData.csv") print("DataFrame...", dataFrame) # fill NaN values with interpolate() res = dataFrame.interpolate() print("DataFrame after interpolation...", res) OutputThis will produce the following output −DataFrame...        Car   Reg_Price   Units 0      BMW ... Read More

Python Pandas – Propagate non-null values backward

AmitDiwan
Updated on 28-Sep-2021 11:41:04

271 Views

Use the “method” parameter of the fillna() method. For backward fill, use the value ‘bfill’ as shown below −fillna(method='bfill')Let’s say the following is our CSV file opened in Microsoft Excel with some NaN values −At first, import the required library −import pandas as pdLoad data from a CSV file into a Pandas DataFrame −dataFrame = pd.read_csv("C:\Users\amit_\Desktop\SalesData.csv") ExampleFollowing is the code −import pandas as pd # Load data from a CSV file into a Pandas DataFrame dataFrame = pd.read_csv("C:\Users\amit_\Desktop\SalesData.csv") print("DataFrame...", dataFrame) # propagate non null values backward res = dataFrame.fillna(method='bfill') print("DataFrame after backward fill...", res)OutputThis will produce the ... Read More

Python Pandas - Plot a Grouped Horizontal Bar Chart will all the columns

AmitDiwan
Updated on 28-Sep-2021 11:25:34

1K+ Views

For a grouped Horizontal Bar Chart with all the columns, create a Bar Chart using the barh() and do not set the a and y values.At first, import the required libraries −import pandas as pd import matplotlib.pyplot as pltCreate a DataFrame with 3 columns −dataFrame = pd.DataFrame({"Car": ['Bentley', 'Lexus', 'BMW', 'Mustang', 'Mercedes', 'Jaguar'], "Cubic_Capacity": [2000, 1800, 1500, 2500, 2200, 3000], "Reg_Price": [7000, 1500, 5000, 8000, 9000, 6000], })Plotting grouped Horizontal Bar Chart with all the columns −dataFrame.plot.barh(title='Car Specifications', color=("blue", "orange")) ExampleFollowing is the complete code − import pandas as pd import matplotlib.pyplot as plt # creating dataframe dataFrame = ... Read More

Draw a lineplot passing the entire dataset with Seaborn – Python Pandas

AmitDiwan
Updated on 28-Sep-2021 11:20:39

189 Views

Lineplot in Seaborn is used to draw a line plot with possibility of several semantic groupings. The seaborn.lineplot() is used for this. To plot lineplot with entire dataset, simply use the lineplot() and set the complete dataset in it without mentioning the x and y values.Let’s say the following is our dataset in the form of a CSV file − Cricketers2.csvAt first, import the required libraries −import seaborn as sb import pandas as pd import matplotlib.pyplot as pltLoad data from a CSV file into a Pandas DataFrame −dataFrame = pd.read_csv("C:\Users\amit_\Desktop\Cricketers2.csv")Plot lineplot with entire dataset −sb.lineplot(data=dataFrame) ExampleFollowing is the code −import ... Read More

Python Pandas - Create a Bar Plot and style the bars in Seaborn

AmitDiwan
Updated on 28-Sep-2021 11:12:32

272 Views

Bar Plot in Seaborn is used to show point estimates and confidence intervals as rectangular bars. The seaborn.barplot() is used. Style the bars using the facecolor, linewidth and edgecolor parameters.Let’s say the following is our dataset in the form of a CSV file −Cricketers2.csvAt first, import the required libraries −import seaborn as sb import pandas as pd import matplotlib.pyplot as pltLoad data from a CSV file into a Pandas DataFrame −dataFrame = pd.read_csv("C:\Users\amit_\Desktop\Cricketers2.csv") Design the bars −sb.barplot(x=dataFrame["Role"], y=dataFrame["Matches"], facecolor=(1, 1, 0, 0), linewidth=4, edgecolor=sb.color_palette("dark", 2))ExampleFollowing is the code − import seaborn as sb import pandas as pd import matplotlib.pyplot as ... Read More

Python Pandas – How to select DataFrame rows on the basis of conditions

AmitDiwan
Updated on 28-Sep-2021 11:01:52

540 Views

We can set conditions and fetch DataFrame rows. These conditions can be set using logical operators and even relational operators.At first, import the required pandas libraries −import pandas as pdLet us create a DataFrame and read our CSV file −dataFrame = pd.read_csv("C:\Users\amit_\Desktop\SalesRecords.csv") Fetching dataframe rows with registration price less than 1000. We are using relational operator for this −dataFrame[dataFrame.Reg_Price < 1000]ExampleFollowing is the code −import pandas as pd # reading csv file dataFrame = pd.read_csv("C:\Users\amit_\Desktop\SalesRecords.csv") print("DataFrame...", dataFrame) # count the rows and columns in a DataFrame print("Number of rows and column in our DataFrame = ", dataFrame.shape) ... Read More

Advertisements