Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 获取列的索引,该列在everyrow中具有最大值,不带(…,)值_Python_Pandas_Dataframe_Tuples - Fatal编程技术网

Python 获取列的索引,该列在everyrow中具有最大值,不带(…,)值

Python 获取列的索引,该列在everyrow中具有最大值,不带(…,)值,python,pandas,dataframe,tuples,Python,Pandas,Dataframe,Tuples,我在我的Jupyter上做了这个。 我想知道是否有办法找到表中每个表中最大值的位置(列索引)。 例如,它看起来是这样的: yo1 = [1,3,7] yo2 = [2,4,5,6,8] yo3 = [0.1,0.3,0.7] yo4 = [0.2,0.4,0.5,0.6,0.8] yoo = [] for x in yo3: vvv = [] for y in yo4: dot = x*y na = x+x nb = y+y

我在我的Jupyter上做了这个。
我想知道是否有办法找到表中每个表中最大值的位置(列索引)
例如,它看起来是这样的:

yo1 = [1,3,7]
yo2 = [2,4,5,6,8]
yo3 = [0.1,0.3,0.7]
yo4 = [0.2,0.4,0.5,0.6,0.8]

yoo = []
for x in yo3:
    vvv = []
    for y in yo4:
        dot = x*y
        na = x+x
        nb = y+y
        prod = dot/(na+nb)
        vvv.append(prod)
    yoo.append(vvv)
yooo = pd.DataFrame(yoo, columns=(yo2), index=[yo1])
print(yooo)
(是的,它是余弦相似性)

然后,我想得到everyrow中最大值列的索引。我用了这个:

go = yooo.idxmax().reset_index()
go.columns=['column', 'get']
go

output:
    column  get
0   2       (7,)
1   4       (7,)
2   5       (7,)
3   6       (7,)
4   8       (7,)
但是我的期望输出是:

output:
    column  get
0   2       7
1   4       7
2   5       7
3   6       7
4   8       7
我已尝试将“(”替换为“”

go['get']=go['get'].str.replace('(','')
并且使用了lstrip rstrip

go['get']=go['get'].map(lambda x: x.lstrip('(').rstrip(',)'))
还有这个

top_n=1
get = pd.DataFrame({n: yooo[col].nlargest(top_n).index.tolist() for n, col in enumerate(yooo)}).T
它们都不太管用:(


帮帮我..如何解决这个问题,你能给我解释一下吗???谢谢!!!

你真正的问题在于“yooo”的数据帧构造函数,你正在用[]包装一个列表,创建一个二维列表,从而创建一个pd.MultiIndex,从而得到元组(7),。请改用这个:

 yooo = pd.DataFrame(yoo, columns=(yo2), index=yo1)

 yooo.idxmax()
输出:

2    7
4    7
5    7
6    7
8    7
dtype: int64
   column  get
0       2    7
1       4    7
2       5    7
3       6    7
4       8    7
并进一步获取具有列名的数据帧:

yooo.idxmax().rename_axis('column').rename('get').reset_index()
输出:

2    7
4    7
5    7
6    7
8    7
dtype: int64
   column  get
0       2    7
1       4    7
2       5    7
3       6    7
4       8    7

是的。非常感谢。你救了我的命:D,这正是我所要找的。“如果我的回答有帮助,请考虑回答,以结束这个问题。谢谢。”