Python 使用数据框中的要素创建计算器

Python 使用数据框中的要素创建计算器,python,pandas,Python,Pandas,我想创建一个计算器来计算Airbnb房间的平均价格,当我们给邻居、床、浴室、卧室计数作为输入时,这些特征已经在数据集中给出了 邻居、床、卧室、浴室和价格是数据集中的功能,,,请帮助如果您提供更多详细信息并提出具体问题,将会有所帮助 可通过以下方式计算熊猫的平均价格: import pandas as pd df = pd.read_csv(path_to_file.csv) # assuming the file has all the relevant fields def calcula

我想创建一个计算器来计算Airbnb房间的平均价格,当我们给邻居、床、浴室、卧室计数作为输入时,这些特征已经在数据集中给出了
邻居、床、卧室、浴室和价格是数据集中的功能,,,请帮助

如果您提供更多详细信息并提出具体问题,将会有所帮助

可通过以下方式计算熊猫的平均价格:

import pandas as pd

df = pd.read_csv(path_to_file.csv) # assuming the file has all the relevant fields

def calculate_price(row):
    return row['price_per_room'] * row['number_of_rooms'] * row['number_of_nights']

df['price'] = df.apply(calculate_price)

average_price = df['price'].mean()

print(f"The average price is {average_price }")

## use group by to aggregate across categories

希望这有帮助

我不确定这是否是您真正需要的,您应该更好地指定您的问题,添加示例数据、首选输出、您的代码……,但groupby可能会很有用。。。大概是这样的:

df = pd.DataFrame({
    'neighbourhood' : ['nice', 'not so nice', 'nice', 'awesome', 'not so nice'],
    'room_type' : ['a', 'a', 'b', 'b', 'a']
    'beds': [7,2,1,6,6],
    'bedrooms': [3,1,1,3,2],
    'bathrooms': [2,1,1,1,1],
    'price': [220,100,125,320,125]
})

print('Mean of all prices:\n', df['price'].mean())
print('\nMean grouped by neighbourhood:\n', df.groupby(['neighborhood']).mean().price)
print('\nMean grouped by more cols:\n', df.groupby(['neighbourhood', 'beds', 'bedrooms']).mean().price) 
# select requested data data in loc[...] and then apply groupby
df_filtered = df.loc[(df['neighbourhood']=='nice') & (df['beds']==1)]
df_filtered.groupby('neighbourhood')['price'].mean()
# or the same on one line:
df.loc[(df['neighbourhood']=='nice') & (df['beds']==1)].groupby('neighbourhood')['price'].mean()
def calculate_price(air_df):
    a = str(input("Enter the Neighbourhood : "))
    b = str(input("Enter the Room Type : "))
    c = float(input("Enter number of Beds : "))
    d = float(input("Enter number of Bedrooms : "))
    e = float(input("Enter number of Bathrooms : "))
    return air_df.loc[
        (air_df['neighbourhood']==a) & 
        (air_df['room_type']==b) &
        (air_df['beds']==c) &
        (air_df['bedrooms']==d) &
        (air_df['bathrooms']==e)
    ].groupby('neighbourhood')['price'].mean()
输出:

Mean of all prices:
 178.0

Mean grouped by neighbourhood:
 neighbourhood
awesome        320.0
nice           172.5
not so nice    112.5

Mean grouped by more cols:
 neighbourhood  beds  bedrooms
awesome         6     3           320
nice            1     1           125
                7     3           220
not so nice     2     1           100
                6     2           125
您还可以在应用groupy之前过滤数据帧,例如:

df = pd.DataFrame({
    'neighbourhood' : ['nice', 'not so nice', 'nice', 'awesome', 'not so nice'],
    'room_type' : ['a', 'a', 'b', 'b', 'a']
    'beds': [7,2,1,6,6],
    'bedrooms': [3,1,1,3,2],
    'bathrooms': [2,1,1,1,1],
    'price': [220,100,125,320,125]
})

print('Mean of all prices:\n', df['price'].mean())
print('\nMean grouped by neighbourhood:\n', df.groupby(['neighborhood']).mean().price)
print('\nMean grouped by more cols:\n', df.groupby(['neighbourhood', 'beds', 'bedrooms']).mean().price) 
# select requested data data in loc[...] and then apply groupby
df_filtered = df.loc[(df['neighbourhood']=='nice') & (df['beds']==1)]
df_filtered.groupby('neighbourhood')['price'].mean()
# or the same on one line:
df.loc[(df['neighbourhood']=='nice') & (df['beds']==1)].groupby('neighbourhood')['price'].mean()
def calculate_price(air_df):
    a = str(input("Enter the Neighbourhood : "))
    b = str(input("Enter the Room Type : "))
    c = float(input("Enter number of Beds : "))
    d = float(input("Enter number of Bedrooms : "))
    e = float(input("Enter number of Bathrooms : "))
    return air_df.loc[
        (air_df['neighbourhood']==a) & 
        (air_df['room_type']==b) &
        (air_df['beds']==c) &
        (air_df['bedrooms']==d) &
        (air_df['bathrooms']==e)
    ].groupby('neighbourhood')['price'].mean()
最后一条评论中的函数可能如下所示:

df = pd.DataFrame({
    'neighbourhood' : ['nice', 'not so nice', 'nice', 'awesome', 'not so nice'],
    'room_type' : ['a', 'a', 'b', 'b', 'a']
    'beds': [7,2,1,6,6],
    'bedrooms': [3,1,1,3,2],
    'bathrooms': [2,1,1,1,1],
    'price': [220,100,125,320,125]
})

print('Mean of all prices:\n', df['price'].mean())
print('\nMean grouped by neighbourhood:\n', df.groupby(['neighborhood']).mean().price)
print('\nMean grouped by more cols:\n', df.groupby(['neighbourhood', 'beds', 'bedrooms']).mean().price) 
# select requested data data in loc[...] and then apply groupby
df_filtered = df.loc[(df['neighbourhood']=='nice') & (df['beds']==1)]
df_filtered.groupby('neighbourhood')['price'].mean()
# or the same on one line:
df.loc[(df['neighbourhood']=='nice') & (df['beds']==1)].groupby('neighbourhood')['price'].mean()
def calculate_price(air_df):
    a = str(input("Enter the Neighbourhood : "))
    b = str(input("Enter the Room Type : "))
    c = float(input("Enter number of Beds : "))
    d = float(input("Enter number of Bedrooms : "))
    e = float(input("Enter number of Bathrooms : "))
    return air_df.loc[
        (air_df['neighbourhood']==a) & 
        (air_df['room_type']==b) &
        (air_df['beds']==c) &
        (air_df['bedrooms']==d) &
        (air_df['bathrooms']==e)
    ].groupby('neighbourhood')['price'].mean()

我想要的是,我想装一个计算器,就像我输入邻里=漂亮,床=2,卧室=3。。。它应该根据这些投入计算平均价格。。。我们能做到这一点吗?@Aswin Babu,我已经更新了上面的示例。您只需根据您的条件使用loc,只选择您需要的数据,然后计算平均价格。def calculate_priceair_df:a=strinputer邻居:b=strinputer房间类型:c=floatinputer床位数:d=floatinputer卧室数:e=floatinputer浴室数:. @卢卡斯,兄弟。。。像这样的东西。。。我想为这些特性中的每个值提供输入,并根据我们提供的输入得到平均价格。。。有没有可能做一个这样的计算器…请帮助。。。我是编程新手..def calculate\u priceair\u df:a=strinputter邻居:b=strinputter房间类型:c=floatinputter床位数:d=floatinputter卧室数:e=floatinputter浴室数:。我想要这样的东西。。。我想为这些特性中的每个值提供输入,并根据我们提供的输入得到平均价格。。。有没有可能做一个这样的计算器…请帮助。。。我是编程新手。请将从用户获取这些值的代码与进行计算的代码分开,并显示结果。为这些编写单独的函数,然后在主代码中使用。