[英]Getting the max value from a list of columns by their index in Pandas
I have a dataframe with a variety of columns, but the key part of data I am looking to extract is in columns which are named using datetime values which hold a floating point number for currency.我有一个带有各种列的 dataframe,但我要提取的数据的关键部分是在使用日期时间值命名的列中,这些值包含货币的浮点数。
I am basically just looking to find the max value of any column that is of a date value (ie 2021-01-15 00:00:00) per row.我基本上只是想找到每行具有日期值(即 2021-01-15 00:00:00)的任何列的最大值。 I originally used list() to try find any column with '-' in but guessing due to the format I can't directly reference the datetime values?我最初使用 list() 来尝试查找任何带有“-”的列,但由于格式而猜测我无法直接引用日期时间值?
Example df:示例 df:
index, ID, Cost, 2021-01-01 00:00:00, 2021-01-08 00:00:00, 2021-01-15 00:00:00
0, 1, 4000, 40.50, 50.55, 60.99
0, 1, 500, 20.50, 80.55, 160.99
0, 1, 4000, 40.50, 530.55, 1660.99
0, 1, 5000, 40.50, 90.55, 18860.99
0, 1, 9000, 40.50, 590.55, 73760.99
You can find the 'date' columns using a list comprehension which will return the columns that contain /
.您可以使用列表推导找到“日期”列,该推导将返回包含/
的列。 Then you can use max(axis=1)
to create the column which will show the highest value per row, of your date like columns:然后,您可以使用max(axis=1)
创建列,该列将显示每行的最高值,例如列:
date_cols = [c for c in list(df) if '/' in c]
df['max_per_row'] = df[date_cols].max(axis=1)
prints:印刷:
index ID Cost ... 08/01/2021 00:00 15/01/2021 00:00 max_per_row
0 0 1 4000 ... 50.55 60.99 60.99
1 0 1 500 ... 80.55 160.99 160.99
2 0 1 4000 ... 530.55 1660.99 1660.99
3 0 1 5000 ... 90.55 18860.99 18860.99
4 0 1 9000 ... 590.55 73760.99 73760.99
Use DataFrame.iloc
for select all columns without first 2:将DataFrame.iloc
用于 select 的所有列,没有第一个 2:
df['new'] = df.iloc[:, 2:].max(axis=1)
If need select float columns use DataFrame.select_dtypes
:如果需要 select 浮点列使用DataFrame.select_dtypes
:
df['new'] = df.select_dtypes('float').max(axis=1)
For columns with -
use DataFrame.filter
:对于使用DataFrame.filter
的列-
df['new'] = df.filter(like='-').max(axis=1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.