Found 507 Articles for Pandas

How to reset index in Pandas dataframe?

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

306 Views

In this program, we will replace or, in other words, reset the default index in the Pandas dataframe. We will first make a dataframe and see the default index and then replace this default index with our custom index.AlgorithmStep 1: Define your dataframe. Step 2: Define your own index. Step 3: Replace the default index with your index using the reset function in Pandas library.Example Codeimport pandas as pd dataframe = {'Name':["Allen", "Jack", "Mark", "Vishal"], 'Marks':[85, 92, 99, 87]} df = pd.DataFrame(dataframe) print("Before using reset_index:", df) own_index = ['a', 'j', 'm', 'v'] df = pd.DataFrame(dataframe, own_index) ... Read More

Python Pandas – How to use Pandas DataFrame tail( ) function

Vani Nalliappan
Updated on 27-May-2024 11:21:00

480 Views

Write a Python code to find price column value between 30000 to 70000 and print the id and product columns of the last three rows from the products.csv file. Result for price column value between 30000 to 70000 and id and product columns last three rows are −    id product 79 80 Truck 81 82 Bike 98 99 TruckSolution 1 Read data from products.csv file and assign to dfdf = pd.read_csv('products.csv ')Apply pandas slicing to access all rows of price column between 30000 to 50000 as, df[df.iloc[:, 4].between(30000, 50000)Save the above result to df1Apply slicing to access last ... Read More

Python Pandas – How to use Pandas DataFrame Property: shape

Vani Nalliappan
Updated on 29-Feb-2024 13:29:10

740 Views

Write a Python program to read data from the products.csv file and print the number of rows and columns. Then print the ‘product’ column value matches ‘Car’ for first ten rowsAssume, you have ‘products.csv’ file and the result for number of rows and columns and ‘product’ column value matches ‘Car’ for first ten rows are −Rows: 100 Columns: 8  id product engine avgmileage price height_mm width_mm productionYear 1 2  Car    Diesel    21      16500    1530    1735       2020 4 5  Car    Gas       18      17450    1530   ... Read More

Python Pandas - Read data from a CSV file and print the ‘product’ column value that matches ‘Car’ for the first ten rows

Vani Nalliappan
Updated on 17-Feb-2021 06:26:32

748 Views

Assume, you have ‘products.csv’ file and the result for a number of rows and columns and ‘product’ column value matches ‘Car’ for the first ten rows are −Download the products.csv file here.Rows: 100 Columns: 8 id    product    engine    avgmileage    price    height_mm    width_mm    productionYear 1 2    Car       Diesel       21         16500       1530          1735         2020 4 5    Car         Gas        18         17450   ... Read More

Write a program in Python to verify camel case string from the user, split camel cases, and store them in a new series

Vani Nalliappan
Updated on 25-Feb-2021 07:26:35

670 Views

The result for splitting camel case strings into series as, enter the sring: pandasSeriesDataFrame Series is: 0    pandas 1    Series 2    Data 3    Frame dtype: objectTo solve this, we will follow the steps given below −SolutionDefine a function that accepts the input stringSet result variable with the condition as input is not lowercase and uppercase and no ’_’ in input string. It is defined below, result = (s != s.lower() and s != s.upper() and "_" not in s)Set if condition to check if the result is true the apply re.findall method to find camel case ... Read More

Write a Python code to combine two given series and convert it to a dataframe

Vani Nalliappan
Updated on 25-Feb-2021 07:25:31

148 Views

Assume, you have two series and the result for combining two series into dataframe as,  Id Age 0 1 12 1 2 13 2 3 12 3 4 14 4 5 15To solve this, we can have three different approaches.Solution 1Define two series as series1 and series2Assign first series into dataframe. Store it as dfdf = pd.DataFrame(series1)Create a column df[‘Age’] in dataframe and assign second series inside to df.df['Age'] = pd.DataFrame(series2)ExampleLet’s check the following code to get a better understanding −import pandas as pd series1 = pd.Series([1, 2, 3, 4, 5], name='Id') series2 = pd.Series([12, 13, 12, 14, 15], name='Age') ... Read More

Write a program in Python to split the date column into day, month, year in multiple columns of a given dataframe

Vani Nalliappan
Updated on 25-Feb-2021 07:22:14

11K+ Views

Assume, you have a dataframe and the result for a date, month, year column is,       date  day  month  year 0 17/05/2002 17   05    2002 1 16/02/1990 16   02    1990 2 25/09/1980 25   09    1980 3 11/05/2000 11   05    2000 4 17/09/1986 17   09    1986To solve this, we will follow the steps given below −SolutionCreate a list of dates and assign into dataframe.Apply str.split function inside ‘/’ delimiter to df[‘date’] column. Assign the result to df[[“day”, “month”, “year”]].ExampleLet’s check the following code to get a better understanding −import ... Read More

Write a Python code to convert a given series into a dummy variable and drop any NaN values if they exist

Vani Nalliappan
Updated on 25-Feb-2021 07:20:58

131 Views

Assume, you have a series and the result for converting to dummy variable as,    Female Male 0    0    1 1    1    0 2    0    1 3    1    0 4    0    1 5    0    0 6    1    0 7    1    0To solve this, we will follow the steps given below −SolutionCreate a list with ‘Male’ and ‘Female’ elements and assign into Series.Apply get_dummies function inside series and set dummy_na value as False. It is defined below, pd.get_dummies(series, dummy_na=False)ExampleLet’s check the following code to get ... Read More

Write a program in Python to convert a given dataframe to a LaTex document

Vani Nalliappan
Updated on 25-Feb-2021 07:20:06

130 Views

Assume, you have a dataframe and the result for converted to latex as, \begin{tabular}{lrr} \toprule {} &   Id &  Age \ \midrule 0 &    1 &    12 \ 1 &    2 &    13 \ 2 &    3 &    14 \ 3 &    4 &    15 \ 4 &    5 &    16 \ \bottomrule \end{tabular}SolutionTo solve this, we will follow the steps given below −Define a dataframeApply to_latex() function to the dataframe and set index and multirow values as True. It is defined below, df.to_latex(index = True, multirow = True)ExampleLet’s ... Read More

Write a program in Python to generate an even (length) series of random four-digit pin. Get the length from user and ask until it’s valid

Vani Nalliappan
Updated on 25-Feb-2021 07:18:12

281 Views

The result for generating even length random four-digit pin numbers as, enter the series size 4 Random four digit pin number series 0    0813 1    7218 2    6739 3    8390To solve this, we will follow the steps given below −SolutionCreate an empty and list and set result as TrueSet while loop and get the size from the userSet if condition to find the size is even or odd. If the size is odd then assign the result as False and runs the loop until an even number is entered.l = [] while(True):    size = int(input("enter ... Read More

Advertisements