当前位置: 首页 > 知识库问答 >
问题:

如何解决这个错误......使用a.empty、a.bool()、a.item()、a.any()或a.all()

闻人嘉木
2023-03-14
    import numpy as np
    import pandas as pd

    data = pd.read_excel('F:/Desktop/Downloads/Funds_Holding_12_11_2017.xls')
    M_Cap = pd.read_excel('F:/Desktop/Share_4_Feb_2018.xlsx','CAP(R2)_New')
    Shariah = pd.read_excel('F:/Shariah_List.xlsx')
    Data = data[['Ticker','SecurityName','FundCode','FundName','TotalValueFund','LiquidityNative']]
    Shariah_Data = Shariah[['Ticker','Compliance Status: DEC 2017']]
    Filter_Fund = Data['FundCode'].isin ([14, 16, 17, 24, 25, 49])
    Fund = Data[Filter_Fund]
    total_assets = np.sum(Fund['TotalValueFund'])+np.sum(Fund['LiquidityNative'])
    weights = Fund['TotalValueFund']/total_assets

# 10% Breach Calculation
    Fund['10% Breach'] = weights > 0.1
    Fund['Weightage'] = round(weights*100,2)
    out_1 = Fund[['Ticker','SecurityName','FundCode','FundName','Weightage','10%     Breach','TotalValueFund']]
    Cap_Data = M_Cap[['Ticker','% of Total  Free','S&P Saudi Arabia','Free Float Mrk.','Riyad Income BM']]
    newdata = out_1.merge(Cap_Data, on = 'Ticker', how ='inner')

#Market Cap Calculation
    if Fund == 14:
        newdata['Mkt_Cap'] = newdata['Weightage'] < newdata['% of Total  Free']
    elif Fund == 16:
        newdata['Mkt_Cap_S&P'] = newdata['Weightage'] < newdata['S&P Saudi Arabia']
    CAP = Shariah_Data.merge(Cap_Data_16, on = 'Ticker', how = 'inner')
    newdata = out_1.merge(Cap_Data, on = 'Ticker', how ='inner')
    elif Fund == 17:
        newdata['Mkt_Cap_RI'] = newdata['Weightage'] < newdata['Riyad Income BM']
    elif Fund == 24:
        newdata['Mkt_Cap_Emaar'] = newdata['Weightage'] < newdata['Al Emaar BM %']
    elif Fund == 25:
        newdata['Mkt_Cap_BI'] = newdata['Weightage'] < newdata['SBIF (S&P Sharia)']
    elif Fund == 49:
        newdata['Mkt_Cap_Mid'] = newdata['Weightage'] < newdata['S&P SA Shariah']
    else:
        print("No Data Available")

# 5% Holding Calculation
    Holding = newdata['TotalValueFund'] / (newdata['Free Float Mrk.']*1000000)
    newdata['Holding Weight'] = Holding*100
    newdata['5% Holdings'] = Holding > 0.05
    newdata['% of Total Free'] = newdata['% of Total  Free']*100
    Final_Result = newdata[['Ticker','SecurityName','FundCode','FundName','Weightage','% of Total Free','10% Breach','Mkt_Cap','Mkt_Cap_S&P','Mkt_Cap_RI','Mkt_Cap_Emaar','Mkt_Cap_BI','Mkt_Cap_Mid','5% Holdings','Compliance Status: DEC 2017']]
    Final_Result.to_excel('F:/Monitoring.xlsx')

运行上述操作后,我得到了错误:

ValueError:数据帧的真值不明确。使用a.empty、a.bool()、a.item()、a.any()或a.all()。

请告知我如何解决此问题。

共有1个答案

谭飞掣
2023-03-14

不清楚您试图用代码实现什么。但是,您看到错误的原因很清楚:

data = pd.read_excel('F:/Desktop/Downloads/Funds_Holding_12_11_2017.xls')
# So 'data' is a pandas dataframe

Data = data[['Ticker','SecurityName','FundCode','FundName','TotalValueFund','LiquidityNative']]
# 'Data' is a dataframe as it's a slice of 'data'

Fund = Data[Filter_Fund]
# 'Fund' id a dataframe as it's a slice of 'Data'

weights = Fund['TotalValueFund']/total_assets
# 'weights' id a dataframe as it's a simple function of 'Fund'

Fund['10% Breach'] = weights > 0.1
# 'Fund' is a dataframe as it's a simple function of 'weights'

if Fund == 14:
# You are comparing 'Fund' to an integer.

熊猫感到困惑,认为您正试图通过if Fund检查数据帧的真实性,这是一种不允许的方法,因为它是模糊的。

 类似资料: