[英]Applying function to columns in DataFrame
I have a DataFrame with columns ['A', 'B', 'C'].我有一个带有 ['A'、'B'、'C'] 列的 DataFrame。 I am trying to normalize each of the column using my function.
我正在尝试使用我的 function 对每一列进行标准化。 The problem is that it works when I do
normalization(df['A'])
, but doesn't work when I pass a list to the function:问题是它在我进行
normalization(df['A'])
时有效,但是当我将列表传递给 function 时它不起作用:
def normalization(x):
x = (x - np.min(x)) / (np.max(x) - np.min(x))
for column in df.columns:
normalization(df[column])
How to deal with it in this case?在这种情况下如何处理? I did read answers with
.map
and .apply
but that didn't work in my case for some reason.我确实阅读了
.map
和.apply
的答案,但由于某种原因,这在我的情况下不起作用。 I am new to Python, hope my question makes sense.我是 Python 的新手,希望我的问题有意义。
The problem is your normalization function.问题是您的标准化 function。 it should return the value of the normalization:
它应该返回标准化的值:
def normalization(x):
return (x - np.min(x)) / (np.max(x) - np.min(x))
When you don't return the value the value None is returned causing the values in map\apply to be None.当您不返回值时,将返回值 None 导致 map\apply 中的值为 None。
Example of working code:工作代码示例:
import pandas as pd
import numpy as np
def normalization(x):
return (x - np.min(x)) / (np.max(x) - np.min(x))
data = {'A': [1, 2, 3], 'B': [3, 4, 5], 'C': [4,5,6]}
df = pd.DataFrame(data=data)
df = df.apply(normalization)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.