python - 如何连接/加入这三个数据框

标签 python python-3.x pandas dataframe

我有三个数据框 df_Male , df_female , Df_TransGender

示例数据框

df_Male

continent   avg_count_country   avg_age
  Asia          55                5
  Africa        65                10
  Europe        75                8

df_Female

continent   avg_count_country   avg_age
  Asia          50                7
  Africa        60                12
  Europe        70                0

df_Transgender

continent   avg_count_country   avg_age
  Asia          30                6
  Africa        40                11
  America       80                10

现在我像下面这样连接

frames = [df_Male, df_Female, df_Transgender]
df = pd.concat(frames, keys=['Male', 'Female', 'Transgender'])

如您所见,America 出现在 df_transgender 中,欧洲同样出现在 df_Maledf_Female

所以我必须以某种方式连接它,使其看起来像下面但不是手动的,因为可能有大量的行

              continent  avg_count_country  avg_age
Male        0      Asia                 55        5
            1    Africa                 65       10
            2    Europe                 75        8
            3    America                 0        0
Female      0      Asia                 50        7
            1    Africa                 60       12
            2    Europe                 70        0
            3    America                 0        0
Transgender 0      Asia                 30        6
            1    Africa                 40       11
            2    America                80       10
            3    Europe                 0         0

所以对于其他 continentavg_count_countryavg_age 应该是 0

最佳答案

您可以在连接之前添加“性别”列。

我们使用 Categorical Datagroupby 计算笛卡尔积。这也应该会产生性能优势。

df = pd.concat([df_Male.assign(gender='Male'),
                df_Female.assign(gender='Female'),
                df_Transgender.assign(gender='Transgender')])

for col in ['gender', 'continent']:
    df[col] = df[col].astype('category')

res = df.groupby(['gender', 'continent']).first().fillna(0).astype(int)

print(res)

                       avg_count_country  avg_age
gender      continent                            
Female      Africa                    60       12
            America                    0        0
            Asia                      50        7
            Europe                    70        0
Male        Africa                    65       10
            America                    0        0
            Asia                      55        5
            Europe                    75        8
Transgender Africa                    40       11
            America                   80       10
            Asia                      30        6
            Europe                     0        0

关于python - 如何连接/加入这三个数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51327745/

相关文章:

python - DateTime属性时区

python - 如何根据系列值从 DataFrame 中删除行

pandas - 将 Pandas 或 Pyspark 数据框从 Databricks 保存到 SharePoint

python - 按一列或另一列对 pandas 数据框进行分组

python - 如何在不使用 One Hot 编码的情况下将行数据转换为列

python - 如果我想使用 python 进行 RPC 服务,thrift 还是 Pyro 更好?除了他们还有更好的选择吗?

python - 将两列中的数字组合起来创建一个数组

python - 键为数字时的字典顺序

python-3.x - 尝试导入 cv2(opencv-python) 包时出错

python - Tensorflow Executor 无法创建内核。未实现: Cast string to float is not supported