PythonはTypeErrorを解決します:( '' Series 'オブジェクトは呼び出し可能ではありません'、 'インデックス13012で発生しました')



Python Resolves Typeerror



まず、ソースデータの最初の5行は次のようになります。

DealTime bf_StudentID AccName PerSex MonDeal avgMonDeal transaction_times
0 2018-07-01 13983 男性 -3.7 -3.70 1 2018-07-01
1 2018-07-01 14018 男性 -9.5 -9.50 1 2018-07-01
2018-07-01 14073 劉ムーモウ 男性 -8.0 -8.00 1 2018-07-01
3 2018-07-01 14074 周ムーモウ 男性 -14.3 -7.15 2018-07-01
4 2018-07-01 14097 真央ムーモウ 男性 -10.0 -10.00 1 2018-07-01
# Follow the previous chapter to perform data pivoting on user consumption data. pivoted_counts=df.pivot_table(index='bf_StudentID',columns='month',values='transaction_times',aggfunc='sum').fillna(0) columns_month=df.month.sort_values().astype('str').unique() pivoted_counts.columns=columns_month pivoted_counts.head()
2018-07-01 2018-08-01 2018-09-01 2018-10-01 2018-11-01 2018-12-01 2019-01-01
bf_StudentID
13012 0.0 0.0 10.0 10.0 15.0 7.0 9.0
13564 17.0 25.0 82.0 63.0 69.0 75.0 50.0
13599 8.0 10.0 39.0 33.0 34.0 31.0 26.0
13685 8.0 12.0 28.0 33.0 34.0 39.0 17.0
13947 9.0 22.0 81.0 64.0 66.0 72.0 63.0
# Convert data with applymap+lambda, as long as the consumption is more than 30 times in the month, it is recorded as 1, and vice versa. pivoted_purchase = pivoted_counts.applymap(lambda x: 1 if x > 30 else 0) pivoted_purchase.head()
2018-07-01 2018-08-01 2018-09-01 2018-10-01 2018-11-01 2018-12-01 2019-01-01
bf_StudentID
13012 0 0 0 0 0 0 0
13564 0 0 1 1 1 1 1
13599 0 0 1 1 1 1 0
13685 0 0 0 1 1 1 0
13947 0 0 1 1 1 1 1

次に、最初に関数を定義してから、その関数をデータフレームに適用します。



# This student consumption case: # Next, user stratification, we simply divide into several dimensions according to the student's consumption behavior: new students (new users), students who love the canteen (active users), students who do not like the canteen (inactive users) Occasionally, students who love the dining hall (returning users). # is the first time spending more than 30 times. The students who love the dining hall are frequent customers/old customers, who have spent more than 30 times in a certain time window. Students who do not like the dining hall are old customers who have not spent more than 30 times in the time window. The reflow user did not consume more than 30 times in the previous window, but had spent more than 30 times in the current time window. The above time windows are all counted by month. # For example, if a student spends more than 30 times in the first time in September, then his stratification in September is a new student (new user) # He spends more than 30 times in October, which is a student who loves the dining hall (active users) # November has not consumed more than 30 times, this time is a student who does not love the dining hall (inactive users) # 12 months to spend more than 30 times, this time is the student who loves the dining hall occasionally (returning users) # January is still more than 30 times, it is a student who loves the dining hall (active users). # involves more complex logical judgments.

関数の定義

def active_status(data): status=[] for i in range(7): # If there is no “consumption of more than 30 times” this month if data[i] == 0: if len(status) > 0: if status[i-1] == 'unreg': status.append('unreg') else: status.append('unlike_canteen') else: status.append('unreg') #If you spend more than 30 times this month else: if len(status) == 0: status.append('new') else: if status[i-1] == 'unlike_canteen': status.append('occasionally_like_canteen') elif status[i-1] == 'unreg': status.append('new') else: status.append('love_canteen') return status # Function is more complicated to write, mainly divided into two parts of the judgment, whether this month is 'consumed more than 30 times' as the boundary. # There is no “consumption of more than 30 times” this month, and it is necessary to judge whether he is a new student (new customer). #Because some students only “spent more than 30 times” in September to become a new student (new guest), then in July and August he should not even be a new student (new guest), using unreg. # If it is an old customer (that is, spending more than 30 times in the previous month), it is like_canteen. # If there is “monthly consumption of more than 30 times” this month, it is necessary to judge whether it is the first time “more than 30 times per month”, and there is no “monthly consumption of more than 30 times” in the previous time window. # You can debug several times to smooth out the logical relationship inside, layer the user, the logic is not simple, and here is only a simplified version.

次に、apply関数を使用して、初めてエラーを実行します。次のように。
(「シリーズ」オブジェクトは呼び出し可能ではありません」、「インデックス13012で発生しました」)



# pivoted_purchase_status = pivoted_purchase.apply(lambda x: active_status(x),axis = 1) # pivoted_purchase_status.head() #Run error: (''Series' object is not callable', 'occurred at index 13012')

エラー訂正プロセス。次のように。

# 'index 13012' is what is sacred. pivoted_purchase.loc[13012] 2018-07-01 0 2018-08-01 0 2018-09-01 0 2018-10-01 0 2018-11-01 0 2018-12-01 0 2019-01-01 0 Name: 13012, dtype: int64 # See if there is any difference between other rows and index 13012 pivoted_purchase.loc[13564] # No difference. . . 2018-07-01 0 2018-08-01 0 2018-09-01 1 2018-10-01 1 2018-11-01 1 2018-12-01 1 2019-01-01 1 Name: 13564, dtype: int64 pivoted_purchase.shape # 1730 ( ), 7 columns (months) (1730, 7) pivoted_purchase.dtypes 2018-07-01 int64 2018-08-01 int64 2018-09-01 int64 2018-10-01 int64 2018-11-01 int64 2018-12-01 int64 2019-01-01 int64 dtype: object

シリーズと適用機能の関係を検索した後、エラーメッセージに戻って詳細を確認し、いくつかの手がかりを見つけました。 (次の言葉はとても大きいです、私はしたくありません、このエディターは有毒です...)


実際、この問題は後で解決され、フォーマットは正しくデータフレームになりました。詳細については、別のブログを参照してください。
https://blog.csdn.net/weixin_44216391/article/details/89329804