Python 3.x 两行之间数据系列的最小值或最大值
我有一个熊猫数据框。假设列名为“A”、“B”和“C” 如何计算列“A”中数据的最小值和/或最大值(仅包括行m到p)?其中mPython 3.x 两行之间数据系列的最小值或最大值,python-3.x,pandas,max,min,object-slicing,Python 3.x,Pandas,Max,Min,Object Slicing,我有一个熊猫数据框。假设列名为“A”、“B”和“C” 如何计算列“A”中数据的最小值和/或最大值(仅包括行m到p)?其中m
如果我将列名存储在一个列表中,并希望在循环中对它们进行迭代并获得最大值,该怎么办
colNames = ['A', 'B', 'C']
for col in colNames:
# get the max here
这个怎么样
df.A[m:p].min()
这个怎么样
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]})
通过使用
.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]})
谢谢你的帮助!谢谢你的帮助!