[英]subtracting mean of each column away from the column and returning it
我有一個包含許多列的數據集。 我必須創建一個 function 獲取每列的平均值並從列中的每一行中減去它,然后返回減去這些平均值的數據集。 我在這里發現了一個類似的問題並應用了答案,但我不斷收到錯誤消息。 這是我的代碼:
def exercise1(df):
df1 = DataFrame(df)
df2 = df1 - df1.mean()
return df2
exercise1(data)
# Where data is the a csv file regarding salaries in the San Francisco area.
我收到以下錯誤
TypeError: ufunc 'subtract' did not contain a loop with signature matching types dtype('<U32') dtype('<U32') dtype('<U32')
我無法弄清楚我做錯了什么
您可以使用try-except
在列上執行 for 循環:
def exercise1(df):
df1 = df.copy()
for col in df1.columns:
try: # if we can compute the mean then substract
df1[col] -= df1[col].mean()
except: # otherwise just ignore the column
pass
return df1
df.mean() 生成 pandas 系列數據類型,其中僅包含原始 DataFrame 中的數字列。
means = df.mean()
您可以使用以下方法獲取該系列的索引值:
means.index
用它來切片你原來的 DataFrame 並減去平均值
df2 = df[means.index] - means
您需要指定要從中減去的列:
df = {'values1': [1,2,3], 'values2': [4,5,6]}
def exercise1(df):
df1 = pd.DataFrame(df)
df2 = df1['values2'] - df1['values2'].mean()
return df2
print(exercise1(df))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.