两行之间 pandas 个数据系列的最小值或最大值
Min or Max of pandas data series between two rows
我有一个 pandas 数据框。假设列名是 'A'、'B' 和 'C'.
如何计算第 'A' 列中仅包含第 m 到 p 行的数据的最小 and/or 最大值?其中 m < p 和 m != 0 且 p < 数据帧中的行数。
如果我将列名存储在列表中并希望在循环中迭代它们并获得最大值怎么办?
colNames = ['A', 'B', 'C']
for col in colNames:
# get the max here
这个怎么样?
df.A[m:p].min()
通过使用 .iloc
和 loc
,假设您的 m=1 和 p=3
colNames=['A','B']
df.iloc[1:3,:].loc[:,colNames]
Out[767]:
A B
1 2 2
2 3 1
df.iloc[1:3,:].loc[:,colNames].min() #get the min of each column
Out[768]:
A 2
B 1
dtype: int64
df.iloc[1:3,:].loc[:,colNames].min(1) # get the min of each row
Out[769]:
1 2
2 1
dtype: int64
数据输入
df = pd.DataFrame({"A": [1,2,3],
'B':[3,2,1],'C':[2,1,3]})
我有一个 pandas 数据框。假设列名是 'A'、'B' 和 'C'.
如何计算第 'A' 列中仅包含第 m 到 p 行的数据的最小 and/or 最大值?其中 m < p 和 m != 0 且 p < 数据帧中的行数。
如果我将列名存储在列表中并希望在循环中迭代它们并获得最大值怎么办?
colNames = ['A', 'B', 'C']
for col in colNames:
# get the max here
这个怎么样?
df.A[m:p].min()
通过使用 .iloc
和 loc
,假设您的 m=1 和 p=3
colNames=['A','B']
df.iloc[1:3,:].loc[:,colNames]
Out[767]:
A B
1 2 2
2 3 1
df.iloc[1:3,:].loc[:,colNames].min() #get the min of each column
Out[768]:
A 2
B 1
dtype: int64
df.iloc[1:3,:].loc[:,colNames].min(1) # get the min of each row
Out[769]:
1 2
2 1
dtype: int64
数据输入
df = pd.DataFrame({"A": [1,2,3],
'B':[3,2,1],'C':[2,1,3]})