我有以下内容:
implementation 'com.bignerdranch.android:recyclerview-multiselect:0.2'
我想要以下内容:
t len id accession
t1 150 A X1
t1 150 B X2
t1 150 C X3
t2 200 E X4
t2 200 F X5
t2 200 G X6
是否有如pandas.DataFrame.pivot所述的旋转数据框的方法,但同时将列条目合并为一个列?
答案 0 :(得分:1)
我提出的解决方案结合了groupby
和agg
。让我们从数据开始:
import pandas as pd
data = pd.DataFrame({
"t": ["t1", "t1", "t1", "t2", "t2", "t2"],
"len": [150, 150, 150, 200, 200, 200],
"id": ["A", "B", "C", "E", "F", "G"],
"accession": ["X1", "X2", "X3", "X4", "X5", "X6"]})
解决方案
data.groupby(["t", "len"]).agg({"id": lambda x: ",".join(x),
"accession": lambda x: ",".join(x)}).reset_index()
输出
t len id accession
0 t1 150 A,B,C X1,X2,X3
1 t2 200 E,F,G X4,X5,X6
答案 1 :(得分:0)