【发布时间】:2019-05-23 12:11:04
【问题描述】:
我正在这里练习:https://www.machinelearningplus.com/python/101-pandas-exercises-python/
问题 #16 有一个使用 np.where() 的解决方案 (#1),但我无法理解。
import pandas as pd
import numpy as np
print('pandas: {}'.format(pd.__version__))
print('NumPy: {}'.format(np.__version__))
print('-----')
ser1 = pd.Series([10, 9, 6, 5, 3, 1, 12, 8, 13])
ser2 = pd.Series([1, 3, 10, 13])
# Get the positions of items of 'ser2' in 'ser1' as a list.
# Solution 1
list1 = [np.where(i == ser1)[0].tolist()[0] for i in ser2]
print(list1)
print()
# Solution 2
list2 = [pd.Index(ser1).get_loc(i) for i in ser2]
print(list2)
我在这里查找了 np.where():
# https://stackoverflow.com/questions/34667282/numpy-where-detailed-step-by-step-explanation-examples
# https://thispointer.com/numpy-where-tutorial-examples-python/
# https://www.geeksforgeeks.org/numpy-where-in-python/
确切地说,我不了解两者的功能和位置 括号内的零 ( [0] )。
【问题讨论】:
-
明白。你有什么问题?
-
我的最后一句话。我不明白括号中的零的功能和位置。我不明白他们在做什么,他们在函数中代表什么。
-
这不是问题。那是一个声明。你有什么不明白的?可回答问题的一些示例:“[0] 对列表有什么作用?”、“为什么我必须取 np.where 的第 0 个元素?”、“np.where 有什么作用?”、“tolist 有什么作用? () 做什么?”等
标签: python python-3.x numpy