Python Forum
Look up or assign value from another column - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Look up or assign value from another column (/thread-29151.html)



Look up or assign value from another column - Hilda_Python - Aug-20-2020

Hi. new coder here.
I have to find the oldest and newest housing block and find the price difference between them. I need to find the max/min and lookup or assign the corresponding house value from the 'house_value' column.
I can't get the price difference. it keeps coming up with a tuple error.

in advance, thnks. dammit, it's so easy but so hard.

import pandas as pd
import numpy as np
import math 

# read datafile 
df=pd.read_csv ("housing_dataset.csv")
df2=df[['housing_age','house_value']]

#find oldest house etc
oldest_house = np.max(df2)
newest_house = np.min(df2)
print ("The oldest house is : " , oldest_house)
print ("The newest house is : " , newest_house)

#find the house value
x = (oldest_house, 'house_value')
y = (newest_house, 'house_value')

print(x)
print(y)

diff = x - y
print(diff)
Output:
The oldest house is : housing_age 52 house_value 500001 dtype: int64 The newest house is : housing_age 1 house_value 14999 dtype: int64 (housing_age 52 house_value 500001 dtype: int64, 'house_value') (housing_age 1 house_value 14999 dtype: int64, 'house_value')
Error:
TypeError Traceback (most recent call last) <ipython-input-292-83b8727a34d2> in <module> 19 print(y) 20 ---> 21 diff = x - y 22 print(diff) TypeError: unsupported operand type(s) for -: 'tuple' and 'tuple'