Python 如何将数据帧行转换为标题?

Python 如何将数据帧行转换为标题?,python,pandas,Python,Pandas,在151个不同的数据帧中,我有一些关于前151个口袋妖怪的数据 id identifier pokemon_id stat_id base_stat local_language_id name 36 7 Squirtle 7 1 44 9 HP 37 7 Squirtle 7 2 48 9 Attack 38 7 Squirtle 7 3 65 9 Defense 39 7 Squirtl

在151个不同的数据帧中,我有一些关于前151个口袋妖怪的数据

    id  identifier  pokemon_id  stat_id base_stat   local_language_id   name
36  7   Squirtle    7   1   44  9   HP
37  7   Squirtle    7   2   48  9   Attack
38  7   Squirtle    7   3   65  9   Defense
39  7   Squirtle    7   4   50  9   Special Attack
40  7   Squirtle    7   5   64  9   Special Defense
41  7   Squirtle    7   6   43  9   Speed


    id  identifier  pokemon_id  stat_id base_stat   local_language_id   name
18  4   Charmander  4   1   39  9   HP
19  4   Charmander  4   2   52  9   Attack
20  4   Charmander  4   3   43  9   Defense
21  4   Charmander  4   4   60  9   Special Attack
22  4   Charmander  4   5   50  9   Special Defense
23  4   Charmander  4   6   65  9   Speed
我真正想要的是每个口袋妖怪一行,每个统计数据作为新数据帧的一列。差不多

id    identifier    pokemon_id   HP  Attack    ...
4     Charmander    4            39  52        ...
7     Squirtle      7            44  48        ...

有没有一种简单的方法可以通过熊猫数据框实现这一点?

我相信这会满足您的需求:

df.groupby(['id', 'identifier', 'name']).base_stat.first().unstack('name')

我相信这会满足你的要求:

df.groupby(['id', 'identifier', 'name']).base_stat.first().unstack('name')
您可以使用:

如果所有
数据帧
都在列表
dfs
中,请与
列表理解一起使用

dfs = [df1, df2]

df = pd.concat([df.pivot_table(index=['id','identifier'], 
                               columns='name', 
                               values='base_stat', 
                               aggfunc='first') for df in dfs])
print (df)
name           Attack  Defense  HP  Special Attack  Special Defense  Speed
id identifier                                                             
7  Squirtle        48       65  44              50               64     43
4  Charmander      52       43  39              60               50     65
上次使用
reset_index
时,如果使用
pandas bellow 0.18.0
忽略
rename_轴
并使用
df.columns.name=None

df = pd.concat([df.pivot_table(index=['id','identifier'], 
                               columns='name', 
                               values='base_stat', 
                               aggfunc='first') for df in dfs])
                  .reset_index()
                  .rename_axis(None, axis=1)
print (df)
   id  identifier  Attack  Defense  HP  Special Attack  Special Defense  Speed
0   7    Squirtle      48       65  44              50               64     43
1   4  Charmander      52       43  39              60               50     65
您可以使用:

如果所有
数据帧
都在列表
dfs
中,请与
列表理解一起使用

dfs = [df1, df2]

df = pd.concat([df.pivot_table(index=['id','identifier'], 
                               columns='name', 
                               values='base_stat', 
                               aggfunc='first') for df in dfs])
print (df)
name           Attack  Defense  HP  Special Attack  Special Defense  Speed
id identifier                                                             
7  Squirtle        48       65  44              50               64     43
4  Charmander      52       43  39              60               50     65
上次使用
reset_index
时,如果使用
pandas bellow 0.18.0
忽略
rename_轴
并使用
df.columns.name=None

df = pd.concat([df.pivot_table(index=['id','identifier'], 
                               columns='name', 
                               values='base_stat', 
                               aggfunc='first') for df in dfs])
                  .reset_index()
                  .rename_axis(None, axis=1)
print (df)
   id  identifier  Attack  Defense  HP  Special Attack  Special Defense  Speed
0   7    Squirtle      48       65  44              50               64     43
1   4  Charmander      52       43  39              60               50     65

组/取消堆栈与透视表基本相同。组/取消堆栈与透视表基本相同。