Я пытаюсь создать список кортежей в фрейме данных. Используя код ниже:
# creating the Numpy array
array = np.array([[('A' , 1)], [('B' , 2)]])
# creating a list of index names
index_values = ['x1', 'x2']
# creating a list of column names
column_values = ['(a,b)']
# creating the dataframe
df = pd.DataFrame(data = array,
index = index_values,
columns = column_values)
df
возвращает:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
/tmp/ipykernel_45/2020978637.py in <module>
13 df = pd.DataFrame(data = array,
14 index = index_values,
---> 15 columns = column_values)
16
17 df
/opt/oss/conda3/lib/python3.7/site-packages/pandas/core/frame.py in __init__(self, data, index, columns, dtype, copy)
676 dtype=dtype,
677 copy=copy,
--> 678 typ=manager,
679 )
680
/opt/oss/conda3/lib/python3.7/site-packages/pandas/core/internals/construction.py in ndarray_to_mgr(values, index, columns, dtype, copy, typ)
302 # by definition an array here
303 # the dtypes will be coerced to a single dtype
--> 304 values = _prep_ndarray(values, copy=copy)
305
306 if dtype is not None and not is_dtype_equal(values.dtype, dtype):
/opt/oss/conda3/lib/python3.7/site-packages/pandas/core/internals/construction.py in _prep_ndarray(values, copy)
553 values = values.reshape((values.shape[0], 1))
554 elif values.ndim != 2:
--> 555 raise ValueError(f"Must pass 2-d input. shape = {values.shape}")
556
557 return values
ValueError: Must pass 2-d input. shape=(2, 1, 2)
Использование кортежа из одного элемента:
array = np.array([[(1)], [(2)]])
То, как вы создаете массив numpy, неверно. Поскольку это массив кортежей, вам нужно будет указать dtype
элементов кортежа при создании массива, а затем привести его обратно к типу объекта, используя astype(object)
.
Сделайте следующее -
array = np.array([[('A',1)], [('B',2)]], dtype=('<U10,int')).astype(object)
index_values = ['x1', 'x2']
column_values = ['(a,b)']
df = pd.DataFrame(data = array, index = index_values, columns = column_values)
Выход:
>>> df
(a,b)
x1 (A, 1)
x2 (B, 2)
Можете ли вы показать пример ожидаемого результата?