Found 26504 Articles for Server Side Programming

How to read all excel files under a directory as a Pandas DataFrame ?

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

12K+ Views

To read all excel files in a directory, use the Glob module and the read_excel() method.Let’s say the following are our excel files in a directory −Sales1.xlsxSales2.xlsxAt first, set the path where all the excel files 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, use the for loop to iterate and read all the excels files in a specific directory. We are also using read_excel() −for file in filenames: print("Reading file = ", file) print(pd.read_excel(file))ExampleFollowing is the complete code ... Read More

How to Merge multiple CSV Files into a single Pandas dataframe ?

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

2K+ Views

To merge more than one CSV files into a single Pandas dataframe, use read_csv. At first, import the required Pandas library. Here. We have set pd as an alias −import pandas as pdNow, let’s say the following are our CSV Files −Sales1.csvSales2.csvWe have set the path as string. Both the files are on the Desktop −file1 = "C:\Users\amit_\Desktop\sales1.csv" file2 = "C:\Users\amit_\Desktop\sales2.csv"Next, merge the above two CSV files. The pd.concat() merge the CSV files together −dataFrame = pd.concat( map(pd.read_csv, [file1, file2]), ignore_index=True)ExampleFollowing is the code −import pandas as pd file1 = "C:\Users\amit_\Desktop\sales1.csv" file2 = "C:\Users\amit_\Desktop\sales2.csv" print("Merging ... Read More

Find the Nth Even Length Palindrome using C++

prateek jangid
Updated on 27-Sep-2021 10:58:29

795 Views

If you ever used C + +, then you must have heard about Palindrome numbers. So in this guide, we will explain everything about "Nth even-length Palindrome" using appropriate examples. Palindrome numbers are numbers that stay the same after reversing them. Not only numbers but a word whose spelling stays the same when its characters are reversed. For Example −Numbers = {1, 121, 131, 656, 1221, 1551}Words = {saas, malayalam, level, mom}It looks complicated but very easy to perform on any system. So let's discuss the palindrome in brief.Nth Even length Palindrome Number11, 22, 33, 44, 55, 66, 77, 88, ... Read More

Python - How to Concatenate more than two Pandas DataFrames?

Akshitha Mote
Updated on 22-Jan-2025 13:44:05

1K+ Views

In this article, we will explore how to concatenate more than two pandas DataFrames using the pandas and numpy modules. Typically, the pandas.concat() method is used to concatenate multiple data frames. This method allows for concatenation along rows (axis=0) or columns (axis=1), providing flexibility in combining data efficiently. A DataFrame in Python's pandas library is a two-dimensional labeled data structure that is used for data manipulation and analysis. It can handle different data types such as integers, floats, and strings. Each column has a unique label, and each row is labeled with a unique index value, which helps in ... Read More

Python - Stacking a multi-level column in a Pandas DataFrame

AmitDiwan
Updated on 22-Sep-2021 12:43:49

2K+ Views

To stack a multi-level column, use the stack() method. At first, import the required library −import pandas as pdCreate a multi-level column −items = pd.MultiIndex.from_tuples([('Maths', 'Mental Maths'), ('Maths', 'Discrete Mathematics'), ('Maths', 'Applied Mathematics')]) Now, create a DataFrame and set multi-level columns we set above −dataFrame = pd.DataFrame([[67, 86, 78], [56, 92, 97], [92, 95, 91]], index=['John', 'Tom', 'Henry'], columns=items)Stack the multi-level column −dataframe.stack()ExampleFollowing is the complete code −import pandas as pd # multi-level columns items = pd.MultiIndex.from_tuples([('Maths', 'Mental Maths'), ('Maths', 'Discrete Mathematics'), ('Maths', 'Applied Mathematics')]) # creating a DataFrame dataFrame = pd.DataFrame([[67, 86, 78], [56, 92, 97], ... Read More

Python Pandas – Create a subset and display only the last entry from duplicate values

AmitDiwan
Updated on 22-Sep-2021 12:26:25

1K+ Views

To create a subset and display only the last entry from duplicate values, use the “keep” parameter with the ‘last” value in drop_duplicates() method. The drop_duplicates() method removed duplicates.Let us first create a DataFrame with 3 columns −dataFrame = pd.DataFrame({'Car': ['BMW', 'Mercedes', 'Lamborghini', 'BMW', 'Mercedes', 'Porsche'], 'Place': ['Delhi', 'Hyderabad', 'Chandigarh', 'Delhi', 'Hyderabad', 'Mumbai'], 'UnitsSold': [85, 70, 80, 95, 55, 90]})Removing duplicates and displaying last entry. Using keep parameter, we have set "last". Duplicate rows except the last entry will get deleted. We have considered a subset using the “subset” parameter −dataFrame2 = dataFrame.drop_duplicates(subset = ['Car', 'Place'], keep ='last').reset_index(drop = True)ExampleFollowing ... Read More

Python – Get the Columns Shared by Two Pandas DataFrames using Numpy

AmitDiwan
Updated on 22-Sep-2021 12:19:15

269 Views

To get the columns shared by two DataFrames, use the intersect1d() method. This method is provided by numpy, so you need to import Numpy also with Pandas. Let us first import the required libraries −import pandas as pd import numpy as npCreate two DataFrames −# creating dataframe1 dataFrame1 = pd.DataFrame({"Car": ['Bentley', 'Lexus', 'Tesla', 'Mustang', 'Mercedes', 'Jaguar'], "Cubic_Capacity": [2000, 1800, 1500, 2500, 2200, 3000], "Reg_Price": [7000, 1500, 5000, 8000, 9000, 6000], "Units_Sold": [ 100, 110, 150, 80, 200, 90] }) # creating dataframe2 dataFrame2 = pd.DataFrame({"Car": ['BMW', 'Lexus', 'Tesla', 'Mustang', 'Mercedes', 'Jaguar'], "Units_Sold": [ 100, 110, 150, 80, 200, 90] ... Read More

Merge Pandas DataFrame with a common column

AmitDiwan
Updated on 22-Sep-2021 12:12:13

11K+ Views

To merge two Pandas DataFrame with common column, use the merge() function and set the ON parameter as the column name.At first, let us import the pandas library with an alias −import pandas as pdLet us create the 1st DataFrame −dataFrame1 = pd.DataFrame(    {       "Car": ['BMW', 'Lexus', 'Audi', 'Mustang', 'Bentley', 'Jaguar'], "Units": [100, 150, 110, 80, 110, 90] } )Next, create the 2nd DataFrame −dataFrame2 = pd.DataFrame(    {       "Car": ['BMW', 'Lexus', 'Audi', 'Mustang', 'Mercedes', 'Jaguar'], "Reg_Price": [7000, 1500, 5000, 8000, 9000, 6000] } )Now, merge ... Read More

Python – Merge two Pandas DataFrame

AmitDiwan
Updated on 22-Sep-2021 12:02:26

780 Views

To merge two Pandas DataFrame, use the merge() function. Just set both the DataFrames as a parameter of the merge() function.At first, let us import the required library with alias “pd” −import pandas as pdCreate the 1st DataFrame −# Create DataFrame1 dataFrame1 = pd.DataFrame(    {       "Car": ['BMW', 'Lexus', 'Audi', 'Mustang', 'Bentley', 'Jaguar'], "Units": [100, 150, 110, 80, 110, 90]    } )Next, create the 2nd DataFrame −# Create DataFrame2 dataFrame2 = pd.DataFrame(    {       "Car": ['BMW', 'Lexus', 'Audi', 'Mustang', 'Mercedes', 'Jaguar'], "Reg_Price": [7000, 1500, 5000, 8000, 9000, 6000] } ... Read More

How to append a list to a Pandas DataFrame using iloc in Python?

AmitDiwan
Updated on 22-Sep-2021 11:49:13

1K+ Views

The iloc method is an integer-location based indexing for selection by position. We are using iloc to append a list to a DataFrame.Let us first create a DataFrame. The data is in the form of lists of team rankings for our example −# data in the form of list of team rankings Team = [['India', 1, 100], ['Australia', 2, 85], ['England', 3, 75], ['New Zealand', 4 , 65], ['South Africa', 5, 50], ['Bangladesh', 6, 40]] # Creating a DataFrame and adding columns dataFrame = pd.DataFrame(Team, columns=['Country', 'Rank', 'Points'])Following is the row to be appended −myList = ["Sri Lanka", 7, ... Read More

Advertisements