- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我需要拆开联系人列表(ID、亲戚、电话号码...),以便各列保持特定顺序。
给定一个索引,数据帧 UNSTACK 通过一个一个地拆散单个列来操作,即使应用于几个列也是如此
数据有
df_have=pd.DataFrame.from_dict({'ID': {0: '100', 1: '100', 2: '100', 3: '100', 4: '100', 5: '200', 6: '200', 7: '200', 8: '200', 9: '200'},
'ID_RELATIVE': {0: '100', 1: '100', 2: '150', 3: '150', 4: '190', 5: '200', 6: '200', 7: '250', 8: '290', 9: '290'},
'RELATIVE_ROLE': {0: 'self', 1: 'self', 2: 'father', 3: 'father', 4: 'mother', 5: 'self', 6: 'self', 7: 'father', 8: 'mother', 9: 'mother'},
'PHONE': {0: '111111', 1: '222222', 2: '333333', 3: '444444', 4: '555555', 5: '123456', 6: '456789', 7: '987654', 8: '778899', 9: '909090'}})
需要数据
df_want=pd.DataFrame.from_dict({'ID': {0: '100', 1: '200'},
'ID_RELATIVE_1': {0: '100', 1: '200'},
'RELATIVE_ROLE_1': {0: 'self', 1: 'self'},
'PHONE_1_1': {0: '111111', 1: '123456'},
'PHONE_1_2': {0: '222222', 1: '456789'},
'ID_RELATIVE_2': {0: '150', 1: '250'},
'RELATIVE_ROLE_2': {0: 'father', 1: 'father'},
'PHONE_2_1': {0: '333333', 1: '987654'},
'PHONE_2_2': {0: '444444', 1: 'nan'},
'ID_RELATIVE_3': {0: '190', 1: '290'},
'RELATIVE_ROLE_3': {0: 'mother', 1: 'mother'},
'PHONE_3_1': {0: '555555', 1: '778899'},
'PHONE_3_2': {0: 'nan', 1: '909090'}})
所以,最后,我需要将 ID 作为索引,并拆散其他列,这些列将因此成为 ID 的属性。
通常的 unstack 过程提供“正确”的输出,但形状错误。
df2=have.groupby(['ID'])['ID_RELATIVE','RELATIVE_ROLE','PHONE'].apply(lambda x: x.reset_index(drop=True)).unstack()
这将需要对列重新排序并删除一些重复项(按列,而不是按行),以及一个 FOR 循环。我想避免使用这种方法,因为我正在寻找一种更“优雅”的方式来通过分组/堆叠/取消堆叠/旋转等方式实现所需的结果。
非常感谢
最佳答案
解决方案有两个主要步骤 - 首先按没有 PHONE
的所有列分组,将列名称转换为有序分类以进行正确排序,然后按 ID
分组:
c = ['ID','ID_RELATIVE','RELATIVE_ROLE']
df = df_have.set_index(c+ [df_have.groupby(c).cumcount().add(1)])['PHONE']
df = df.unstack().add_prefix('PHONE_').reset_index()
df = df.set_index(['ID', df.groupby('ID').cumcount().add(1)])
df.columns = pd.CategoricalIndex(df.columns, categories=df.columns.tolist(), ordered=True)
df = df.unstack().sort_index(axis=1, level=1)
df.columns = [f'{a}_{b}' for a, b in df.columns]
df = df.reset_index()
print (df)
ID ID_RELATIVE_1 RELATIVE_ROLE_1 PHONE_1_1 PHONE_2_1 ID_RELATIVE_2 \
0 100 100 self 111111 222222 150
1 200 200 self 123456 456789 250
RELATIVE_ROLE_2 PHONE_1_2 PHONE_2_2 ID_RELATIVE_3 RELATIVE_ROLE_3 PHONE_1_3 \
0 father 333333 444444 190 mother 555555
1 father 987654 NaN 290 mother 778899
PHONE_2_3
0 NaN
1 909090
如果需要更改 PHONE
列中的数字顺序:
df.columns = [f'{a.split("_")[0]}_{b}_{a.split("_")[1]}'
if 'PHONE' in a
else f'{a}_{b}' for a, b in df.columns]
df = df.reset_index()
print (df)
ID ID_RELATIVE_1 RELATIVE_ROLE_1 PHONE_1_1 PHONE_1_2 ID_RELATIVE_2 \
0 100 100 self 111111 222222 150
1 200 200 self 123456 456789 250
RELATIVE_ROLE_2 PHONE_2_1 PHONE_2_2 ID_RELATIVE_3 RELATIVE_ROLE_3 PHONE_3_1 \
0 father 333333 444444 190 mother 555555
1 father 987654 NaN 290 mother 778899
PHONE_3_2
0 NaN
1 909090
关于python - Pandas :如何在保持列成对的同时按列组取消堆叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57869323/
如何将单元格(在 UICollectionView 中)堆叠在一起? Feedly 做得很好,我想知道是否可以以某种方式操纵 zIndex 属性以在 UICollectionView 的导航中获得类似
我正在尝试堆叠 div 以使用和图像显示顶部和底部淡入淡出。 外部 div(“旋转器”)有一个背景图像,该图像在顶部和底部具有渐变,在中间是透明的。在中间,我想显示内容或图像,并将外部 div 背景图
我有两个动画(在 CSS 中)div,它们围绕彼此旋转。我想在中间为文本添加另一个 div。但是,如果我将 div 放在这两个中的任何一个中,它就会与父 div 一起设置动画。如果我尝试定位它,它就位
图中显示了我想要的 View 。 最佳答案 为此你可以采取 FrameLayout . 例如 - 1: 更新: 例如 - 2:精湛的示例和技巧,可在此处找到:http://w
使用 tf.keras.layers.RNN 的 TensorFlow (1.13.1) 中多层/堆叠 RNN 的初始状态所需的结构是什么? API? 我尝试了以下方法: lstm_cell_size
我在制作条形图时遇到问题,其中 y 轴不是计数,而是变量的值。 我使用stat=identity,这对于一个变量来说很好。但是,如果我有两个变量并且想要创建堆叠/闪避条形图怎么办? 我这里有一些模拟数
我有一个栅格路径列表(rplist = 912 raster pathe for 912 rasters (19yeras)(12months)(4weeks))如下: [1] "C:/Users
我已经完成了一个项目,现在需要更改该项目,以便在变量位于数组中时显示一个 div,如果变量不在数组中则显示另一个 div。 通常我会这么做 祝你愉快,但是我得到的代码已经包含大量 html 和 ph
我在制作条形图时遇到问题,其中 y 轴不是计数,而是来自变量的值。 我使用 stat=identity,这对一个变量来说没问题。但是,如果我有两个变量并想创建一个堆叠/闪避条形图怎么办? 我这里有一些
我有一个栅格路径列表(rplist = 912 raster pathe for 912 rasters (19yeras)(12months)(4weeks))如下: [1] "C:/Users
我正在尝试将分数和耗时标签(scoreAndTimer)添加到我已经工作的贪吃蛇游戏代码中。问题是当我使用 ScoreAndTimer.setText(); 时它与之前的文本堆叠在一起。 我尝试 se
有没有办法将 GTK+ 小部件放入堆栈中? IE。在标签上放置一个按钮,使按钮覆盖标签的一部分。 最佳答案 您可以使用 GtkFixed 作为布局。这将允许您控制子小部件的确切位置,并且它们可以重叠。
我有以下代码: for (Map.Entry e : classes.entrySet()) { ClassReader reader = e.getValue(); ClassWri
我正在尝试根据类别的计数(或比例)更改我的(堆叠)条宽,作为示例,我使用了钻石数据集。我想根据每个类别(变量 cut )的频率看到不同的宽度。我首先创建了一个变量 cut_prop然后用下面的代码绘制
我目前正在 UWP 中开展一个项目,我有一个 CommandBar,我想将其从 Hidden 变为 Compact 如果鼠标移动。五秒钟后(如果鼠标不移动)CommandBar 应该再次返回到Hidd
我是 Swift 新手,并尝试在学习过程中编写一本交互式书籍。我使用 CATransform3DMakeRotation 创建了封面效果。 正如您所看到的,当我堆叠 View 时,问题就出现了。如果我
我已经阅读了 finish(); 命令和 FLAG_ACTIVITY_CLEAR_TOP 命令,还查看了 Common Ware 关于终止应用程序的回答,但我不确定如何放置这进入我的应用程序。 基本上
在我的引擎中,我从成对的顶点/像素着色器文件中加载 Cg 着色器。我希望能够堆叠着色器以组合它们(照明+ Material 等)。除了将着色器分解为单独的函数然后从中创建单个着色器脚本字符串之外,您是
我有一个 Activity 应该自行完成并关闭应用程序。现在,在某些情况下,根据用户导航到 Activity 的方式而变化, Activity 正在堆叠。当 Activity 堆积时,然后调用 fin
我有 10 个具有完全相同的列和数据类型的 csv 文件。什么是最快/最有效的堆叠方式? CSV1: col1 | col2 | col3 1 | 'a' | 0.1 2 | 'b'
我是一名优秀的程序员,十分优秀!