Python Forum
Look up or assign value from another column
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Look up or assign value from another column
#1
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'
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Get an average of the unique values of a column with group by condition and assign it klllmmm 0 289 Feb-17-2024, 05:53 PM
Last Post: klllmmm
  How to assign a value to pandas dataframe column rows based on a condition klllmmm 0 849 Sep-08-2022, 06:32 AM
Last Post: klllmmm
  Pandas, Assign a value in a row, to another column based on a condition klllmmm 6 5,516 Oct-16-2020, 04:43 PM
Last Post: klllmmm

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020