亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

值錯誤:。使用 a.empty、a.bool()、a.item()、a.any()

值錯誤:。使用 a.empty、a.bool()、a.item()、a.any()

忽然笑 2021-10-19 10:44:35
我使用 CSV 文件作為輸入并生成 JSON 格式文件以輸入 kafka 主題df = pd.read_csv(csv_file, delimiter=",",                 dtype={'E': 'S10', 'C': 'S10', 'Date': 'S10', 'TimeCode': 'S10',                          'Workrule': 'S10'})common.time_calc(df) #time_calc is the function from adf = df.drop(['Workrule'], axis=1)在我有的功能上def time_calc(df_entry):    if (df_entry['TimeCode'] == 'R') and (df_entry['Workrule'] == 'C'):        df_entry['TimeCode'] = 'A'    if df_entry['TimeCode'] in ['O', 'L']:        df_entry['TimeCode'] = 'O'我得到ValueError:系列的真值不明確。使用 a.empty、a.bool()、a.item()、a.any() 或 a.all()。我嘗試將代碼修改為if (df_entry['TimeCode'] == 'R') & (df_entry['Workrule'] == 'C'):        df_entry['TimeCode'] = 'A'但仍然得到同樣的錯誤。添加了以下內容,現在可以發布了。謝謝!json_data = df.to_json(orient='records')json_input = '{"value":' + json_data + '}'decodedJson = json.loads(json_input) for entry in decodedJson['value']: common.time_calc(entry) del entry['Workrule']
查看完整描述

2 回答

?
侃侃無極

TA貢獻2051條經驗 獲得超10個贊

您的函數time_calc將 aDataFrame作為參數。在部分中df_entry['TimeCode'] == 'R',當您將整個列與標量值進行比較時,您實際上計算了一個系列。


當您and對此使用邏輯時,python 會嘗試計算boolean系列的等效項,從而引發異常。您實際打算做的是使用向量運算或循環遍歷行。


固定代碼的示例可以是(未測試):


def time_calc(df):

    df.loc[df['TimeCode'] == 'R' & df['Workrule'] == 'C', 'TimeCode'] = 'A'

    df.loc[df['TimeCode'].isin(['O', 'L']), 'TimeCode'] = 'O'


查看完整回答
反對 回復 2021-10-19
?
慕勒3428872

TA貢獻1848條經驗 獲得超6個贊

您正在將整個列與單個值進行比較df_entry['TimeCode'] =='R'。您需要逐行迭代以比較單列值或更好地使用np.where


def time_calc(df_entry):

    df_entry['TimeCode'] = np.where((df_entry['TimeCode'] == 'R') and (df_entry['Workrule'] == 'C'), 'A', df_entry['TimeCode'])

    df_entry['TimeCode'] = np.where(df_entry['TimeCode'] in ['O','L'], 'O', df_entry['TimeCode'])



查看完整回答
反對 回復 2021-10-19
  • 2 回答
  • 0 關注
  • 296 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號