Python 创建合并列表中其他两列内容的新列
假设我有一个Python 创建合并列表中其他两列内容的新列,python,pandas,dataframe,Python,Pandas,Dataframe,假设我有一个DataFrame,如下所示: 我想创建一个新的列,它的值是单元格中的第2列和第3列合并到一个列表中 i、 e 有什么办法吗?我得到这个错误: ValueError: Length of values does not match length of index 当我尝试这样做时: DF['combined'] = [DF['chicago_bound1'], DF['chicago_bound2']] 尝试: 或 您可以按整数切片的位置进行选择,并将其输出到列表中 在这种情况
DataFrame
,如下所示:
我想创建一个新的列
,它的值是单元格中的第2列和第3列合并到一个列表中
i、 e
有什么办法吗?我得到这个错误:
ValueError: Length of values does not match length of index
当我尝试这样做时:
DF['combined'] = [DF['chicago_bound1'], DF['chicago_bound2']]
尝试:
或
您可以按整数切片的位置进行选择,并将其输出到列表中 在这种情况下,您的选择将是
df.iloc[0:2,1:3]
foo = df.iloc[0:2, 1:3].values.tolist()
df['combined']= foo
输出:
chicago chicago_bound1 chicago_bound2 combined
0 -7541.18 -8589.95 -6492.41 [-8589.95, -6492.41]
1 -612.89 -1475.30 249.52 [-1475.3, 249.52]
df['combined'] = df.apply(lambda x: [[x.chicago_bound1, x.chicago_bound2]], axis=1)
foo = df.iloc[0:2, 1:3].values.tolist()
df['combined']= foo
chicago chicago_bound1 chicago_bound2 combined
0 -7541.18 -8589.95 -6492.41 [-8589.95, -6492.41]
1 -612.89 -1475.30 249.52 [-1475.3, 249.52]