我的函数遇到以下回溯错误:
TypeError: unsupported operand type(s) for &: 'str' and 'str'
这是我的代码:
def age():
thirties_df = (df.loc[df['age'] <= 39]) & (df.loc[df['age'] >= 30])
fourties_df = (df.loc[df['age'] <= 49]) & (df.loc[df['age'] >= 40])
fifties_df = (df.loc[df['age'] <= 59]) & (df.loc[df['age'] >= 50])
sixties_df = (df.loc[df['age'] <= 69]) & (df.loc[df['age'] >= 60])
seventies_df = (df.loc[df['age'] <= 79]) & (df.loc[df['age'] >= 70])
eighties_df = (df.loc[df['age'] <= 89]) & (df.loc[df['age'] >= 80])
for i in thirties_df, fourties_df, fifties_df, sixties_df,
\ seventies_df, eighties_df:
答案 0 :(得分:0)
错误与在实例化这些变量时使用可疑的'&'字符有关。(这两个字符串之间的'&'毫无意义,但会产生错误)
另外,放and
也没有意义(我想您是在尝试放这个)
在字符串之间,因为最后一个赋值用作该变量的值。
看到这个:-
>>> var = 'str1' and 'str2'
>>> var
'str2'
答案 1 :(得分:0)
没有 df
很难说,但看起来你的年龄列是字符串。括号错位:df['age'] <= 39
是一系列 dtype bool。并且 2 Series of bool 的 & 本身就是一个 Series of bool,您可以使用它在 DataFrame 上进行选择 (如果长度合适)
df = pd.DataFrame(
map(str, np.random.randint(5, 90, size=1000)),
columns=('age',)
) # what I think your df roughly looks like.
df['age'] = df['age'].astype('int') # make sure age is numeric
# building thirties_df step by step
younger_than_forty = df['age'] <= 39
older_than_thirty = df['age'] >= 30
thirties_df = df[younger_than_forty & older_than_thirty]
# or all at once and using loc like in your question
thirties_df = df.loc[(df['age'] <= 39) & (df['age'] >= 30)]
# or using between
thirties_df = df[df['age'].between(30, 39)]
# because
assert all(df['age'].between(30, 39) == younger_than_forty & older_than_thirty)
# so age could become something like
def age(df):
min_age = 30
max_age = 80
buckets = (
df[df["age"].between(n, n + 9)]
for n in range(min_age, max_age + 1, 10)
)
for i in buckets:
print(i)