不知道如何在不删除其他列的情况下对一列执行tolist()。你知道吗
我有三列
category | item | subcategory
Construction | [28, 0, 72168025] | tools
我要打开行李测向项目所以我会得到:
category | name | price | view | subcategory
Construction | 28 | 0 | 72168025 | tools
我做到了:
df = pd.DataFrame(df.item.tolist(), columns=['Name', 'Price', 'View'])
但我得到:
| name | price | view |
| 28 | 0 | 72168025 |
如何将其他列包含到df。你知道吗
您可以使用原始解决方案并将结果连接到原始数据帧,删除原始列:
如果您的列包含每行上的列表,则不需要
.tolist()
将其转换为单独的列。你知道吗这里有一种可能的方法来解决这个问题
生成一些虚拟数据
现在,在
item
列(per this SO post或this one)上使用apply(pd.Series)
来获得一个单独的dataframe,其中每个list元素对应于一个单独的pandas序列,并分配列名最后将原始数据帧与新的(拆分的)数据帧连接起来
输出
(可选)删除原始的
item
列编辑:虽然这种方法是可行的,但是有比使用
apply
更快的方法-参见here。你知道吗^{} +^{}
加入由列表列表构造的数据帧:
示例
相关问题 更多 >
编程相关推荐