简体   繁体   English

如何提取列的最大值并将列名返回以将其保存为变体

[英]How to exctract MAX value of a Colummn and give Column name back to save it as a variant

I am a very beginner enthusiastic programmer wanna be.我是一个非常初学者的热心程序员。 I am trying to compare two Columns of a csv which I got from Google trends and extract the "Winner word" into a variant or list.我正在尝试比较我从 Google trends 获得的 csv 的两个 Columns,并将“Winner word”提取到变体或列表中。 So I could compare to other keywords in Trends.所以我可以与趋势中的其他关键字进行比较。 So far, I managed:到目前为止,我设法:

The csv always looks like this: csv 总是这样:

date,VR,metaverse
2004-01-01,17,0
2004-02-01,17,0
2004-03-01,18,0
2004-04-01,16,0
2004-05-01,17,0
2004-06-01,17,0

in:在:

csv1 = pd.read_csv ("search_trends.csv").drop("date", axis=1)
csv1 = pd.DataFrame(csv1)
result1 = csv1.max(axis=1)
result1.index = csv1.idxmax(axis=1)

out:出去:

VR    17
VR    17
VR    18
VR    16
VR    17
      ..
VR    45
VR    46
VR    45
VR    44
VR    40

Length: 226, dtype: int64
['VR', 'metaverse']

The trick here is, that I might not know the names of the column, therefore it needs to be universal somehow.这里的技巧是,我可能不知道列的名称,因此它需要以某种方式通用。

you can also access a column by index and retrieve its name, but you have to know the index:您还可以通过索引访问列并检索其名称,但您必须知道索引:

import pandas as pd

foo = pd.DataFrame()
foo['a'] = [1, 2, 3]

col_index = 0

max_of_column = max(foo.iloc[:, col_index])
name_of_column = foo.columns[col_index]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM