汇总数据框中的设置值

时间:2019-05-28 13:48:09

标签: python pandas dataframe

我试图对dataFrame中的一组列进行求和。

类似于:['xxxx',{1,2,3}],而我需要['xxxx',6]

感谢您的帮助。


 for index,row in df_clusters.iterrows():
        if isinstance(row['sum_coefs'],set):
            row.loc['sum_coefs']=sum(row['sum_coefs'])

我在输出中得到了一个未更改的Dataframe,在我的sum_coefs列中有一个集合,而不是总和。

1 个答案:

答案 0 :(得分:1)

可以尝试使用Series.apply

a. select cno, count(*) from took where score is not null group by cno

b. select cno, count(cno) from took group by cno having score is not null

c. select cno, count(*) from course except score null

d. select c.cno, q.cnt from course c, (select cno, count(*) cnt from took where score is not null group by cno) q where c.cno = q.cno

e. select c.cno, q.cnt from course c, (select cno, count(*) cnt from took group by cno) q where c.cno = q.cno and q.score is not null 

[出]

# Setup    
df_clusters = pd.DataFrame(['xxxx',set([1,2,3])], columns=['sum_coefs'])

def sum_sets(val):
    if isinstance(val, set):
        return sum(val)
    return val

df_clusters['sum_coefs'] = df_clusters['sum_coefs'].apply(sum_sets)

或者,或者使用内联0 xxxx 1 6 dtype: object 函数来获得相同的结果:

lambda