pandas - 提取信息以使用 pandas
问题描述
我有这个数据框:
列非空 Dtype
0 nombre 74 非空对象
1 个制造商 - 74 个非空对象
2 卡路里 -74 非空 -int64
3 蛋白质 -74 非空 -int64
4 grasa -74 非空 -int64
5 sodio -74 非空 -int64
6 纤维饮食 -74 非空 -float64 7 carbohidratos -74非空 -float64 8 azúcar -74 非空 -int64
9 钾 -74 非空 -int64
10 维生素矿物质 -74 非空 -int64
我试图提取这样的信息:
cereal_df.loc[cereal_df['fabricante'] == 'Kelloggs', 'sodio']
输出是(很好,这就是我想在这种情况下提取的内容,对吧?)
2 260 3 140 6 125 16 290 17 90 19 140 21 220 24 125 25 200 26 0 27 27 240 37 170 38 170 43 150 43 150 45 190 46 220 47 170 170 50 320 55 210 57 0 59 290 63 70 63 70 64 230数据类型:int64
到目前为止,这就是我所需要的,但是当我尝试编写这样的函数时(为了获得信心):
def valor_medio_intervalo(fabricante, variable, confianza):
subconjunto = cereal_df.loc[cereal_df['fabricante'] == fabricante, cereal_df[variable]]
inicio, final = sm.stats.DescrStatsW(subconjunto[variable]).zconfint_mean(alpha = 1 - confianza)
return inicio, final
然后我运行函数:
valor_medio_intervalo('Kelloggs', 'azúcar', 0.95)
输出是:
KeyError Traceback (most recent call last)
<ipython-input-57-11420ac4d15f> in <module>()
1 #TEST_CELL
----> 2 valor_medio_intervalo('Kelloggs', 'azúcar', 0.95)
7 frames
/usr/local/lib/python3.7/dist-packages/pandas/core/indexing.py in _validate_read_indexer(self, key, indexer, axis, raise_missing)
1296 if missing == len(indexer):
1297 axis_name = self.obj._get_axis_name(axis)
-> 1298 raise KeyError(f"None of [{key}] are in the [{axis_name}]")
1299
1300 # We (temporarily) allow for some missing keys with .loc, except in
KeyError: "None of [Int64Index([ 6, 8, 5, 0, 8, 10, 14, 8, 6, 5, 12, 1, 9, 7, 13, 3, 2,\n 12, 13, 7, 0, 3, 10, 5, 13, 11, 7, 12, 12, 15, 9, 5, 3, 4,\n 11, 10, 11, 6, 9, 3, 6, 12, 3, 13, 6, 9, 7, 2, 10, 14, 3,\n 0, 0, 6, -1, 12, 8, 6, 2, 3, 0, 0, 0, 15, 3, 5, 3, 14,\n 3, 3, 12, 3, 3, 8],\n dtype='int64')] are in the [columns]"
我不明白发生了什么事。感谢您的帮助或任何提示。提前致谢
解决方案
刚刚得到检查代码的答案:
def valor_medio_intervalo(fabricante, variable, confianza):
subconjunto = cereal_df.loc[cereal_df['fabricante'] == fabricante,cereal_df[variable]]
inicio, final = sm.stats.DescrStatsW(subconjunto[variable]).zconfint_mean(alpha = 1 -
confianza)
return inicio, final
在行中
inicio, final = sm.stats.DescrStatsW(subconjunto[variable]).zconfint_mean(alpha = 1 -
confianza)
这
(subconjunto[variable])
只是
(subconjunto)
推荐阅读
- android - 如何将 Json 数组序列化为要在 Android Studio 中使用的数组
- javascript - 为什么警报在不工作时只工作一次?
- django - 生成数据表以在 Django 模板中进行迭代
- python - 尝试配置 gunicorn
- javascript - 不使用 split 方法查找字符串中最长的单词
- java - 如何在 JSON 中转换部分字符串?
- java - 如何在 Java 中创建高性能记录器
- python - 带有来自另一个模块的对象的 init 方法
- java - 将base64字符串转换为十进制时出错
- c++ - 用右值引用和可变参数模板替换 C++ std::bind 到 lambda