Draw Violin Plot and Set Quartiles as Horizontal Lines with Seaborn

AmitDiwan
Updated on 27-Sep-2021 16:27:27

574 Views

Violin Plot in Seaborn is used to draw a combination of boxplot and kernel density estimate. The seaborn.violinplot() is used. Set quartiles as horizontal lines using the inner parameter with value quartile.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 a CSV file into a Pandas DataFrame −dataFrame = pd.read_csv("C:\Users\amit_\Desktop\Cricketers.csv")Plotting violin plot with Role and Age. Control box order by passing an explicit order i.e. ordering on the basis of "Role". Set quartiles as horizontal ... Read More

Renaming Columns of Pandas DataFrame in Python

AmitDiwan
Updated on 27-Sep-2021 13:59:10

989 Views

To rename the columns of a DataFrame, use the rename() method. Set the column names you want to rename under the “columns” parameter of the rename() method. For example, changing “Car” column to “Car Name” −dataFrame.rename(columns={'Car': 'Car Name'}, inplace=False)At first, read the CSV and create a DataFrame −dataFrame = pd.read_csv("C:\Users\amit_\Desktop\SalesRecords.csv") Now, rename the column names. Here, we are renaming the columns “Car”, “Date_of_Purchase” and “Reg_Price” −dataFrame = dataFrame.rename(columns={'Car': 'Car Name', 'Date_of_Purchase': 'Sold On', 'Reg_Price' : 'Booking Price'}, inplace=False)ExampleFollowing is the codeimport pandas as pd # reading csv file dataFrame = pd.read_csv("C:\Users\amit_\Desktop\SalesRecords.csv") print("DataFrame...", dataFrame) # count the rows and ... Read More

Remove Missing NaN Values in DataFrame using Python

AmitDiwan
Updated on 27-Sep-2021 13:50:53

3K+ Views

To remove the missing values i.e. the NaN values, use the dropna() method. At first, let us import the required library −import pandas as pdRead the CSV and create a DataFrame −dataFrame = pd.read_csv("C:\Users\amit_\Desktop\CarRecords.csv") Use the dropna() to remove the missing values. NaN will get displayed for missing values after dropna() is used −dataFrame.dropna()ExampleFollowing is the complete codeimport pandas as pd # reading csv file dataFrame = pd.read_csv("C:\Users\amit_\Desktop\CarRecords.csv") print("DataFrame with some NaN (missing) values...", dataFrame) # count the rows and columns in a DataFrame print("Number of rows and column in our DataFrame = ", dataFrame.shape) # drop ... Read More

Find Summary of Statistics of a Pandas DataFrame in Python

AmitDiwan
Updated on 27-Sep-2021 13:41:37

401 Views

To find the summary of statistics of a DataFrame, use the describe() method. At first, we have imported the following pandas library with an aliasimport pandas as pdFollowing is our CSV file and we are creating a Pandas DataFrame −dataFrame = pd.read_csv("C:\Users\amit_\Desktop\CarRecords.csv")Now, get the summary of statistics of our Pandas DataFrame −dataFrame.describe()ExampleFollowing is the complete codeimport 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) # summary of DataFrame print("Get the summary of statistics ... Read More

Count Rows and Columns in a Pandas DataFrame

AmitDiwan
Updated on 27-Sep-2021 13:29:08

689 Views

To count the rows and columns in a DataFrame, use the shape property. At first, let’s say we have the a CSV file on the Desktop as shown in the below path −C:\Users\amit_\Desktop\CarRecords.csvRead the CSV file −dataFrame = pd.read_csv("C:\Users\amit_\Desktop\CarRecords.csv") Let us now count the rows and columns using shapedataFrame.shapeExampleFollowing is the 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) # returns top 5 row records print("DataFrame with specific number of ... Read More

Display Specific Number of Rows from a DataFrame in Python Pandas

AmitDiwan
Updated on 27-Sep-2021 13:19:53

4K+ Views

To display specific number of rows from a DataFrame, use the head() function. Set the parameter to be the number of row records to be fetched. For example, for 10 rows, mention −dataFrame.head(10)At first, let us import the required library with an alias −import pandas as pd Our CSV is on the Desktop as shown in the below path −C:\Users\amit_\Desktop\CarRecords.csvLet us read the CSV file and create Pandas DataFrame −dataFrame = pd.read_csv("C:\Users\amit_\Desktop\CarRecords.csv") Return specific number of rows i.e. in this case, we are returning top 5 row records −dataFrame.head(5)ExampleFollowing is the code import pandas as pd # reading csv ... Read More

Iterate and Fetch Rows Containing Desired Text in Python Pandas

AmitDiwan
Updated on 27-Sep-2021 13:13:57

255 Views

To iterate and fetch the rows containing the desired text, use the itertuples() and find() method. The itertuples() iterate over DataFrame rows.At first, let us import the required library with an alias −import pandas as pdOur CSV is on the Desktop as shown in the below path −C:\Users\amit_\Desktop\CarRecords.csv Let us read the CSV file and create Pandas DataFrame −dataFrame = pd.read_csv("C:\Users\amit_\Desktop\CarRecords.csv")Iterate and fetch the rows containing a specific text. We are fetching Car column with text “Lamborghini” −for k in dataFrame.itertuples():    if k[1].find('Lamborghini') != -1: print(k)ExampleFollowing is the code import pandas as pd ... Read More

Merge All Excel Files in a Folder Using Python

AmitDiwan
Updated on 27-Sep-2021 12:43:43

11K+ Views

To merge all excel files in a folder, use the Glob module and the append() method.Let’s say the following are our excel files on the Desktop −Sales1.xlsxSales2.xlsxNote − You may need to install openpyxl and xlrd packages.At first, set the path where all the excel files you want to merge are located. Get the excel files and read them using glob −path = "C:\Users\amit_\Desktop\" filenames = glob.glob(path + "\*.xlsx") print('File names:', filenames)Next, create an empty dataframe for the merged output excel file that will get the data from the above two excel files −outputxlsx = pd.DataFrame()Now, the actual process ... Read More

Sort CSV by a Single Column in Python

AmitDiwan
Updated on 27-Sep-2021 12:33:40

7K+ Views

To sort CSV by a single column, use the sort_values() method. Set the column using which you want to sort in the sort_values() method.At first, let’s read our CSV file “SalesRecords.csv”with DataFrame −dataFrame = pd.read_csv("C:\Users\amit_\Desktop\SalesRecords.csv")Sort according to a single column “Car” −dataFrame.sort_values("Car", axis=0, ascending=True, inplace=True, na_position='first')Next, sort according to a single column “Reg_Price” −dataFrame.sort_values("Reg_Price", axis=0, ascending=True, inplace=True, na_position='first')ExampleFollowing is the codeimport pandas as pd # DataFrame to read our input CS file dataFrame = pd.read_csv("C:\Users\amit_\Desktop\SalesRecords.csv") print("Input CSV file = ", dataFrame) # sorting according to Car column dataFrame.sort_values("Car", axis=0, ascending=True, inplace=True, na_position='first') print("Sorted CSV file (according to ... Read More

Read All CSV Files in a Folder using Pandas

AmitDiwan
Updated on 27-Sep-2021 12:15:29

6K+ Views

To read all excel files in a folder, use the Glob module and the read_csv() method. Let’s say the following are our excel files in a directory −At first, let us set the path and get the csv files. Our CSV files are in the folder MyProject −path = "C:\Users\amit_\Desktop\MyProject\"Read files with extension .csv from the above path −filenames = glob.glob(path + "\*.csv") Let us now write a for loop to iterate all csv files, read and print them −for file in filenames: # reading csv files print("Reading file = ", file) ... Read More

Advertisements