简体   繁体   English

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

[英]How can I turn dataframe rows into headers?

I have some data on each of the first 151 pokemon in 151 different dataframes. 我在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

What I would really like is one row per pokemon with each stat as a column of a new dataframe. 我真正想要的是每个口袋妖怪一行,每个统计信息作为新数据框的一列。 Something like 就像是

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

Is there an easy way to do that with a pandas dataframe? 有没有一种简单的方法可以对熊猫数据框执行此操作?

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

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

You can use pivot_table : 您可以使用pivot_table

df = df.pivot_table(index=['id','identifier'], 
                    columns='name', 
                    values='base_stat', 
                    aggfunc='first')

print (df)
name           Attack  Defense  HP  Special Attack  Special Defense  Speed
id identifier                                                             
7  Squirtle        48       65  44              50               64     43

If all DataFrames are in list dfs , use concat with list comprehension : 如果所有DataFrames都在列表dfsDataFrames concatlist comprehension一起使用:

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

Last use reset_index with rename_axis (new in pandas 0.18.0 ), if use pandas bellow 0.18.0 omit rename_axis and use df.columns.name = None : 最后一次将reset_indexrename_axis reset_index使用( pandas 0.18.0新增功能),如果使用的是rename_axis pandas bellow 0.18.0 ,则忽略rename_axis并使用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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM