简体   繁体   English

我如何从pandas数据框中的每一列获取最大(x)值,同时保持每个索引的索引?

[英]How can I get the max (x) number of values from each column in a pandas dataframe while keeping the index for each?

I'm attempting to get the top x largest values from each column in a pandas dataframe. 我正在尝试从pandas数据框中的每一列中获取最高的x最大值。 Each column is one date while each row is a different stock ticker(see photo) 每列是一个日期,而每一行是不同的股票行情自动收录器(见图)

ideally i'd like to see the ticker and number of the top 5 for each date(column) 理想情况下,我想查看每个日期的前5名的行情自动收录器和代码(列)

I have tried a few different iterators but none have worked and kept the index. 我尝试了一些不同的迭代器,但是没有一个起作用并且保留了索引。

The output I want is into a new csv with the date and top 5 stock tickers (index) based on their value in the column that day. 我想要的输出将根据日期和当天行中的值输入到带有日期和前5个股票行情自动收录器(索引)的新csv中。

import pandas as pd 将熊猫作为pd导入

df = pd.read_csv (see photo) df = pd.read_csv(见图)

Haven't been able to get it to turn out right. 尚未能够正确解决。 enter image description here 在此处输入图片说明

Apply pd.Series.nlargest to each column to mask everything but the top N values. pd.Series.nlargest应用于每列以屏蔽除前N个值之外的所有内容。 Then unstack and remove NaN . 然后拆下unstack并移除NaN I'll use the top 2 values here for illustration. 我将在此处使用前两个值进行说明。

Sample Data 样本数据

import pandas as pd
import numpy as np

np.random.seed(42)
df = pd.DataFrame(np.random.normal(0, 10, (4, 3)), 
                  columns=['Date1', 'Date2', 'Date3'], 
                  index=['Stock1', 'Stock2', 'Stock3', 'Stock4'])
#            Date1     Date2     Date3
#Stock1   4.967142 -1.382643  6.476885
#Stock2  15.230299 -2.341534 -2.341370
#Stock3  15.792128  7.674347 -4.694744
#Stock4   5.425600 -4.634177 -4.657298

Code

df.apply(pd.Series.nlargest, n=2).unstack().dropna()

#Date1  Stock2    15.230299
#       Stock3    15.792128
#Date2  Stock1    -1.382643
#       Stock3     7.674347
#Date3  Stock1     6.476885
#       Stock2    -2.341370
#dtype: float64

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 Pandas 数据框中的每一行获取前三个最大值? - How can I get the first three max values from each row in a Pandas dataframe? 如何计算 pandas dataframe 中每个数字的 3 个值的总和,包括第一个数字? - How can I calculate the sum of 3 values from each number in a pandas dataframe including the first number? 如何在整个熊猫而不是每一行中使用整个数据框的groupby获取最大值 - How to get max values with groupby of entire dataframe in Pandas, not each row Pandas Dataframe:按索引分组,每列仅保留 notnan 值 - Pandas Dataframe: grouping by index keeping only notnan value in each column 从pandas.DataFrame的每列中获取最大值 - Get top biggest values from each column of the pandas.DataFrame 如何从 Pandas 数据框中按 ID 获取每年最大日期的行? - How can I grab rows with max date for each year by ID from Pandas dataframe? Python pandas dataframe:为另一列的每个唯一值查找最大值 - Python pandas dataframe: find max for each unique values of an another column 如何获取 dataframe 中每个组的每列的最大值? - How to get the max value of each column for each group in a dataframe? Python:获取 pandas dataframe 每一行的最大值列 - Python: Get Columns of max values each row of an pandas dataframe 如何将每列与Pandas DataFrame的其他列相乘? - How can I multiply each column with the other columns of the Pandas DataFrame?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM