使用列表理解从索引列表列表生成 pandas DataFrame 列
Generate pandas DataFrame column from list of lists of indexes using list comprehension
这是我编写的应用于 df 的每一行的函数
def use_list_of_list_of_indexes(column1, column2):
"""
Column that contains list of lists of indices is used to generate a new column based on indices
_
Args:
column1 - list of elements
column2 - list of lists of indexes
_
Returns:
pandas.core.series.Series
"""
list_of_lists = column1.apply(lambda row: [[row[i] for i in x] for x in column2])
return list_of_lists
这里是df
col1 col2
[55, 475,243,60] [[0,1,2], [0,1,3], [0,2,3], [1,2,3]]
使用时
df['col3'] = use_list_of_list_of_indexes(df.col1, df.col2)
我想得到
col3
[[55, 475, 243], [55, 475, 60], [55, 243, 60], [475, 243, 60]]
但是,我得到错误
TypeError: list indices must be integers or slices, not list
将 [0]
添加到 df.col2
解决了问题,但是我应该如何重写 use_list_of_list_of_indexes 函数才能将其应用于整个列?
df['col3'] = use_list_of_list_of_indexes(df.col1, df.col2[0])
如果你必须这样做,你就错过了一个循环:
column1.apply(lambda row: [[[row[z] for z in y] for y in x] for x in column2])
x 给你 [[0,1,2], [0,1,3], [0,2,3], [1,2,3]]
y 给你 [0,1,2]
所以你必须使用 z
来遍历 y
稍微好一点的做法:
df['col3'] = df[['col1', 'col2']].apply(lambda r: [[r['col1'][y] for y in x] for x in r['col2']], axis = 1)
这是我编写的应用于 df 的每一行的函数
def use_list_of_list_of_indexes(column1, column2):
"""
Column that contains list of lists of indices is used to generate a new column based on indices
_
Args:
column1 - list of elements
column2 - list of lists of indexes
_
Returns:
pandas.core.series.Series
"""
list_of_lists = column1.apply(lambda row: [[row[i] for i in x] for x in column2])
return list_of_lists
这里是df
col1 col2
[55, 475,243,60] [[0,1,2], [0,1,3], [0,2,3], [1,2,3]]
使用时
df['col3'] = use_list_of_list_of_indexes(df.col1, df.col2)
我想得到
col3
[[55, 475, 243], [55, 475, 60], [55, 243, 60], [475, 243, 60]]
但是,我得到错误
TypeError: list indices must be integers or slices, not list
将 [0]
添加到 df.col2
解决了问题,但是我应该如何重写 use_list_of_list_of_indexes 函数才能将其应用于整个列?
df['col3'] = use_list_of_list_of_indexes(df.col1, df.col2[0])
如果你必须这样做,你就错过了一个循环:
column1.apply(lambda row: [[[row[z] for z in y] for y in x] for x in column2])
x 给你 [[0,1,2], [0,1,3], [0,2,3], [1,2,3]]
y 给你 [0,1,2]
所以你必须使用 z
来遍历 y
稍微好一点的做法:
df['col3'] = df[['col1', 'col2']].apply(lambda r: [[r['col1'][y] for y in x] for x in r['col2']], axis = 1)