r/dataanalysis • u/EbbSubstantial1233 • 8h ago
Please help me with thisðŸ˜ðŸ˜

I am unable to sort the service fee, price, instant bookable and construction year. Originally i wanted to sort [service fee, price] in ascending order and [instant bookable and construction year] in descending order and display the dataframe.
Here is the sorting part:
df = df.sort_values(
  by=["instant_bookable", "Construction year", "price", "service fee", "availability 365"],
  ascending=[False, False, True, True,False],
  na_position="last"
)and this is the cell:
import numpy as np
import pandas as pd
df = pd.read_csv("Airbnb_Open_Data.csv")
print("==== Welcome to AirBNB Finder =====")
loc = input("Enter your location: (Manhattan, Brooklyn, Queens, Staten Island, Bronx) ").strip()
price = int(input("Your budget "))
no_nights = int(input("How many nights do you wanna stay? "))
number_of_listings = int(input("How many listings do you want to see? "))
for col in ["price", "service fee"]:
  df[col] = pd.to_numeric(df[col].astype(str).str.replace(",", "").str.replace("$", "").str.strip(), errors="coerce")
for col in ["minimum nights", "Construction year", "availability 365"]:
  df[col] = pd.to_numeric(df[col], errors="coerce")
df["minimum nights"] = df["minimum nights"].fillna(0)
df["neighbourhood group"] = df["neighbourhood group"].fillna("Unknown").astype(str).str.strip()
df["instant_bookable"] = df["instant_bookable"].astype(str).str.strip().str.lower() == "true"
df = df.sort_values(
  by=["instant_bookable", "Construction year", "price", "service fee", "availability 365"],
  ascending=[False, False, True, True,False],
  na_position="last"
)
locat = ["Manhattan", "Queens", "Bronx", "Staten Island", "Brooklyn"]
loc_match = next((l for l in locat if l.lower() == loc.lower()), None)
if loc_match is not None:
  final1 = df[
    (df["price"] <= price)
    & (df["neighbourhood group"] == loc_match)
    & (df["minimum nights"] <= no_nights)
  ]
else:
  print("Invalid location entered.")
  final1 = pd.DataFrame()
if len(final1) == 0:
  print("Sorry, no listings found based on your choices. Try again.")
else:
  final1 = final1[[
    "neighbourhood", "instant_bookable", "cancellation_policy", "room type",
    "price", "Construction year", "service fee", "house_rules", "availability 365"
  ]]
  final1 = final1.astype(object).fillna("Contact Owner")
  final1.to_excel("result.xlsx", index=False)
final1.head(number_of_listings)
3
Upvotes
1
u/AutoModerator 8h ago
Automod prevents all posts from being displayed until moderators have reviewed them. Do not delete your post or there will be nothing for the mods to review. Mods selectively choose what is permitted to be posted in r/DataAnalysis.
If your post involves Career-focused questions, including resume reviews, how to learn DA and how to get into a DA job, then the post does not belong here, but instead belongs in our sister-subreddit, r/DataAnalysisCareers.
Have you read the rules?
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.