如何在不获得空值的情况下创建数据帧?

How to create a dataframe without getting empty values?

提问人:Noob Coder 提问时间:9/27/2023 最后编辑:Noob Coder 更新时间:9/27/2023 访问量:61

问:

我一直在使用一种算法,并且也获得了所需的输出,但是当我尝试将两个输出转换为特定的数据帧时,我总是收到空的数据帧,谁能告诉我哪里出了问题?我只想将这两个输出列表及其数据保存在单独的数据帧中。正如您在我的输出中看到的那样:推荐酒店有一个列表,所有其他酒店都有一个列表,我想要两个单独的数据帧,仅此而已。谢谢。

import pandas as pd


def recommend_hotels(data, weights):

  df = pd.DataFrame(data)

  # Ask the user for the passenger count
  passenger_count = int(input("Enter the number of passengers: "))

  # Calculate the minimum number of rooms needed
  min_rooms_needed = passenger_count

  # Calculate the difference between total room availability and the passenger count
  df['room_diff'] = df['total_rooms'] - min_rooms_needed

  # Filter hotels with higher availability than the passenger count and sort by room availability in ascending order
   df = df[df['room_diff'] >= 0].sort_values(by='total_rooms')

  # If there are no hotels with higher availability, sort by room availability in ascending order
  if df.empty:
    df = data.copy()
    df = df.sort_values(by='total_rooms')

  # Initialize variables to track room assignments
  assigned_rooms = []
  remaining_passengers = passenger_count

  # Assign double rooms first
  for index, row in df.iterrows():
    double_rooms_available = row['double_rooms']
    if double_rooms_available > 0:
        assigned_double_rooms = min(double_rooms_available, remaining_passengers // 2)
        remaining_passengers -= assigned_double_rooms * 2
        assigned_rooms.append((row['hotel_name'], assigned_double_rooms, 'double'))

  # Assign single rooms if needed
   for index, row in df.iterrows():
    if remaining_passengers > 0:
        single_rooms_available = row['single_rooms']
        assigned_single_rooms = min(single_rooms_available, remaining_passengers)
        remaining_passengers -= assigned_single_rooms
        assigned_rooms.append((row['hotel_name'], assigned_single_rooms, 'single'))

  # Recommend hotels based on room assignments
    recommended_hotels = [hotel[0] for hotel in assigned_rooms]

  # Calculate the total number of rooms to book for all passengers
  total_rooms_to_book = min_rooms_needed

  # Create a DataFrame for recommended hotels
   recommended_hotels_df = 
 df[df['hotel_name'].isin(recommended_hotels)].sort_values(by='total_rooms', ascending=False)
recommended_hotels_df
# Print the initial recommendations
print(f"Recommended Hotels: {', '.join(recommended_hotels)}")
print(f"Book {total_rooms_to_book} rooms for {passenger_count} passengers.")

# List unrecommended hotels in descending order of room availability
unrecommended_hotels = set(data['hotel_name']) - set(recommended_hotels)

if unrecommended_hotels:
    unrecommended_df = df[df['hotel_name'].isin(unrecommended_hotels)].sort_values(by='total_rooms', ascending=False)
    #print(f"\nUnrecommended Hotels (Not in Recommended List) in Descending Order of Availability:")
    for index, row in unrecommended_df.iterrows():
        print(f"{row['hotel_name']} - Total Rooms: {row['total_rooms']}")

# List all other hotels that are not in the recommended list
other_hotels = set(data['hotel_name']) - set(recommended_hotels)

print(f"\nAll Other Hotels (Not in Recommended List): {', '.join(other_hotels)}")

# Create a DataFrame for all other hotels that are not in the recommended list
other_hotels_df = df[df['hotel_name'].isin(other_hotels)]

# Define your data and weights
 data = {
'hotel_name': ['A', 'B', 'C', 'D', 'E'],
'single_rooms': [10, 25, 8, 12, 7],
'double_rooms': [35, 18, 44, 110, 6],
'total_rooms': [45, 43, 52, 122, 13],
 }

 weights = {
 'num_hotels': 0.35,
 'transport': 0.3,
 'room_price': 0.15,
 'meals': 0.1,
 'availability': 0.075,
 'rooms': 0.025,
}
other_hotels_df


# Call the recommend_hotels function with your data and weights
recommend_hotels(data, weights)

我得到的输出是:enter image description here

我尝试的是:

other_hotels = set(data['hotel_name']) - set(recommended_hotels) # 
   
other_hotels_df =df[df['hotel_name'].isin(other_hotels)].sort_values(by='total_rooms', ascending=False). 

预期输出:

Recommended_df:
 |hotel_name |single_rooms |double_rooms |total_rooms|
 |B|25|18|43|
 |A|10|35|45|
 |C|8|44|52|
 |D |12|110|122|



UnRecommended_df:
 |hotel_name |single_rooms |double_rooms |total_rooms|
 |E|7|6|13|
python pandas 数据帧 列表 numpy

评论

0赞 renzo21 9/27/2023
函数的输入是什么样子的?你能举一些例子吗?dataweights
0赞 Noob Coder 9/27/2023
@renzo21它只是随机的,无论我们给它什么输入,它总是打印一个列表。但是对于我的例子,我使用 20 作为我的乘客编号,算法完成了它的工作。权重和数据已经在代码中指定,这给了我指定的输出。谢谢。
1赞 Timeless 9/27/2023
您没有展示如何尝试创建此“DataFrame”。顺便说一句,你能精确地说明它应该是什么样子吗?
0赞 renzo21 9/27/2023
我们必须看到一个示例和预期的输出来提供帮助dataweights
0赞 Noob Coder 9/27/2023
@renzo21@Timeless我刚刚编辑,请告诉我是否需要任何其他信息。

答:

1赞 Timeless 9/27/2023 #1

一个快速的解决方案是替换这一行(在您的代码中为 74):

    other_hotels_df = df[df['hotel_name'].isin(other_hotels)]

有了这两个:

    other_hotels_df = pd.DataFrame(data).loc[lambda x: x['hotel_name'].isin(other_hotels)]
    
    return recommended_hotels_df[list(data)[:4]], other_hotels_df[list(data)[:4]]

测试/输出 :

Enter the number of passengers:  20 # <-- input value
Recommended Hotels: B, A, C, D
Book 20 rooms for 20 passengers.

All Other Hotels (Not in Recommended List): E

然后,您可以在两个不同的 DataFrame 中解压缩结果:

rec_df, unrec_df = recommend_hotels(data, weights)

>>> print(rec_df)

  hotel_name  single_rooms  double_rooms  total_rooms
3          D            12           110          122
2          C             8            44           52
0          A            10            35           45
1          B            25            18           43

>>> print(unrec_df)

  hotel_name  single_rooms  double_rooms  total_rooms
4          E             7             6           13