- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我知道使用 groupby 和/或 pivot_table 和/或 stack 很容易做到这一点 - 我似乎无法将其从基础 1 中删除。我的笔记并没有很好地告诉我如何做到这一点。我以为我已经接近 pandas 文档中的 pivot_table - 但甚至不能让它达到一个级别 - 更不用说 2 了。因为我不想聚合任何东西。我的笔记都是关于聚合的……
感谢接受任何建议
创建第一个数据框的代码:
df2 = pd.DataFrame({'CPC_qtr_root': {13: 0.13493790567404607,
14: 0.14353736611331172,
15: 0.10359919568913414,
16: 0.077153346715340618,
17: 0.066759430932458397,
39: 0.12067193385680651,
40: 0.049033000970486448,
41: 0.047640864406214359,
42: 0.040086869604689483,
43: 0.038795815932666726,
100: 0.11017683494905577,
101: 0.15510499735697988,
102: 0.16478351543691827,
103: 0.091894700285988867,
104: 0.0359603120618152},
'Country': {13: u'Afghanistan',
14: u'Afghanistan',
15: u'Afghanistan',
16: u'Afghanistan',
17: u'Afghanistan',
39: u'Albania',
40: u'Albania',
41: u'Albania',
42: u'Albania',
43: u'Albania',
100: u'Angola',
101: u'Angola',
102: u'Angola',
103: u'Angola',
104: u'Angola'},
'IncomeLevel': {13: 'Lower Income',
14: 'Lower Income',
15: 'Lower Income',
16: 'Lower Income',
17: 'Lower Income',
39: 'Upper Middle Income',
40: 'Upper Middle Income',
41: 'Upper Middle Income',
42: 'Upper Middle Income',
43: 'Upper Middle Income',
100: 'Lower Middle Income',
101: 'Lower Middle Income',
102: 'Lower Middle Income',
103: 'Lower Middle Income',
104: 'Lower Middle Income'},
'Rate': {13: 27.0,
14: 37.0,
15: 35.0,
16: 39.0,
17: 48.0,
39: 95.0,
40: 95.0,
41: 96.0,
42: 93.0,
43: 96.0,
100: 36.0,
101: 65.0,
102: 66.0,
103: 52.0,
104: 52.0},
'Year': {13: 2000,
14: 2001,
15: 2002,
16: 2003,
17: 2004,
39: 2000,
40: 2001,
41: 2002,
42: 2003,
43: 2004,
100: 2000,
101: 2001,
102: 2002,
103: 2003,
104: 2004}})
最佳答案
df3 = df2.set_index(['Year','Country']).stack().unstack(1)
print (df3)
Country Afghanistan Albania Angola
Year
2000 CPC_qtr_root 0.134938 0.120672 0.110177
IncomeLevel Lower Income Upper Middle Income Lower Middle Income
Rate 27 95 36
2001 CPC_qtr_root 0.143537 0.049033 0.155105
IncomeLevel Lower Income Upper Middle Income Lower Middle Income
Rate 37 95 65
2002 CPC_qtr_root 0.103599 0.0476409 0.164784
IncomeLevel Lower Income Upper Middle Income Lower Middle Income
Rate 35 96 66
2003 CPC_qtr_root 0.0771533 0.0400869 0.0918947
IncomeLevel Lower Income Upper Middle Income Lower Middle Income
Rate 39 93 52
2004 CPC_qtr_root 0.0667594 0.0387958 0.0359603
IncomeLevel Lower Income Upper Middle Income Lower Middle Income
Rate 48 96 52
获取混合类型:
print (df3.head().applymap(type))
Country Afghanistan Albania Angola
Year
2000 CPC_qtr_root <class 'float'> <class 'float'> <class 'float'>
IncomeLevel <class 'str'> <class 'str'> <class 'str'>
Rate <class 'float'> <class 'float'> <class 'float'>
2001 CPC_qtr_root <class 'float'> <class 'float'> <class 'float'>
IncomeLevel <class 'str'> <class 'str'> <class 'str'>
关于python - 如何使用 Groupby、Pivot_table、Stack 和 Unstack reshape Pandas Dataframe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50002754/
如何检查字符串是否被 reshape ?示例:“aab”返回 0,因为“a”无法 reshape 为该字符串或任何其他更短的字符串。 另一个例子是“aabbaab”返回 1,因为“aabb”可以被 r
我无法清楚地理解theano的reshape。我有一个形状的图像矩阵: [batch_size, stack1_size, stack2_size, height, width] ,其中有 s
如何检查字符串是否被 reshape ?示例:“aab”返回 0,因为“a”无法 reshape 为该字符串或任何其他更短的字符串。 另一个例子是“aabbaab”返回 1,因为“aabb”可以被 r
这是原始数据 a=[[1,2,3,4,5,6], [7,8,9,10,11,12]] 我想把它转换成这样的格式: b=[[1,2,3,7,8,9], [4,5,6,10,11,12]] a
我目前正在学习 CS231 作业,我意识到一些令人困惑的事情。在计算梯度时,当我第一次 reshape x 然后得到转置时,我得到了正确的结果。 x_r=x.reshape(x.shape[0],-1
这个问题在这里已经有了答案: Reshaping multiple sets of measurement columns (wide format) into single columns (lon
我有一个包含超过 1500 列的宽格式数据集。由于许多变量都是重复的,我想将其 reshape 为长形式。然而,r 抛出一个错误: Error in guess(varying) : Failed
我有一个长格式的数据框狗,我正在尝试使用 reshape() 函数将其重新格式化为宽格式。目前看起来是这样的: dogid month year trainingtype home scho
这个问题在这里已经有了答案: how to reshape an N length vector to a 3x(N/3) matrix in numpy using reshape (1 个回答)
我对 ndarray.reshape 的结构有疑问.我读过 numpy.reshape()和 ndarray.reshape是 python 中用于 reshape 数组的等效命令。 据我所知,num
所以这是我的麻烦:我想将一个长格式的数据文件改成宽格式。但是,我没有唯一的“j”变量;长格式文件中的每条记录都有几个关键变量。 例如,我想这样做: | caseid | gender | age |
Whis 这个数据框, df df id parameter visit value sex 1 01 blood V1 1 f 2 01 saliva V
我有一个列表,其中包含几个不同形状的 numpy 数组。我想将这个数组列表 reshape 为一个 numpy 向量,然后更改向量中的每个元素,然后将其 reshape 回原始数组列表。 例如: 输入
我有一个形状为 (1800,144) 的数组 (a) 其中 a[0:900,:] 都是实数,后半部分数组 a[900:1800,:] 全部为零。我想把数组的后半部分水平地放在前半部分旁边,然后将它们推
我有一个如下所示的数组: array([[0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1], [2, 2, 2, 2, 2
我正在创建一个 tf.Variable(),然后使用该变量创建一个简单的函数,然后我使用 tf.reshape() 展平原始变量,然后我在函数和展平变量之间使用了 tf.gradients()。为什么
我有一个名为 data 的数据框,我试图从中识别任何异常价格。 数据框头部看起来像: Date Last Price 0 29/12/2017 487.74 1 28/
我有一个 float vec 数组,我想对其进行 reshape vec.shape >>> (3,) len(vec[0]) # all 3 rows of vec have 150 columns
tl;dr 我可以在不使用 numpy.reshape 的情况下将 numpy 数组的 View 从 5x5x5x3x3x3 reshape 为 125x1x1x3x3x3 吗? 我想对一个体积(大小
set.seed(123)data <- data.frame(ID = 1:10, weight_hus = rnorm(10, 0, 1),
我是一名优秀的程序员,十分优秀!