小编Can*_*ice的帖子

将熊猫系列中的嵌套列表解压缩到新的DataFrame中

我有一个数据框df,它的column [“ shares”]似乎是列表中的一个列表

0     [[10], [9], [9]]
1     [[3], [3], [2]]
2     [[17], [17], [18]]
Run Code Online (Sandbox Code Playgroud)

如何将此列分为3列:

col1 col2 col3
10   9     9
3    3     2
17   17    18
Run Code Online (Sandbox Code Playgroud)

我尝试过df["shares"].apply(literal_eval) 但是它给我错误的节点或字符串格式错误:

python dataframe pandas

3
推荐指数
2
解决办法
202
查看次数

Python pandas 中 Excel 索引匹配的等效项

如果它是 excel,我可以使用索引匹配。我有一个数据框 df = pd.DataFrame(np.random.randn(200,5),columns = ['apple','pear','orange','mango','banana'])

    apple      pear    orange     mango    banana
0   -1.162567  0.488261  1.716845 -1.375144 -0.510948
1   -0.344498 -1.096802 -0.544039 -0.106573 -0.316679
2    0.097983 -0.313277  0.572100 -0.176696 -0.574828
3   -1.300936 -2.749289 -0.065648  1.072607  2.099388
4    0.956781 -1.036766  0.794087  1.962683 -2.087505
5   -2.619787  1.024262  1.025925 -0.763013  0.942017
...
Run Code Online (Sandbox Code Playgroud)

我还有一个由 200 个项目组成的列表:['apple','orange','mango',mango','pear'...] 如何遍历 df 中的行并根据列名获取值列表:所需的输出:

     values     
0   -1.162567  
1   -0.544039
2    -0.176696
3   1.072607
4   -1.036766
...
Run Code Online (Sandbox Code Playgroud)

python pandas

1
推荐指数
1
解决办法
3994
查看次数

标签 统计

pandas ×2

python ×2

dataframe ×1