Dan*_*ler 8 python dataframe dask
我有一个 dask 数据框和一个 dask 数组,它们的行数相同,逻辑顺序相同。数据帧行由字符串索引。我正在尝试将数组列之一添加到数据框中。我尝试了几种方法,但都以它们特定的方式失败了。
df['col'] = da.col
# TypeError: Column assignment doesn't support type Array
df['col'] = da.to_frame(columns='col')
# TypeError: '<' not supported between instances of 'str' and 'int'
df['col'] = da.to_frame(columns=['col']).set_index(df.col).col
# TypeError: '<' not supported between instances of 'str' and 'int'
df = df.reset_index()
df['col'] = da.to_frame(columns='col')
# ValueError: Not all divisions are known, can't align partitions. Please use `set_index` to set the index.
Run Code Online (Sandbox Code Playgroud)
和其他一些变体。
当结构在逻辑上兼容时,将 dask 数组列添加到 dask 数据帧的正确方法是什么?
解决方案是取出原始Dask数据帧的索引列作为普通的pandas数据帧,将Dask数组列添加到其中,然后通过索引列将其合并回Dask数据帧
index_col = df['index'].compute()
index_col['new_col'] = da.col.compute()
df = df.merge(index_col, 'left', on='index')
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2624 次 |
| 最近记录: |