我對計算大熊貓分類列濃度的舊函數有問題。似乎已經發生了一些變化,無法對分類系列的方法的結果進行子集分組。計算大熊貓分類的「濃度」
最小非工作例如:
import pandas as pd
import numpy as np
df = pd.DataFrame({"A":["a","b","c","a"]})
def get_concentration(df,cat):
tmp = df[cat].astype("category")
counts = tmp.value_counts()
obs = len(tmp)
all_cons = []
for key in counts.keys():
single = np.square(np.divide(float(counts[key]),float(obs)))
all_cons.append(single)
return np.sum(all_cons)
get_concentration(df, "A")
這導致counts["a"]
一個關鍵的錯誤。我很確定這在熊貓的過去版本中起作用,並且文檔似乎沒有提及關於.value_counts()
方法的更改。
我補充說,也不需要''categorical'一個dtype'簡化和矢量版本。 – Stefan
在其他問題中,return語句應該在for循環之外。 – Alexander