Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何从动态列数中查找最大值_Python_Pandas_Dataframe - Fatal编程技术网

Python 如何从动态列数中查找最大值

Python 如何从动态列数中查找最大值,python,pandas,dataframe,Python,Pandas,Dataframe,我有两个数据帧: A:20*15数字矩阵 B:20*1数字列表(从1-15) 我想找到表A中每一行的最大数量,但是只查看表B中的列 下面是一个简化的例子 谢谢 +-----------------+ | A: | +-----------------+ | 7 3 5 4 | | 8 1 2 5 | | 2 3 7 2 | | 4 1 3 6 | +-----------------+ |

我有两个数据帧: A:20*15数字矩阵 B:20*1数字列表(从1-15)

我想找到表A中每一行的最大数量,但是只查看表B中的列

下面是一个简化的例子

谢谢

+-----------------+
|       A:        |
+-----------------+
| 7  3  5  4      |
| 8  1  2  5      |
| 2  3  7  2      |
| 4  1  3  6      |


+-----------------+
|       B:        |
+-----------------+
| 2               |
| 4               |
| 1               |
| 2               |

其中
+
最大值
您希望在每行的第一列
n
中找到最大值,其中
n
来自第二个数据帧。因此,屏蔽不重要的单元格,然后默认情况下,将最大值作为
max
忽略
NaN

import numpy as np

m = np.arange(dfa.shape[1]) < dfb[0][:, None]  # Thanks rafaelc
dfa.where(m).max(1)

#0    7.0
#1    8.0
#2    2.0
#3    4.0
#dtype: float64

使用
pd.DataFrame.where
np.one

m = np.ones(dfa.shape).cumsum(1)
dfa.where(m <= dfb.to_numpy()).max(1)


pandas
解决方案

S=A.stack()
S[B.reindex(S.index.get_level_values(0)).values>=S.index.get_level_values(1)].max(level=0)
Out[276]: 
0    7
1    8
2    2
3    4
dtype: int64

一个简单的代码示例会很好,也可以表明您已经投入了一些精力来发现解决方案。至少对我来说,你们的模型示例对理解这个问题也没有多大帮助。我不清楚这个问题
m = np.ones(dfa.shape).cumsum(1)
dfa.where(m <= dfb.to_numpy()).max(1)
m = np.broadcast_to(np.arange(len(dfa)) + 1, dfa.shape)
0    7.0
1    8.0
2    2.0
3    4.0
dtype: float64
S=A.stack()
S[B.reindex(S.index.get_level_values(0)).values>=S.index.get_level_values(1)].max(level=0)
Out[276]: 
0    7
1    8
2    2
3    4
dtype: int64