[英]Select all rows in Python pandas
I have a function that aims at printing the sum
along a column of a pandas
DataFrame
after filtering on some rows to be defined;我有一个 function 旨在在过滤要定义的某些行后沿着pandas
DataFrame
的列打印sum
; and the percentage this quantity makes up in the same sum without any filter:以及这个数量在没有任何过滤器的情况下占相同总和的百分比:
def my_function(df, filter_to_apply, col):
my_sum = np.sum(df[filter_to_apply][col])
print(my_sum)
print(my_sum/np.sum(df[col]))
Now I am wondering if there is any way to have a filter_to_apply
that actually doesn't do any filter (ie keeps all rows), to keep using my function (that is actually a bit more complex and convenient) even when I don't want any filter.现在我想知道是否有任何方法可以让filter_to_apply
实际上不执行任何过滤器(即保留所有行),以继续使用我的 function(实际上有点复杂和方便),即使我不这样做想要任何过滤器。
So, some filter_f1
that would do: df[filter_f1] = df
and could be used with other filters: filter_f1 & filter_f2
.因此,一些filter_f1
可以: df[filter_f1] = df
并且可以与其他过滤器一起使用: filter_f1 & filter_f2
。
One possible answer is: df.index.isin(df.index)
but I am wondering if there is anything easier to understand (eg I tried to use just True
but it didn't work).一个可能的答案是: df.index.isin(df.index)
但我想知道是否有更容易理解的东西(例如,我试图只使用True
但它没有用)。
This is a way to select all rows:这是一种选择所有行的方法:
df[range(0, len(df))]
this is also这也是
df[:]
But I haven't figured out a way to pass :
as an argument.但我还没有想出一种方法来传递:
作为参数。
Theres a function called loc
on pandas that filters rows.在 Pandas 上有一个名为loc
的函数可以过滤行。 You could do something like this:你可以这样做:
df2 = df.loc[<Filter here>]
#Filter can be something like df['price']>500 or df['name'] == 'Brian'
#basically something that for each row returns a boolean
total = df2['ColumnToSum'].sum()
A Python slice object, ie slice(-1)
, acts as an object that selects all indexes in a indexable object. So df[slice(-1)]
would select all rows in the DataFrame
. Python 切片 object,即slice(-1)
充当 object,它选择可索引 object 中的所有索引。因此df[slice(-1)]
将 select 中的所有行DataFrame
. You can store that in a variable an an initial value which you can further refine in your logic:您可以将其存储在一个变量中,您可以在您的逻辑中进一步完善该初始值:
filter_to_apply = slice(-1) # initialize to select all rows
... # logic that may set `filter_to_apply` to something more restrictive
my_function(df, filter_to_apply, col)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.