熊猫输入类型列表?

时间:2018-08-28 14:24:40

标签: python pandas

熊猫中的列可以为np.array类型吗?或python列表等? 我想拥有

    a         array_col
0   100       [1,2,8,0]
1   5         [1,3 4,8]      
2   3         [1,2 4,8]     

如果相关,所有列的长度将相同。 我要执行的功能是将所有array_col初始化为[0,0,... 0],然后针对每一行根据该行以及上一个array(e.g on row n, multiply all the array by n, and make array_col[n] = 0
另一种方法是为每个索引创建一个新列(在本例中为30个新列,并使操作慢得多)。
想法?
编辑:
我想要这样的东西:

df

    a         
0   100       
1   5               
2   3 

df['hist'] = [1,2,3]


df

    a         array_col
0   100       [1,2,3]
1   5         [1,2,3]      
2   3         [1,2,3]

但是两者

df['hist'] = [1,2,3]

df['hist'] = np.zeros(3)

给出错误

ValueError: Length of values does not match length of index

1 个答案:

答案 0 :(得分:1)

您的示例可以这样解决:

df['hist'] = [[1, 2, 3] for _ in range(df.size)]
df
#     a       hist
#0  100  [1, 2, 3]
#1    5  [1, 2, 3]
#2    3  [1, 2, 3]

或者:

df['hist'] = [np.zeros(3) for _ in range(df.size)]
df
#     a             hist
#0  100  [0.0, 0.0, 0.0]
#1    5  [0.0, 0.0, 0.0]
#2    3  [0.0, 0.0, 0.0]

并获得所需的输出-分配嵌套列表:

df['hist'] = [[1,2,8,0],[1,3,4,8],[1,2,4,8]]
df
#     a          hist
#0  100  [1, 2, 8, 0]
#1    5  [1, 3, 4, 8]
#2    3  [1, 2, 4, 8]