Py学习  »  MoRe  »  全部回复
回复总数  2
3 年前
回复了 MoRe 创建的主题 » Python使用索引列计算精确匹配的列

如果我理解你的意思,对吗

你的数据框是这样的:

df = pd.DataFrame(data = [
    ["a", 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0],
    ["b", 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0],
    ["c", 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0],
    ["d", 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0],
    ["e", 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1],
    ["f", 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0],
])
df = df.rename(columns = {0:"name"}).set_index("name")

然后:

def exact_match(lst):
    s = df[df.columns[df.loc[lst].sum(axis = 0) == len(lst)]].sum(axis = 0) == len(lst)
    return len(s[s])
exact_match(["c","d"]) # output: 2
3 年前
回复了 MoRe 创建的主题 » Python试图创建一个图形,但它是空的
import matplotlib.pyplot as plt
plt.plot(df['cyl'], df['mpg'])
plt.show()

或者:

ax = plt.subplot(2, 1, 1)
ax.plot(df['cyl'], df['mpg'])
plt.show()