簡體   English   中英

Pandas - '系列' object 沒有屬性

[英]Pandas - 'Series' object has no attribute

我需要使用 lambda function 進行逐行計算。 例如創建一些 dataframe

import pandas as pd
import numpy as np

def myfunc(x, y):
    return x + y

colNames = ['A', 'B']
data = np.array([np.arange(10)]*2).T

df = pd.DataFrame(data, index=range(0, 10), columns=colNames)

使用“myfunc”這確實有效

df['D'] = (df.apply(lambda x: myfunc(x.A, x.B), axis=1))

但是第二種情況不起作用!

df['D'] = (df.apply(lambda x: myfunc(x.colNames[0], x.colNames[1]), axis=1))

給出錯誤

AttributeError: ("'Series' object has no attribute 'colNames'", u'occurred at index 0')

我真的需要使用第二種情況(使用列表訪問 colNames),它會給出錯誤,關於如何做到這一點的任何線索?

當你使用df.apply() ,你的DataFrame的每一行都將作為pandas Series傳遞給你的lambda函數。 然后,框架的列將成為系列的索引,您可以使用series[label]訪問值。

所以這應該工作:

df['D'] = (df.apply(lambda x: myfunc(x[colNames[0]], x[colNames[1]]), axis=1)) 

通常,如果您嘗試訪問 object 上不存在的屬性,則會發生此錯誤。 對於 pandas 系列(或 DataFrames),它發生是因為您嘗試使用屬性訪問( . ) 對其進行索引。

在 OP 的情況下,他們使用x.colNames[0]訪問xcolNames[0]上的值,但df沒有屬性colNames ,因此發生了錯誤。 1

另一種可能發生此錯誤的情況是,如果索引中包含您不知道的空格。 例如,以下情況會重現此錯誤。

s = pd.Series([1, 2], index=[' a', 'b'])
s.a

在這種情況下,請確保刪除空格:

s.index = [x.strip() for x in s.index]
# or
s.index = [x.replace(' ', '') for x in s.index]

最后,使用[]來索引 Series(或 DataFrame)總是安全的。

1 : 系列具有以下屬性: axesdtypesemptyindexndimsizeshapeTvalues DataFrames 具有所有這些屬性 + columns 當您使用df.apply(..., axis=1)時,它會遍歷行,其中每行是一個 Series,其索引是df的列名。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM