简体   繁体   English

如何迭代 DataFrame 列并更新列值

[英]How to Iterate over DataFrame Column and Update Column Values

I'm trying to iterate though the abc column and update its' value, but I keep getting errors.我正在尝试遍历 abc 列并更新其值,但我不断收到错误消息。 Any help would be appreciated.任何帮助,将不胜感激。

somedata.csv:
                    ABC
0               0.00094
1               0.00094
2               0.00094
3               0.00094
...
4999            0.00094


data = pd.read_csv("somedata.csv")
abc = pd.DataFrame(data, columns = ['ABC'])

for column in abc:
    value = (abc - min(abc)) / (max(abc) - min(abc))
    abc = value

Error: numpy.core._exceptions.UFuncTypeError: ufunc 'subtract' did not contain a loop with signature matching types (dtype(' dtype('错误:numpy.core._exceptions.UFuncTypeError:ufunc 'subtract' 不包含具有签名匹配类型的循环 (dtype(' dtype('

line 72, in第 72 行,在
value = (abc - min(abc)) / (max(abc) - min(abc))值 = (abc - min(abc)) / (max(abc) - min(abc))

So there's a few things wrong.所以有几件事是错误的。 On "abc" instantiation, finish the ")".在“abc”实例化时,完成“)”。 The line value = (abc - min(abc)) / (max(abc) - min(abc)) is very confusing.value = (abc - min(abc)) / (max(abc) - min(abc))非常混乱。 abc you have as a dataframe column, so I assume you would like to update every row value of this column titled 'ABC'. abc 你有一个数据框列,所以我假设你想更新这个标题为“ABC”的列的每一行值。

To get value = (ThisRowValue - min(abc)) / (max(abc) - min(abc)), utilize Panda's max and min要获得值 = (ThisRowValue - min(abc)) / (max(abc) - min(abc)),请利用Panda 的最大值和最小值

I would write this我会写这个

#Separate out the column (assuming multiple columns)
abc = df['ABC']
abc = list(map(float, abc)) #Edited to turn it into float values

list2 = [] # Empty list for storage
for this_row_value in abc: #Iterate for each value in column 'ABC'
    value = (this_row_value - abc.min()) / (abc.max() - abc.min()) #Utilize Panda's Min/Max
    list2.append(value)

#Set updated values to the column values. You can also use Pandas Update.
df['ABC'] = list2

Where df is just the dataframe object.其中df只是数据框对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM