我是python新手。我想找到col2相对于列表col1中的“man”、“women”和“people”的最大值。例如,['men', 12, '1946-Truman.txt'], ['women', 7, '1946-Truman.txt']
和['people', 49, '1946-Truman.txt']
包含男性、女性和人群的col2的最大值。在
一种可能的解决方案是将这个元组列表转换为三个单独的数组,分别用于男性、女性和人群,然后从所有数组中找到最大值。但是,我想要一个更好的解决方案。在
数据:
[['men', 2, '1945-Truman.txt']
['women', 2, '1945-Truman.txt']
['people', 10, '1945-Truman.txt']
['men', 12, '1946-Truman.txt']
['women', 7, '1946-Truman.txt']
['people', 49, '1946-Truman.txt']
['men', 7, '1947-Truman.txt']
['women', 2, '1947-Truman.txt']
['people', 12, '1947-Truman.txt']
['men', 4, '1948-Truman.txt']
['women', 1, '1948-Truman.txt']
['people', 22, '1948-Truman.txt']
['men', 2, '1949-Truman.txt']
['women', 1, '1949-Truman.txt']
['people', 15, '1949-Truman.txt']
['men', 6, '1950-Truman.txt']
['women', 2, '1950-Truman.txt']
['people', 15, '1950-Truman.txt']
['men', 8, '1951-Truman.txt']
['women', 2, '1951-Truman.txt']
['people', 9, '1951-Truman.txt']
['men', 3, '1953-Eisenhower.txt']
['women', 0, '1953-Eisenhower.txt']
['people', 17, '1953-Eisenhower.txt']]
提前谢谢。在
您可以使用
pandas
包。 通过定义数据帧:那么
^{pr2}$返回
这就是你想要的吗?在
如果您使用的列表包括:
然后可以使用以下代码。在
^{pr2}$这将进入名为
lst
的位列表中的每个列表,并找到男性、女性和人员的最大值。在pandas
是一个很好的选择,但是您可以使用max
和{相关问题 更多 >
编程相关推荐