0

この種のデータ構造を pandas に保存する慣用的な方法は何ですか:

### Option 1
df = pd.DataFrame(data = [
    {'kws' : np.array([0,0,0]), 'x' : i, 'y', i} for i in range(10)
])

# df.x and df.y works as expected
# the list and array casting is required because df.kws is
# an array of arrays
np.array(list(df.kws))

# this causes problems when trying to assign as well though:
# for any other data type, this would set all kws in df to the rhs [1,2,3]
# but since the rhs is a list, it tried to do an element-wise assignment and
# errors saying that the length of df and the length of the rhs do not match
df.kws = [1,2,3]

### Option 2
df = pd.DataFrame(data = [
    {'kw_0' : 0, 'kw_1' : 0, 'kw_2' : 0, 'x' : i, 'y', i} for i in range(10)
])

# retrieving 2d array:
df[sorted([c for c in df if c.startswith('kw_')])].values

# batch set :
kws = [1,2,3]
for i, kw in enumerate(kws) :
    df['kw_'+i] = kw

これらの解決策はどちらも私には適切ではありません。1 つには、それらのいずれも、すべてのデータをコピーせずに 2 次元マトリックスを取得することはできません。この種の混合次元データを処理するためのより良い方法はありますか、それともパンダが現在取り組んでいないタスクですか?

4

1 に答える 1