我能够在综合数据上重现该错误:
import pandas as pd
from datetime import datetime
df1 = pd.DataFrame({'A': ['A0', 'A1', 'A2', 'A3'],
'B': ['B0', 'B1', 'B2', 'B3'],
'C': [datetime.now(), datetime.now(), datetime.now(), datetime.now()],
'D': ['D0', 'D1', 'D2', 'D3']},
index=[0, 1, 2, 3]);
df2 = pd.DataFrame({'A': ['A1', 'A2', 'A3', 'A4'],
'E': ['E1', 'E2', 'E3', 'E4']},
index=[0,1,2,3]);
df = pd.merge(df1, df2, how='left', on=['A', 'A']);
def getList(row):
r = [];
if row["A"] == "A1": r.append("test-01");
if row["B"] == "B1": r.append("test-02");
if row["B"] == "B2": r.append("test-03");
return r;
df["NEW_COLUMN"] = df.apply(lambda row: getList(row), axis = 1);
原始帖子:我想基于多种条件在pandas数据框中创建一个新列。新列的值应为list。但是我收到“ ValueError:指定索引传递的空数据。” 如果列表为空。
def getList(p_row):
r = [];
if p_row["field1"] > 0: r.append("x");
...
return r;
df["new_list_field"] = df.apply(lambda row: getList(row), axis = 1);
完整的错误:
ValueError追溯(最近一次通话最近)C:\ ProgramData \ Anaconda3 \ lib \ site-packages \ pandas \ core \ internals.py在create_block_manager_from_arrays(数组,名称,轴)4636中尝试:-> 4637块= form_blocks(数组,名称) ,轴)4638 mgr = BlockManager(块,轴)
C:\ ProgramData \ Anaconda3 \ lib \ site-packages \ pandas \ core \ internals.py in form_blocks(数组,名称,轴)4728如果len(object_items)> 0:-> 4729 object_blocks = _simple_blockify(object_items,np.object_ )4730 blocks.extend(object_blocks)
C:\ ProgramData \ Anaconda3 \ lib \ site-packages \ pandas \ core \ internals.py in _simple_blockify(tuples,dtype)4758“”“-> 4759值,位置= _stack_arrays(tuples,dtype)4760
C:\ ProgramData \ Anaconda3 \ lib \ site-packages \ pandas \ core \ internals.py in _stack_arrays(tuples,dtype)4822 for i,enr in enumerate(arrays):-> 4823 Stacked [i] = _asarray_compat(arr) 4824
ValueError:无法将输入数组从形状(2)广播到形状(195)
人到中年有点甜
慕姐4208626
相关分类