取最小值的数据帧的名称

我有3个数据框如下。


df1 = pd.DataFrame( {"val" : [1, 11, 111, 1111, 11111, 11111],

                    "val2" : [2, 22, 23, 24, 25, 26],"val3" : [33333, 33333, 3333, 333, 33, 3]} )

val     val2    val3

1          2    33333

11        22    33333

111       23    3333

1111      24    333

11111     25    33

11111     26    3

df2 = pd.DataFrame( {"val" : [2, 22, 23, 24, 25, 26],"val2" : [1, 11, 111, 1111, 11111, 11111],"val3" : [3, 3, 3, 3, 3, 3] } )

val     val2    val3

2         1     3

22       11     3

23      111     3

24     1111     3

25    11111     3

26    11111     3

&


df3 = pd.DataFrame( {"val" : [33333, 33333, 3333, 333, 33, 3],"val2" : [3, 3, 3, 3333, 3, 3],"val3" : [2, 22, 23, 24, 25, 26] } )

val     val2    val3

33333      3    2

33333      3    22

3333       3    23

333     3333    24

33         3    25

3          3    26

使用代码pd.concat([df1,df2,df3]).min(level=0),我得到一个日期帧,其中包含这 3 个数据帧的每个单元格的最小值


val     val2    val3

1         1     2

11        3     3

23        3     3

24       24     3

25        3     3

3         3     3

现在,我的问题是有没有办法获得一个数据框,该数据框显示这些单独的值来自哪个数据框?预期输出如下


val     val2    val3

df1     df2     df3

df1     df3     df2

df2     df3     df2

df2     df1     df2

df2     df3     df2

df3     df3     df1,df2

这在 Pandas 中是否可行?


茅侃侃
浏览 94回答 3
3回答

米脂

使用numpy.ma.apple_along_axis:def min_finder(arr1d,sep=","):&nbsp; &nbsp; return ",".join(["df%s" % i for i in np.argwhere(arr1d == arr1d.min()).ravel()+1])new_df = pd.DataFrame(np.ma.apply_along_axis(min_finder, 2, np.stack(dfs, 0)).T)print(new_df)&nbsp; &nbsp; &nbsp;0&nbsp; &nbsp; 1&nbsp; &nbsp; &nbsp; &nbsp; 20&nbsp; df1&nbsp; df2&nbsp; &nbsp; &nbsp; df31&nbsp; df1&nbsp; df3&nbsp; &nbsp; &nbsp; df22&nbsp; df2&nbsp; df3&nbsp; &nbsp; &nbsp; df23&nbsp; df2&nbsp; df3&nbsp; &nbsp; &nbsp; df34&nbsp; df2&nbsp; df3&nbsp; &nbsp; &nbsp; df25&nbsp; df3&nbsp; df3&nbsp; df1,df2如果您不想打领带,请使用numpy.argmin:dfs = [df1,df2,df3]np.stack(dfs, 0).argmin(0)输出:array([[0, 1, 2],&nbsp; &nbsp; &nbsp; &nbsp;[0, 2, 1],&nbsp; &nbsp; &nbsp; &nbsp;[1, 2, 1],&nbsp; &nbsp; &nbsp; &nbsp;[1, 0, 1],&nbsp; &nbsp; &nbsp; &nbsp;[1, 2, 1],&nbsp; &nbsp; &nbsp; &nbsp;[2, 2, 0]])或者让它更漂亮一点:np.char.add("df", (np.stack(dfs, 0).argmin(0)+1).astype(str))array([['df1', 'df2', 'df3'],&nbsp; &nbsp; &nbsp; &nbsp;['df1', 'df3', 'df2'],&nbsp; &nbsp; &nbsp; &nbsp;['df2', 'df3', 'df2'],&nbsp; &nbsp; &nbsp; &nbsp;['df2', 'df1', 'df2'],&nbsp; &nbsp; &nbsp; &nbsp;['df2', 'df3', 'df2'],&nbsp; &nbsp; &nbsp; &nbsp;['df3', 'df3', 'df1']], dtype='<U23')

慕斯王

这是一种基本方法,将值映射到其数据帧的名称,找到分钟,然后丢弃值:(pd.concat([df1.applymap(lambda a: (a, 'df1')),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; df2.applymap(lambda a: (a, 'df2')),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; df3.applymap(lambda a: (a, 'df3'))])&nbsp; &nbsp;.min(level=0)&nbsp; &nbsp;.applymap(lambda a: a[1]))&nbsp; &nbsp;val val2 val30&nbsp; df1&nbsp; df2&nbsp; df31&nbsp; df1&nbsp; df3&nbsp; df22&nbsp; df2&nbsp; df3&nbsp; df23&nbsp; df2&nbsp; df1&nbsp; df24&nbsp; df2&nbsp; df3&nbsp; df25&nbsp; df3&nbsp; df3&nbsp; df1请注意,将在值相等的地方返回按名称的第一个数据帧。

千巷猫影

这是使用字典和df.isin后跟数据透视表的一种方法,可能有点冗长,因为我在测试时可能会摇摆不定,但确实可以dict_of_dfs = {'df1':df1,'df2':df2,'df3':df3}concated = pd.concat(dict_of_dfs.values())a = np.vstack([np.where(v.isin(concated.min(level=0)),k,np.nan)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for k,v in dict_of_dfs.items()])b = pd.DataFrame(a,columns=concated.columns,index=concated.index).replace('nan',np.nan)print(b.head(10))&nbsp; &nbsp; val val2 val30&nbsp; df1&nbsp; NaN&nbsp; NaN1&nbsp; df1&nbsp; NaN&nbsp; NaN2&nbsp; NaN&nbsp; NaN&nbsp; NaN3&nbsp; NaN&nbsp; df1&nbsp; NaN4&nbsp; NaN&nbsp; NaN&nbsp; NaN5&nbsp; NaN&nbsp; NaN&nbsp; df10&nbsp; NaN&nbsp; df2&nbsp; NaN1&nbsp; NaN&nbsp; NaN&nbsp; df22&nbsp; df2&nbsp; NaN&nbsp; df23&nbsp; df2&nbsp; NaN&nbsp; df2然后使用pivot_table(当然您可以进行自定义更改以删除额外的级别并重命名索引)final = b.stack().reset_index()final.pivot_table(index='level_0',columns='level_1',values=0,aggfunc=','.join)level_1&nbsp; val val2&nbsp; &nbsp; &nbsp;val3level_0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0&nbsp; &nbsp; &nbsp; &nbsp; df1&nbsp; df2&nbsp; &nbsp; &nbsp; df31&nbsp; &nbsp; &nbsp; &nbsp; df1&nbsp; df3&nbsp; &nbsp; &nbsp; df22&nbsp; &nbsp; &nbsp; &nbsp; df2&nbsp; df3&nbsp; &nbsp; &nbsp; df23&nbsp; &nbsp; &nbsp; &nbsp; df2&nbsp; df1&nbsp; &nbsp; &nbsp; df24&nbsp; &nbsp; &nbsp; &nbsp; df2&nbsp; df3&nbsp; &nbsp; &nbsp; df25&nbsp; &nbsp; &nbsp; &nbsp; df3&nbsp; df3&nbsp; df1,df2
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python