【问题标题】:Merge two dataframes on string column - compound string column合并字符串列上的两个数据框 - 复合字符串列
【发布时间】:2018-11-22 23:13:47
【问题描述】:

我正在尝试合并两个具有以下结构的巨大数据帧(4+ 百万):

数据框 A:

     date    Fruit        a    b    c    d
     01      "apple"      0    3    5    1
     03      "apple"      8    2    7    2
     02      "banana"     1    4    3    5
     04      "banana"     3    5    2    6
     03      "pineapple"  2    6    4    6
     05      "pineapple"  3    5    7    9

数据框 B:

     date   Fruits                         x    y    z 
     01     "apple, pear, strawberry"      a    n    q 
     02     "banana, apple, coconut"       b    m    p 
     03     "pineapple, pear, banana"      c    s    o
     04     "banana, apple, coconut"       d    f    v 
     05     "pineapple, pear, banana"      r    ñ    t  

我想要实现的是具有以下结构的第三个数据框:

数据框 C:

     date   Fruit        a    b    c    d    x    y    z
     01     "apple"      0    3    5    1    a    n    q
     03     "apple"      0    3    5    1    0    0    0
     02     "banana"     1    4    3    5    b    m    p
     04     "banana"     1    4    3    5    d    f    v
     03     "pineapple"  2    6    4    6    c    s    o
     05     "pineapple"  2    6    4    6    r    ñ    t
      ...

我已经尝试过类似的方法:

test = market_test.assetCode.apply(lambda x : news_test.assetCodes.str.find(x)>=0)

但我的内核坏了,我也尝试使用 for 循环将 B 数据框的水果列扩展为“fruit-b”列,保留来自其他 B 的数据列,然后在日期列和'fruit-B'列之间合并,但执行时间太长。

有没有办法使用数据帧AB获取数据帧C,并且不会消耗大量时间和内存? p>

FruitFruits 列的类型是字符串。

【问题讨论】:

  • df_A 和 df_B 中出现的独特水果的总数是多少?您可以将它们转换为 one-hot 或 Categorical,而不是存储为字符串。
  • df_B.Fruits 是一个复合列。我将重命名此“在字符串列/复合字符串列上合并两个数据框”
  • @smci,感谢您的回复,唯一水果编号应该在 5000 左右,我也会按照您的建议重命名问题。

标签: python pandas dataframe merge


【解决方案1】:

用途:

print (df_A)

   date      Fruit  a  b  c  d
0     1      apple  0  3  5  1
1     3      apple  8  2  7  2
2     2     banana  1  4  3  5
3     4     banana  3  5  2  6
4     3  pineapple  2  6  4  6
5     5  pineapple  3  5  7  9

print (df_B)

   date                   Fruits  x  y  z
0     1  apple, pear, strawberry  a  n  q
1     2   banana, apple, coconut  b  m  p
2     3  pineapple, pear, banana  c  s  o
3     4   banana, apple, coconut  d  f  v
4     5  pineapple, pear, banana  r  ñ  t

import pandas as pd
import numpy as np

# Split the strings into list.
df_B.Fruits = df_B.Fruits.str.split(', ')

# reindex and repeat on length of list
temp = df_B.reindex(df_B.index.repeat(df_B.Fruits.str.len())).drop('Fruits',1)

temp['Fruit'] = np.concatenate(df_B.Fruits.values)

df_C = df_A.merge(temp, on=['date','Fruit'], how='left').fillna(0)

print (df_C)

   date      Fruit  a  b  c  d  x  y  z
0     1      apple  0  3  5  1  a  n  q
1     3      apple  8  2  7  2  0  0  0
2     2     banana  1  4  3  5  b  m  p
3     4     banana  3  5  2  6  d  f  v
4     3  pineapple  2  6  4  6  c  s  o
5     5  pineapple  3  5  7  9  r  ñ  t

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-11
    • 1970-01-01
    • 2018-12-18
    • 2021-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-26
    相关资源
    最近更新 更多