繁体   English   中英

Python DataFrames concat 或 append 问题

[英]Python DataFrames concat or append problem

我对 Python 中的数据帧有疑问。 我正在尝试将某些行复制到新的 dataframe 但我无法弄清楚。

有2个arrays:

pokemon_data

    #   HP  Attack  Defense Sp. Atk Sp. Def Speed
0   1   45  49  49  65  65  45
1   2   60  62  63  80  80  60
2   3   80  82  83  100 100 80
3   4   80  100 123 122 120 80
4   5   39  52  43  60  50  65
... ... ... ... ... ... ... ...
795 796 50  100 150 100 150 50
796 797 50  160 110 160 110 110
797 798 80  110 60  150 130 70
798 799 80  160 60  170 130 80
799 800 80  110 120 130 90  70

800 rows × 7 columns
combats_data


    First_pokemon   Second_pokemon  Winner
0   266 298 1
1   702 701 1
2   191 668 1
3   237 683 1
4   151 231 0
... ... ... ...
49995   707 126 0
49996   589 664 0
49997   303 368 1
49998   109 89  0
49999   9   73  0

50000 rows × 3 columns

我用列创建了第三个数据集:

output1

    HP0 Attack0 Defens0 Sp. Atk0    Sp. Def0    Speed0  HP1 Attack1 Defense1    Sp. Atk1    Sp. Def1    Speed1  Winner

我想要做的是将属性从pokemon_data复制到output1中的顺序从contrasts_data

HP0 和 HP1 分别是第一个 Pokemon 的 HP 和第二个 Pokemon 的 HP。

我想在神经网络中使用该数据与 TensorFlow 来预测口袋妖怪会赢。

对于这种类型的争吵,您应该首先“融化”或“整理” combats_data ,以便每个 ID 都有自己的行,然后对两个数据帧进行“连接”或“合并”。

您没有提供最低限度的可重现示例,所以这是我的:

import pandas as pd

df1 = pd.DataFrame({'id': [1,2,3,4,5],
                    'var1': [10,20,30,40,50],
                    'var2': [15,25,35,45,55]})
df2 = pd.DataFrame({'id1': [1,2],
                    'id2': [3,4],
                    'outcome': [1,4]})

df2tidy = pd.melt(df2, id_vars=['outcome'], value_vars=['id1', 'id2'],
                  var_name='name', value_name='id')

df2tidy
#   outcome name    id
# 0 1       id1     1
# 1 4       id1     2
# 2 1       id2     3
# 3 4       id2     4


output = pd.merge(df2tidy, df1, on='id')

output
#   outcome name    id  var1    var2
# 0 1       id1     1   10      15
# 1 4       id1     2   20      25
# 2 1       id2     3   30      35
# 3 4       id2     4   40      45

然后你可以在outcome上训练某种分类器。

(顺便说一句,您应该将outcome设为 0 或 1(对于 pokemon1 与 pokemon2),而不是获胜者的实际 ID。)

所以我想根据这两个 arrays 创建新数组。 例如:

#ids represent pokemons and their attributes
pokemons = pd.DataFrame({'id': [1,2,3,4,5],
                    'HP': [10,20,30,40,50],
                    'Attack': [15,25,35,45,55],
                    'Defese' : [25,15,45,15,35]})

#here 0 or 1 represents whether first or second pokemon won
combats = pd.DataFrame({'id1': [1,2],
                    'id2': [3,4],
                    'winner': [0,1]})

#in output data i want to replace ids with attributes, the order is based on combats array
output = pd.DataFrame({'HP1': [10,20],
                    'Attack1': [15,25],
                    'Defense1': [25,15],
                      'HP2': [30,40],
                    'Attack2': [35,45],
                    'Defense2': [45,15],
                    'winner': [0,1]})

不确定它是否正确的想法。 我想训练神经网络来弄清楚口袋妖怪会赢。

这是来自 4programmers.net 论坛的用户部分的解决方案。 导入 pandas 作为 pd

if __name__ == "__main__":
    pokemon_data = pd.DataFrame({
        "Id": [1, 2, 3, 4, 5],
        "HP": [45, 60, 80, 80, 39],
        "Attack": [49, 62, 82, 100, 52],
        "Defense": [49, 63, 83, 123, 43],
        "Sp. Atk": [65, 80, 100, 122, 60],
        "Sp. Def": [65, 80, 100, 120, 50],
        "Speed": [45, 60, 80, 80, 65]})
    combats_data = pd.DataFrame({
        "First_pokemon": [1, 2, 3], 
        "Second_pokemon": [2, 3, 4], 
        "Winner": [1, 0, 1]})

    output = pokemon_data.merge(combats_data, left_on="Id", right_on="First_pokemon")
    output = output.merge(pokemon_data, left_on="Second_pokemon", right_on="Id",
                          suffixes=("_pokemon1", "_pokemon2"))

    print(output)

暂无
暂无

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

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