将设置值一起添加到数据框单元格中

时间:2018-06-30 23:41:58

标签: python pandas dataframe set series

我有一个数据集,其中一个列是其中之一。我正在尝试将集合的值加在一起并创建一个新列。有什么想法吗?

输入数据框:

1     {0.4154521378005839, 0.5341809649069569, 0.35...  }                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
3     {0.31367362283294203, 0.37413260754622946, 0.... }                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
4     {0.38943794667290854, 0.37413260754622946, 0....}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
5     {0.31367362283294203, 0.3816072438174095, 0.4...} 

输出数据框:

1     {0.4154521378005839, 0.5341809649069569, 0.35...  }   1.29                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
3     {0.31367362283294203, 0.37413260754622946, 0.... }    0.68                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
4     {0.38943794667290854, 0.37413260754622946, 0....}     ...                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
5     {0.31367362283294203, 0.3816072438174095, 0.4...}     ...

谢谢。

1 个答案:

答案 0 :(得分:1)

您可以将pd.Series.apply与内置sum一起使用:

df = pd.DataFrame({'sets': [{1, 2, 3}, {4, 5, 6}, {7, 8, 9}]})

df['sum'] = df['sets'].apply(sum)

print(df)

        sets  sum
0  {1, 2, 3}    6
1  {4, 5, 6}   15
2  {8, 9, 7}   24