此 dtype 不允许执行缩减操作"Argmax"



如何获取B列最后一个单元格的索引

我将通过在其中取值来遍历所有行。对于那个迭代,我想在python中使用panda的特定列中获取max行的索引值。df['columnNameB'].idxmax()

我还尝试过使用axis=1axis='columns会出现其他奇怪的错误。

idxmax()在熊猫系列中工作,而df['colNameB']已经给出了一系列B列值,那么为什么idxmax()不能正常工作呢?

代码如下;

#reading from csv file url-s
def readCSV(path_csv):
df=pd.read_csv(path_csv)
return df
fileCSV=readCSV(r'C:UsersAdminDownloadsurls.csv')
print(fileCSV['columnURLnames'].idxmax())
"""for i in range(len(fileCSV.columns(1))):
xUrl=fileCSV.iloc[0,i]
print(xUrl)
"""

编辑:

columnA    columnB                   columnC
0   book      www.amazon.com=page?1..   onstock
1   headphone www.amazon.com=page?1..   outstock
2   NaN       www.alibaba.com..         NaN
3   NaN       www.amazon....            NaN
4   lightbulb    NaN                    inStock

我需要3号。因为B列中最大值的索引是3

已解决:来自SO

length_of_column_urls=fileCSV['columnURLnames'].last_valid_index()
for i in range(0, length_of_column_urls+1 ):
xUrl=fileCSV.iloc[i,1]
print(xUrl)
idxmax返回最高值的索引。一系列url名称(字符串(没有这样的值。

编辑:我认为你正在寻找类似的东西,而不是idxmax

len(fileCSV.index)

最新更新