Python 如何在不删除其他列的情况下将tolist()放在一列上,请参见
不知道如何在不删除其他列的情况下对一列执行tolist() 我有三个专栏Python 如何在不删除其他列的情况下将tolist()放在一列上,请参见,python,pandas,Python,Pandas,不知道如何在不删除其他列的情况下对一列执行tolist() 我有三个专栏 category | item | subcategory Construction | [28, 0, 72168025] | tools 我想打开df.item的包装,这样我就可以得到: category | name | price | view | subcategory Construction | 28
category | item | subcategory
Construction | [28, 0, 72168025] | tools
我想打开df.item的包装,这样我就可以得到:
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。您可以使用原始解决方案,将结果加入原始数据框,删除原始列:
df2 = pd.DataFrame(df.item.tolist(), columns=['Name', 'Price', 'View'], index=df.index)
final_df = df.join(df2).drop("item", axis=1)
您可以使用原始解决方案,将结果加入原始数据框,删除原始列:
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
现在,在项
列(或)上使用apply(pd.Series)
获得一个单独的数据框,其中每个列表元素对应一个单独的系列,并指定列名
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
(可选)删除原始项
列
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
更快的方法-请参阅。如果列由每行上的列表组成,则不需要.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
现在,在项
列(或)上使用apply(pd.Series)
获得一个单独的数据框,其中每个列表元素对应一个单独的系列,并指定列名
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
(可选)删除原始项
列
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
更快的方法-请参阅。您之所以得到这个结果,是因为您从项目列创建了一个新的数据框。实际上,您要做的是向现有数据帧添加新列:
import pandas as pd
data = [('Construction',[28,0,7216825], 'tools')]
labels = ['category', 'item', 'subcategory']
df = pd.DataFrame.from_records(data, columns=labels)
#Adding the new columns based on the split
df[['name','price', 'view']] = pd.DataFrame(df.item.tolist())
#dropping the unneeded item column
df.drop('item', axis=1, inplace=True )
之所以得到这个结果,是因为您从item列创建了一个新的dataframe。实际上,您要做的是向现有数据帧添加新列:
import pandas as pd
data = [('Construction',[28,0,7216825], 'tools')]
labels = ['category', 'item', 'subcategory']
df = pd.DataFrame.from_records(data, columns=labels)
#Adding the new columns based on the split
df[['name','price', 'view']] = pd.DataFrame(df.item.tolist())
#dropping the unneeded item column
df.drop('item', axis=1, inplace=True )
+
加入从列表列表构造的数据帧:
df = df.join(pd.DataFrame(df.pop('item').values.tolist()).add_prefix('item'))
例子
+
加入从列表列表构造的数据帧:
df = df.join(pd.DataFrame(df.pop('item').values.tolist()).add_prefix('item'))
例子