- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我像这个例子一样生成了一个多索引数据框
import pandas as pd
import numpy as np
iterables = [ ['co1', 'co2', 'co3', 'co4'], ['age','weight'] ]
multi = pd.MultiIndex.from_product(iterables, names= ["Spread", "attribute"])
df = pd.DataFrame(np.random.rand(80).reshape(10,8),index = range(0,10), columns = multi)
每个列都有一个名为“权重”的子级属性
我需要生成一个列表或(最好)系列,对于给定的行,该列表或系列包含该行中的所有“权重”子列。在示例图片中,我想要一个给我 0.02、0.46、0.33、0.47 的系列。
谁能推荐一个好的方法来做到这一点?我想到的解决方案都很粗糙,我怀疑我对 pandas 的索引能力理解不完整。
最佳答案
IIUC 然后你可以使用 loc
并传递一个由 slice
和列标签组成的元组来访问该级别的感兴趣的列:
In [59]:
iterables = [ ['co1', 'co2', 'co3', 'co4'], ['age','weight'] ]
multi = pd.MultiIndex.from_product(iterables, names= ["Spread", "attribute"])
df = pd.DataFrame(np.random.rand(80).reshape(10,8),index = range(0,10), columns = multi)
df
Out[59]:
Spread co1 co2 co3 \
attribute age weight age weight age weight
0 0.600947 0.509537 0.605538 0.496002 0.215206 0.075079
1 0.152956 0.922832 0.167788 0.024761 0.622378 0.983030
2 0.712478 0.603798 0.407014 0.625474 0.445592 0.903240
3 0.420569 0.576604 0.220097 0.401624 0.929464 0.512026
4 0.273088 0.032303 0.607577 0.836231 0.751845 0.181522
5 0.859699 0.274760 0.456812 0.666109 0.349961 0.237894
6 0.632754 0.603252 0.157416 0.221576 0.068355 0.121864
7 0.090595 0.035526 0.698262 0.525770 0.792618 0.220601
8 0.670236 0.805195 0.310680 0.100464 0.875299 0.853238
9 0.020501 0.405245 0.447614 0.999340 0.659616 0.709312
Spread co4
attribute age weight
0 0.297421 0.415730
1 0.235259 0.156014
2 0.365762 0.198299
3 0.695431 0.478457
4 0.331657 0.338436
5 0.943810 0.097999
6 0.638720 0.033747
7 0.646969 0.475316
8 0.623225 0.024976
9 0.023494 0.959514
In [61]:
df.loc[1,(slice(None),'weight')]
Out[61]:
Spread attribute
co1 weight 0.922832
co2 weight 0.024761
co3 weight 0.983030
co4 weight 0.156014
Name: 1, dtype: float64
解释 syntax :
df.loc[1,(slice(None),'weight')]
所以第一个参数只是你的索引 lave,第二个参数是由一个切片和一个 col 标签组成的元组,第一个成员是 slice(None)
选择所有 cols 'col1' 到'col4' 有效,然后第二个参数在下一级选择与标签 'weight' 匹配的 cols
关于python - 在 Pandas 中选择多索引列的子级别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33078463/
pandas.crosstab 和 Pandas 数据透视表似乎都提供了完全相同的功能。有什么不同吗? 最佳答案 pivot_table没有 normalize争论,不幸的是。 在 crosstab
我能找到的最接近的答案似乎太复杂:How I can create an interval column in pandas? 如果我有一个如下所示的 pandas 数据框: +-------+ |
这是我用来将某一行的一列值移动到同一行的另一列的当前代码: #Move 2014/15 column ValB to column ValA df.loc[(df.Survey_year == 201
我有一个以下格式的 Pandas 数据框: df = pd.DataFrame({'a' : [0,1,2,3,4,5,6], 'b' : [-0.5, 0.0, 1.0, 1.2, 1.4,
所以我有这两个数据框,我想得到一个新的数据框,它由两个数据框的行的克罗内克积组成。正确的做法是什么? 举个例子:数据框1 c1 c2 0 10 100 1 11 110 2 12
TL;DR:在 pandas 中,如何绘制条形图以使其 x 轴刻度标签看起来像折线图? 我制作了一个间隔均匀的时间序列(每天一个项目),并且可以像这样很好地绘制它: intensity[350:450
我有以下两个时间列,“Time1”和“Time2”。我必须计算 Pandas 中的“差异”列,即 (Time2-Time1): Time1 Time2
从这个 df 去的正确方法是什么: >>> df=pd.DataFrame({'a':['jeff','bob','jill'], 'b':['bob','jeff','mike']}) >>> df
我想按周从 Pandas 框架中的列中累积计算唯一值。例如,假设我有这样的数据: df = pd.DataFrame({'user_id':[1,1,1,2,2,2],'week':[1,1,2,1,
数据透视表的表示形式看起来不像我在寻找的东西,更具体地说,结果行的顺序。 我不知道如何以正确的方式进行更改。 df示例: test_df = pd.DataFrame({'name':['name_1
我有一个数据框,如下所示。 Category Actual Predicted 1 1 1 1 0
我有一个 df,如下所示。 df: ID open_date limit 1 2020-06-03 100 1 2020-06-23 500
我有一个 df ,其中包含与唯一值关联的各种字符串。对于这些唯一值,我想删除不等于单独列表的行,最后一行除外。 下面使用 Label 中的各种字符串值与 Item 相关联.所以对于每个唯一的 Item
考虑以下具有相同名称的列的数据框(显然,这确实发生了,目前我有一个像这样的数据集!:() >>> df = pd.DataFrame({"a":range(10,15),"b":range(5,10)
我在 Pandas 中有一个 DF,它看起来像: Letters Numbers A 1 A 3 A 2 A 1 B 1 B 2
如何减去两列之间的时间并将其转换为分钟 Date Time Ordered Time Delivered 0 1/11/19 9:25:00 am 10:58:00 am
我试图理解 pandas 中的下/上百分位数计算,但有点困惑。这是它的示例代码和输出。 test = pd.Series([7, 15, 36, 39, 40, 41]) test.describe(
我有一个多索引数据框,如下所示: TQ bought HT Detailed Instru
我需要从包含值“低”,“中”或“高”的数据框列创建直方图。当我尝试执行通常的df.column.hist()时,出现以下错误。 ex3.Severity.value_counts() Out[85]:
我试图根据另一列的长度对一列进行子串,但结果集是 NaN .我究竟做错了什么? import pandas as pd df = pd.DataFrame([['abcdefghi','xyz'],
我是一名优秀的程序员,十分优秀!