Found 26504 Articles for Server Side Programming

Python Pandas - Display specific number of rows from a DataFrame

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

Python Pandas - Iterate and fetch the rows that contain the desired text

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

226 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

Filter the rows – Python Pandas

SaiKrishna Tavva
Updated on 23-Sep-2024 16:57:04

7K+ Views

In Python Pandas, filtering the rows and fetching any specific column values can be done in serval ways, one of which is by using the Pandas contains() method. Usually, this method is applied to columns that are of the string type, to filter rows based on the sub-string ( i.e. by verifying whether the column contains a specific substring). Steps Involved The Steps involved in filtering the rows in pandas are as follows Reading a CSV File ... Read More

How to Sort CSV by a single column in Python ?

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

6K+ 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

Python - Read all CSV files in a folder in 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

Python Pandas - Draw a violin plot and set quartiles as horizontal lines with Seaborn

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

539 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

Python Pandas - Draw swarms of observations on top of a box plot with Seaborn

AmitDiwan
Updated on 27-Sep-2021 12:05:11

244 Views

Swarm Plot in Seaborn is used to draw a categorical scatterplot with non-overlapping points. The seaborn.swarmplot() is used for this. Draw swarms of observations on top of a box plot using the seaborn.boxplot().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 numpy as np import matplotlib.pyplot as pltLoad data from a CSV file into a Pandas DataFrame −dataFrame = pd.read_csv("C:\Users\amit_\Desktop\Cricketers2.csv") Draw swarms of observations on top of a box plot −sb.boxplot(x = "Matches", y = "Role", data= dataFrame, whis=np.inf) sb.swarmplot(x ... Read More

Python Pandas - Draw a swarm plot and control swarm order by passing an explicit order with Seaborn

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

650 Views

Swarm Plot in Seaborn is used to draw a categorical scatterplot with non-overlapping points. The seaborn.swarmplot() is used for this. Control swarm order by passing an explicit order i.e. ordering on the basis of a specific column using the order parameter −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") Plotting swarm plot with Academy and Matches. Control swarm order by passing an explicit order ... Read More

Python Pandas - Group the swarms by two categorical variables with Seaborn

AmitDiwan
Updated on 27-Sep-2021 11:47:21

435 Views

Swarm Plot in Seaborn is used to draw a categorical scatterplot with non-overlapping points. The seaborn.swarmplot() is used for this. To group the swarms by two categorical variables, set those variables in the swarmplot() using the x, y or hue 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") Group the swarms by two categorical variables −sb.swarmplot(x = "Role", y = "Matches", hue = "Academy", ... Read More

Python Pandas - Draw a violin plot and control order by passing an explicit order with Seaborn

AmitDiwan
Updated on 27-Sep-2021 11:40:22

384 Views

Violin Plot in Seaborn is used to draw a combination of boxplot and kernel density estimate. The seaborn.violinplot() is used for this. Set explicit order using the order parameter of the violinplot().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 order by passing an explicit order i.e. ordering on the basis of "Role".sb.violinplot(x = 'Role', y ... Read More

Advertisements