如何在不删除其他列的情况下对一列执行tolist(),python

2024-09-17 18:22:10 发布

您现在位置:Python中文网/ 问答频道 /正文

不知道如何在不删除其他列的情况下对一列执行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。你知道吗


Tags: 项目nameviewdf情况itemtoolsprice
3条回答

您可以使用原始解决方案并将结果连接到原始数据帧,删除原始列:

df2 = pd.DataFrame(df.item.tolist(), columns=['Name', 'Price', 'View'], index=df.index)
final_df = df.join(df2).drop("item", axis=1) 

如果您的列包含每行上的列表,则不需要.tolist()将其转换为单独的列。你知道吗

这里有一种可能的方法来解决这个问题

生成一些虚拟数据

import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.rand(10,1), columns=list('A'))
df['category'] = 'Construction'
df['item'] = [[28,0,72168025]]*df.shape[0]
df['subcategory'] = 'tools'
print(df)
          A      category               item subcategory
0  0.972818  Construction  [28, 0, 72168025]       tools
1  0.583059  Construction  [28, 0, 72168025]       tools
2  0.784836  Construction  [28, 0, 72168025]       tools
3  0.393868  Construction  [28, 0, 72168025]       tools
4  0.806041  Construction  [28, 0, 72168025]       tools
5  0.871041  Construction  [28, 0, 72168025]       tools
6  0.573951  Construction  [28, 0, 72168025]       tools
7  0.513052  Construction  [28, 0, 72168025]       tools
8  0.982331  Construction  [28, 0, 72168025]       tools
9  0.713301  Construction  [28, 0, 72168025]       tools

现在,在item列(per this SO postthis one)上使用apply(pd.Series)来获得一个单独的dataframe,其中每个list元素对应于一个单独的pandas序列,并分配列名

df_split = df['item'].apply(pd.Series)
df_split.columns = ['Name', 'Price', 'View']

最后将原始数据帧与新的(拆分的)数据帧连接起来

df = pd.concat([df, df_split], axis=1)

输出

print(df)
          A      category               item subcategory  Name  Price      View
0  0.684692  Construction  [28, 0, 72168025]       tools    28      0  72168025
1  0.404291  Construction  [28, 0, 72168025]       tools    28      0  72168025
2  0.084463  Construction  [28, 0, 72168025]       tools    28      0  72168025
3  0.060698  Construction  [28, 0, 72168025]       tools    28      0  72168025
4  0.096269  Construction  [28, 0, 72168025]       tools    28      0  72168025
5  0.539278  Construction  [28, 0, 72168025]       tools    28      0  72168025
6  0.159661  Construction  [28, 0, 72168025]       tools    28      0  72168025
7  0.651479  Construction  [28, 0, 72168025]       tools    28      0  72168025
8  0.961392  Construction  [28, 0, 72168025]       tools    28      0  72168025
9  0.741887  Construction  [28, 0, 72168025]       tools    28      0  72168025

(可选)删除原始的item

df.drop(['item'], axis=1, inplace=True)
print(df)
          A      category subcategory  Name  Price      View
0  0.833281  Construction       tools    28      0  72168025
1  0.229584  Construction       tools    28      0  72168025
2  0.403571  Construction       tools    28      0  72168025
3  0.822803  Construction       tools    28      0  72168025
4  0.968666  Construction       tools    28      0  72168025
5  0.053424  Construction       tools    28      0  72168025
6  0.759824  Construction       tools    28      0  72168025
7  0.766610  Construction       tools    28      0  72168025
8  0.752378  Construction       tools    28      0  72168025
9  0.056715  Construction       tools    28      0  72168025

编辑:虽然这种方法是可行的,但是有比使用apply更快的方法-参见here。你知道吗

^{}+^{}

加入由列表列表构造的数据帧:

df = df.join(pd.DataFrame(df.pop('item').values.tolist()).add_prefix('item'))

示例

# data from @cronoik
data = [('Construction',[28,0,7216825], 'tools')]
labels = ['category', 'item', 'subcategory']
df = pd.DataFrame.from_records(data, columns=labels)

df = df.join(pd.DataFrame(df.pop('item').values.tolist()).add_prefix('item'))

print(df)

       category subcategory  item0  item1    item2
0  Construction       tools     28      0  7216825

相关问题 更多 >