Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/svn/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python .diff()函数仅返回数据帧中的NaN值_Python_Pandas_Dataframe_Loops_Iterator - Fatal编程技术网

Python .diff()函数仅返回数据帧中的NaN值

Python .diff()函数仅返回数据帧中的NaN值,python,pandas,dataframe,loops,iterator,Python,Pandas,Dataframe,Loops,Iterator,我想在我的中的log_price列上使用.diff()函数进行循环。我想要的是旧的原木价格值——来自df_DC_产品数据框的新原木价格值。当我尝试在for循环中使用.diff()时,它只返回NaN值。你知道为什么会这样吗?谢谢你的帮助 DC_list = data4['Geography'].drop_duplicates().tolist() Product_List = data4['Product'].drop_duplicates().tolist() # create multipl

我想在我的
中的log_price列上使用.diff()函数进行循环。我想要的是旧的原木价格值——来自df_DC_产品数据框的新原木价格值。当我尝试在for循环中使用
.diff()
时,它只返回NaN值。你知道为什么会这样吗?谢谢你的帮助

DC_list = data4['Geography'].drop_duplicates().tolist()
Product_List = data4['Product'].drop_duplicates().tolist()

# create multiple empty lists to store values in:
my_dict = {
    "Product" : [],
    "Geography" : [],
    "Base Dollar Sales": [],
    "Base Unit Sales" :[],
    "Price Numerator" : [],
    "Price Denominator": [],
    "Demand Numerator" : [],
    "Demand Denominator" : [],
    "% Change in Price" : [],
    "% Change in Demand": [],
    "Price Elasticity of Demand" : []
}
dc_product_ped_with_metrics_all = []

for DC in DC_list:
    
    df_DC = data4.copy()
    # # Filtering to the loop's current DC
    df_DC = df_DC.loc[(df_DC['Geography'] == DC)]
    df_DC = df_DC.copy()
    # Making a list of all of the current DC's Product to loop through
    Product_list = df_DC['Product'].drop_duplicates().tolist()
    
    for Product in Product_list:
        
        df_DC_product = df_DC.copy()
        # # Filtering to the Product
        df_DC_product = df_DC_product.loc[(df_DC_product['Product'] == Product)]
        df_DC_product = df_DC_product.copy()
        
        # create container:
        df_DC_product['pn'] = df_DC_product.iloc[:,5].diff()
        df_DC_product['price_d'] = np.divide(df_DC_product.iloc[:,5].cumsum(),2)
        df_DC_product['dn'] = df_DC_product.iloc[:,6].diff()
        df_DC_product['dd'] = np.divide(df_DC_product.iloc[:,6].cumsum(),2)
        df_DC_product['% Change in Demand'] = np.divide(df_DC_product['dn'],df_DC_product['dd'])*100
        df_DC_product['% Change in Price'] = np.divide(df_DC_product['pn'],df_DC_product['price_d'])*100
        df_DC_product['ped']= np.divide(df_DC_product['% Change in Demand'], df_DC_product['% Change in Price'])
        
        Product = Product,
        DC = DC
        sales = df_DC_product['Base_Dollar_Sales'].sum()
        qty = df_DC_product['Base_Unit_Sales'].sum()
        price = df_DC_product['Price'].mean()
        log_price = df_DC_product['log_price'].mean()
        log_units = df_DC_product['log_units'].sum()
        price_numerator = df_DC_product['pn'].mean()
        price_denominator = df_DC_product['price_d'].sum()
        demand_numerator = df_DC_product['dn'].mean()
        demand_denominator = df_DC_product['dd'].sum()
        delta_demand = df_DC_product['% Change in Demand'].sum()
        delta_price = df_DC_product['% Change in Price'].mean()
        ped = df_DC_product['ped'].mean()
        
        dc_product_ped_with_metrics = [
            Product,
            DC,
            sales,
            qty,
            price,
            price_numerator,
            price_denominator,
            demand_numerator,
            demand_denominator,
            delta_demand,
            delta_price,
            ped
        ]
        
        dc_product_ped_with_metrics_all.append(dc_product_ped_with_metrics)
        
columns = [
    'Product',
    'Geography',
    'Sales',
    'Qty',
    'Price',
    'Price Numerator',
    'Price Denominator',
    'Demand Numerator',
    'Demand Denominator',
    '% Change in Demand',
    '% Change in Price',
    'Price Elasticity of Demand'
]

dc_product_ped_with_metrics_all = pd.DataFrame(data=dc_product_ped_with_metrics_all, columns=columns)
dc_product_ped_with_metrics_all
.append()
不会更新您的数据帧。您需要重新分配数据帧

for DC in DC_list:
    # your code
    for Product in Product_list:
        # your code
        dc_product_ped_with_metrics_all = dc_product_ped_with_metrics_all.append(dc_product_ped_with_metrics)

你能编辑这个问题来关注你的问题吗?@CeliusStingher如果这样更好,请告诉我,谢谢。