Как сравнить значения в двух фреймах данных Pandas?
Давайте обсудим, как сравнивать значения в фрейме данных Pandas. Вот шаги для сравнения значений в двух фреймах данных pandas:
Step 1 Dataframe Creation: The dataframes for the two datasets can be created using the following code:
import pandas as pd # elements of first datasetfirst_Set = {"Prod_1": ["Laptop", "Mobile Phone", "Desktop", "LED"], "Price_1": [25000, 8000, 20000, 35000] } # creation of Dataframe 1df1 = pd.DataFrame(first_Set, columns = ["Prod_1", "Price_1"])print(df1) # elements of second datasetsecond_Set = {"Prod_2": ["Laptop", "Mobile Phone", "Desktop", "LED"], "Price_2": [25000, 10000, 15000, 30000] } # creation of Dataframe 2df2 = pd.DataFrame(second_Set, columns = ["Prod_2", "Price_2"])print (df2) |
Output:


Step 2 Comparison of values: You need to import numpy for the successful execution of this step. Here is the general template to perform the comparison:
df1[‘new column for the comparison results’] = np.where(condition, ‘value if true’, ‘value if false’)
Example: After execution of this code, the new column with the name Price_Matching will be formed under df1. Columns result will be displayed according to the following conditions:
- If Price_1 is equal to Price_2, then assign the value of True
- Otherwise, assign the value of False.
# add the Price2 column from # df2 to df1df1["Price_2"] = df2["Price_2"] # create new column in df1 to # check if prices matchdf1["Price_Matching"] = np.where(df1["Price_1"] == df2["Price_2"], "True", "False") df1 |
Выход:

Внимание компьютерщик! Укрепите свои основы с помощью базового курса программирования Python и изучите основы.
Для начала подготовьтесь к собеседованию. Расширьте свои концепции структур данных с помощью курса Python DS. А чтобы начать свое путешествие по машинному обучению, присоединяйтесь к курсу Машинное обучение - базовый уровень.