与新数据框无关

时间:2019-04-15 04:26:41

标签: python pandas dataframe jupyter-notebook unique

我有一些熊猫数据:

df1
df1['ID_A'].nunique()
5
df2
df2['ID_B'].nunique()
6
df3
df1['ID_A'].nunique()
2
df4
df2['ID_B'].nunique()
9

直到200 df

如何基于此dataframe制作新的nunique

我的预期结果如下:

  combine    ID_A  ID_B
  combine_1  5     6
  combine_2  2     9

谢谢

1 个答案:

答案 0 :(得分:3)

将列表理解与DataFrames列表一起使用,并在必要时通过带有f字符串的列表理解来更改索引名称:

df1 = pd.DataFrame({'ID_A':[1,2,3,4,5,5],
                    'ID_B':[1,2,3,4,5,6]})

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

dfs = [df1, df2]
df = pd.DataFrame([x.nunique() for x in dfs])
df.index = [f'combine_{x+1}' for x in df.index]
df.index.name= 'combine'
print (df)
           ID_A  ID_B
combine              
combine_1     5     6
combine_2     2     9

如有必要,仅按列表过滤列:

cols = ['ID_A', 'ID_B']
dfs = [df1, df2]
df = pd.DataFrame([x[cols].nunique() for x in dfs])
#filter only columns starting by ID_
#df = pd.DataFrame([x.filter(regex='^ID_').nunique() for x in dfs])
df.index = [f'combine_{x+1}' for x in df.index]
df.index.name= 'combine'