Compare values in multiple columns and add a new value in another column in Python

ahbon

I have a house rent price data as follows:

import pandas as pd
import numpy as np
data = {
    "HouseName": ["A", "A", "B", "B", "B"],
    "Type": ["OneRoom", "TwoRooms", "OneRoom", "TwoRooms", "ThreeRooms"],
    "Jan_S": [1100, 1776, 1228, 1640, np.NaN],
    "Feb_S": [1000, 1805, 1231, 1425, 1800],
    "Mar_S": [1033, 1748, 1315, 1591, 2900],
    "Jan_L": [1005, np.NaN, 1300, np.NaN, 7000]
}
df = pd.DataFrame.from_dict(data)
print(df)

  HouseName        Type   Jan_S  Feb_S  Mar_S   Jan_L 
0         A     OneRoom  1100.0   1000   1033  1005.0 
1         A    TwoRooms  1776.0   1805   1748     NaN 
2         B     OneRoom  1228.0   1231   1315  1300.0 
3         B    TwoRooms  1640.0   1425   1591     NaN 
4         B  ThreeRooms     NaN   1800   2900  7000.0 

I need to realize two things: first, I want to find a reasonable rent price for January based on columns 'Jan_S', 'Feb_S', 'Mar_S', 'Jan_L'. Here S and L mean two different data sources, both of them may have outliers and nans but data from S will be taken as final price for January at priority. Second, For the same HouseName I need to check and make sure that the price of one room is lower than two rooms, and prices of two rooms is lower than three rooms. My final results will look like this:

HouseName        Type    Jan_S    Feb_S  Mar_S   Jan_L  
0         A     OneRoom  1100.0   1000   1033  1005.0     
1         A    TwoRooms  1776.0   1805   1748     NaN     
2         B     OneRoom  1228.0   1231   1315  1300.0   
3         B    TwoRooms  1640.0   1425   1591     NaN   
4         B  ThreeRooms     NaN   1800   2900  7000.0    

      Result(Jan)  
0         1100  
1         1776  
2         1228  
3         1640  
4         1800  

My idea is check if Jan_S is in range of 0.95 and 1.05 of Jan_L, if yes, take Jan_S as final result, otherwise, continue to check a value from Feb_S as Jan_S.

Please share any ideas that you might have to deal with this problem in Python. Thanks! Here are some references which may helps.

Find nearest value from multiple columns and add to a new column in Python

Compare values under multiple conditions of one column in Python

Check if values in one column is in interval values of another column in Python

MrE

You can use fillna for this.

If you want to have a conditional on selection of columns, then you need to figure the logic to filter the columns to pick the values from.

I'm showing the logic using the min() of all price columns

# filter out the price columns
price_cols = df.columns[~df.columns.isin(['HouseName','Type', 'Jan_S'])]

# then figure out the logic to filter the columns you need and use fillna
# here with the min of all columns as example
df['Jan_S'] = df['Jan_S'].fillna(df[price_cols].apply(min, axis=1))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Add new columns and insert values in columns based on value in another column

Add new columns with values paired to another column

Find nearest value from multiple columns and add to a new column in Python

Compare two or more columns values only if another column value is True

Compare Multiple DataFrames Add New Column Fill With Binary Values For Matches

How to compare one column's value to multiple values in other columns?

Python Pandas: How to compare values of cells and two columns and maybe using If...Else statement to create another column with new values

Python - DataFrame: Multiply multiple columns by another column and save in new columns

Add a new column to a dataframe based on multiple columns from another dataframe

Add new columns to data frame for each unique value in another column

Compare 2 columns value in a table and show the matching values in a new column . column values are comma seperated

How to Add a New Column With Selected Values from Another Column In Python

Add a new column if multiple columns have negative value

Add a new column that specify multiple columns name that have value

Add value to new column depending on values in another in pandas

How to compare columns and add a new column

Compare values in DGV column with database values and add value to new DGV column

Pandas df: fill values in new column with specific values from another column (condition with multiple columns)

Use aggreagted row values in multiple columns to form new column in Python

Python: display multiple values in a column for a single value in another column

Pandas/Python: Store values of columns into list based on value in another column

Python add new column with repeating value based on two other columns

TSQL-Compare values of two columns and add 0 or 1 to another column of the same table

Is there a way to compare two columns of a dataframe containing float values and create a new column to add labels based on it?

Fill Value in a Column Based on multiple conditions in Another Columns (Python)

Python pandas: add new columns based on the existed a column value, and set the value of new columns as 1 or 0

Creating new columns from the values of another column

convert multiple columns +- values to single column binary value(python, pandas)

Pandas : Compare the columns of a data frame and add a new column & value based on a condition