简体   繁体   English

在 Python 中创建交叉表时出错。 TypeError:列表索引必须是整数或切片,而不是元组

[英]Error creating a Crosstab in Python. TypeError: list indices must be integers or slices, not tuple

I'm trying to do a Crosstab and I'm getting the error TypeError: list indices must be integers or slices, not tuple我正在尝试做一个交叉表,我收到错误TypeError: list indices must be integers or slices, not tuple

I have already including parenthesis and brackets, but nothing works.我已经包括了括号和方括号,但没有任何效果。

This is the code这是代码

y1 = np.array([df_train[df_train['dep_delayed_15min'] == 'Y']['UniqueCarrier'].value_counts().values])
y2 = np.array([df_train[df_train['dep_delayed_15min'] == 'N']['UniqueCarrier'].value_counts().values])
y3 = y1+y2
y4 = y1/y3
y5 = y2/y3
y6 = set(df_train['UniqueCarrier'])

d = {'UniqueCarrier': [y6], 'Perc_Y': [y4], 'Perc_N': [y5]}
df = pd.DataFrame([d])

df.head()

pd.crosstab[df['UniqueCarrier'], df['Perc_Y'], df['Perc_N']]

This is what I get form df.head() that is not what I need for the next step, that is creating the crosstab with the numbers.这就是我从 df.head() 中得到的,这不是我下一步需要的,即用数字创建交叉表。

    UniqueCarrier   Perc_Y  Perc_N
0   [{CO, FL, UA, DH, NW, B6, AS, DL, HP, EV, AA, ...   [[[0.21343323166688768, 0.1879379910809089, 0....   [[[0.7865667683331123, 0.8120620089190911, 0.8...

Unless I'm very confused pd.crosstab is a function so should be invoked with parentheses除非我很困惑 pd.crosstab 是 function 所以应该用括号调用

pd.crosstab(df['UniqueCarrier'], df['Perc_Y'], df['Perc_N'])

暂无
暂无

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

相关问题 TypeError:列表索引必须是整数或切片,而不是元组? - TypeError: list indices must be integers or slices, not tuple? 蟒蛇。 当所有索引都不可能是元组时,列表索引必须是整数或切片而不是元组 - Python. list indices must be integers or slices not tuple when none of the indices could possibly be tuple TypeError:列表索引必须是整数或切片,而不是元组 - TypeError: list indices must be integers or slices, not tuple 类型错误:列表索引必须是整数或切片,而不是在 python 中使用 sys 导入的元组 - TypeError: list indices must be integers or slices, not tuple with sys import in python Python棋盘游戏-“类型错误:列表索引必须是整数或切片,而不是元组” - Python Board Game - "TypeError: list indices must be integers or slices, not tuple" Python 类型错误:列表索引必须是整数或切片,而不是元组 - Python TypeError: list indices must be integers or slices, not tuple 错误:TypeError:列表索引必须是整数或切片,而不是元组一直显示 - Error: TypeError: list indices must be integers or slices, not tuple keeps showing 如何解决 Python 中的“TypeError:元组索引必须是整数或切片”错误? - How to solve "TypeError: tuple indices must be integers or slices" error in Python? Python:“列表索引必须是整数或切片,而不是元组” - Python:'list indices must be integers or slices, not tuple' “列表索引必须是整数或切片,而不是元组”错误 - "List indices must be integers or slices, not tuple" error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM