python - Pandas:包含变量名称和值的多列:如何使用 Pivot?

标签 python pandas pivot pivot-table lreshape

我不确定是否以及如何进行以下转换:

我有一个如下所示的数据框:

Index   Name    detail1 detail1_value   detail2  detail2_value   detail3    detail3_value
1     Albert    Age      30             Group       A            Hometown   beautifulplace
2     Bea       Age      28             Hometown    anotherplace None       None
3     Celin     Age      45             Group       B            None       None
4     Dave      Group    A              None        None         None       None

但正如你可以想象的,我的目标是:

Index   Name    Age Group   Hometown
1     Albert    30  A   beautifulplace
2     Bea       28      anotherplace
3     Celin     45  B   
4     Dave          A   

我很确定 ech 细节只出现一次。 为了让事情变得复杂:我不确定每个细节是否完全相同(在某些情况下,例如家乡而不是家乡)。

到目前为止我能看到的唯一解决方案是从每对列中生成单个数据透视表(例如detail1和detail1_value)。在第二步中,创建一个新的数据集,并搜索每个数据透视表,例如关于年龄的信息。 但我对 python 的信任告诉我,一定有更好的方法......

谢谢!

PS: 可能有帮助:

dataset = pd.DataFrame({'Name': ['Albert', 'Bea', 'Celine', 'Dave'],
                        'detail1': ['Age', 'Age', 'Age', 'Group'],
                        'detail1_value': ['30', '28', '45', 'A'],
                        'detail2': ['Group', 'Hometown', 'Group', None],
                        'detail2_value': ['A', 'anotherplace', 'B', None],
                        'detail3': ['Hometown', None, None, None],
                        'detail3_value': ['beautifulplace', None, None, None]})

最佳答案

您可以使用lreshapepivot :

#get columns names dynamically 
a = dataset.columns[dataset.columns.str.endswith('_value')]
b = dataset.columns[dataset.columns.str.startswith('detail')].difference(a)

df = pd.lreshape(dataset, {'detail':b, 'value':a})
print (df)
     Name           value    detail
0  Albert              30       Age
1     Bea              28       Age
2  Celine              45       Age
3    Dave               A     Group
4  Albert               A     Group
5     Bea    anotherplace  Hometown
6  Celine               B     Group
7  Albert  beautifulplace  Hometown


df = df.pivot(index='Name', columns='detail', values='value')
print (df)
detail   Age Group        Hometown
Name                              
Albert    30     A  beautifulplace
Bea       28  None    anotherplace
Celine    45     B            None
Dave    None     A            None

最后进行一些数据清理:

df = df.reset_index().rename_axis(None, axis=1)
print (df)
     Name   Age Group        Hometown
0  Albert    30     A  beautifulplace
1     Bea    28  None    anotherplace
2  Celine    45     B            None
3    Dave  None     A            None

关于python - Pandas:包含变量名称和值的多列:如何使用 Pivot?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45592411/

相关文章:

mysql - 当条件位于不同行和列时的 CASE 语句

MySQL Pivot 将记录转为列出所有数据的列

sql - MSSQL 2008 R2 中没有聚合函数的数据透视

python - 如何根据对象实例自定义 Django 内联管理表单

python - 无论如何提供 os.walk 的进展吗?

python - 如何在 pandas/python 中查看 excel 电子表格的公式?

python - Pandas - 将所有列中的特定值替换为另一列中的相应值

python - 使用 Python 进行 IC 设计/验证

python - python 中从列表中选择项目到多个变量中

python - 在Python中计算每行中特定列的接下来3行的最大值