【发布时间】:2016-09-18 22:46:51
【问题描述】:
假设我有一个 pandas 数据框,其列是 ['a','b','c']。它们都是整数类型。我想在“c”列中的每个唯一值对应的“b”列中找到唯一值的数量,并将其存储在一个新的数据框中。我如何使用熊猫来做到这一点?我试过这样的事情:
new_df = pd.DataFrame()
for value in df['c'].unique():
x1 = df[df['c']==value]
x2 = x1['b'].unique().size
new_df.append({'A': x2, 'B': value}, ignore_index=True)
但这需要太多时间。有没有有效的方法?
【问题讨论】:
标签: python pandas filter dataframe multiple-columns