Я пытаюсь создать фрейм данных pandas, в котором столбцы являются массивами.Я также хочу назвать столбцы при создании.
Это кажется очень простой задачей.
Работает нормально без имен столбцов, хотя столбцы расположены в неправильном порядке:
import numpy as np
import pandas as pd
n_obs = 500
df = pd.DataFrame(np.random.uniform(low = 1.1, high = 5.0,size = (n_obs) ) , np.random.randint(size = (n_obs), low = 18, high = 80))
print(df.head())
Вывод:
49 3.802458
57 3.830600
29 4.991442
47 2.600079
70 1.658041
52 2.236296
37 3.327520
23 1.366954
22 1.509165
36 1.289901
77 3.834789
68 4.370223
40 4.532152
71 2.348842
Когда я пытаюсь назватьВ столбцах я получаю сообщение об ошибке:
df = pd.DataFrame(np.random.uniform(low = 1.1, high = 5.0,size = (n_obs) ) , np.random.randint(size = (n_obs), low = 18, high = 80), columns =['col1','col2'])
Вывод:
Traceback (most recent call last):
File "C:\Users\GBUHR4\AppData\Local\Continuum\anaconda3\lib\site-packages\pand
as\core\internals.py", line 4622, in create_block_manager_from_blocks
placement=slice(0, len(axes[0])))]
File "C:\Users\GBUHR4\AppData\Local\Continuum\anaconda3\lib\site-packages\pand
as\core\internals.py", line 2957, in make_block
return klass(values, ndim=ndim, fastpath=fastpath, placement=placement)
File "C:\Users\GBUHR4\AppData\Local\Continuum\anaconda3\lib\site-packages\pand
as\core\internals.py", line 120, in __init__
len(self.mgr_locs)))
ValueError: Wrong number of items passed 1, placement implies 2
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "fake.py", line 33, in <module>
df = pd.DataFrame(np.random.uniform(low = 1.1, high = 5.0,size = (n_obs) ) ,
np.random.randint(size = (n_obs), low = 18, high = 80), columns =['col1','col2'
])
File "C:\Users\Me\AppData\Local\Continuum\anaconda3\lib\site-packages\pand
as\core\frame.py", line 361, in __init__
copy=copy)
File "C:\Users\Me\AppData\Local\Continuum\anaconda3\lib\site-packages\pand
as\core\frame.py", line 533, in _init_ndarray
return create_block_manager_from_blocks([values], [columns, index])
File "C:\Users\Me\AppData\Local\Continuum\anaconda3\lib\site-packages\pand
as\core\internals.py", line 4631, in create_block_manager_from_blocks
construction_error(tot_items, blocks[0].shape[1:], axes, e)
File "C:\Users\Me\AppData\Local\Continuum\anaconda3\lib\site-packages\pand
as\core\internals.py", line 4608, in construction_error
passed, implied))
ValueError: Shape of passed values is (1, 500), indices imply (2, 500)
Я не могу найти учебник, который охватывает это.Это, очевидно, очень простая проблема, но я не могу найти решение.