Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将多列映射为键的数据帧_Python_Pandas - Fatal编程技术网

Python 将多列映射为键的数据帧

Python 将多列映射为键的数据帧,python,pandas,Python,Pandas,如何使用2个属性作为键(SeqNum和StreamId)进行合并 我试着用 >>> merge OrderID Symbol Stream SeqNo Timestamp 0 5000000 AXBANK 3 1158 1490250116391063414 1 5000001 AXBANK 6 1733 NaN 2 5000002 AXBANK 6 1

如何使用2个属性作为键(SeqNum和StreamId)进行合并

我试着用

>>> merge
   OrderID    Symbol  Stream     SeqNo    Timestamp
0  5000000  AXBANK       3      1158      1490250116391063414
1  5000001  AXBANK       6      1733      NaN
2  5000002  AXBANK       6      1244      NaN
3  5000003  AXBANK       6      1388      NaN
4  5000004  AXBANK       3      1389      1490250116392819426
但是我需要包括这两个(SeqNum SeqNo和Stream StreamId)作为键 我知道如果我在两个数据帧中重命名相同的列名并使用merge,这会很容易,但我想避免这种情况。我更愿意使用一些通用的方法,如(使用此数据帧,将这些列映射到另一个数据帧中的那些列,并获取所需的coulmns)

我认为您需要:

另一个包含
rename
列的解决方案:

print (pd.merge(oss, p1, left_on=['Stream','SeqNo'], 
                         right_on=['StreamId','SeqNum'],how='left')
          .drop(['StreamId','SeqNum'], axis=1))

   OrderID  Symbol  Stream  SeqNo     Timestamp
0  5000000  AXBANK       3   1158  1.490250e+18
1  5000001  AXBANK       6   1733           NaN
2  5000002  AXBANK       6   1244           NaN
3  5000003  AXBANK       6   1388           NaN
4  5000004  AXBANK       3   1389  1.490250e+18

使用
join

d = {'Stream':'StreamId','SeqNo':'SeqNum'}
print (pd.merge(oss.rename(columns=d), p1, how='left'))
   OrderID  Symbol  StreamId  SeqNum     Timestamp
0  5000000  AXBANK         3    1158  1.490250e+18
1  5000001  AXBANK         6    1733           NaN
2  5000002  AXBANK         6    1244           NaN
3  5000003  AXBANK         6    1388           NaN
4  5000004  AXBANK         3    1389  1.490250e+18

有没有一种方法可以保持时间戳的原样而不在我认为只有一种方法-在转换之前转换为
str
-
p1.timestamp=p1.timestamp.astype(str)
。因为不可能将
int
值与
float
-
int
一起转换为
float
-请参阅
print (pd.merge(oss, p1, left_on=['Stream','SeqNo'], 
                         right_on=['StreamId','SeqNum'],how='left')
          .drop(['StreamId','SeqNum'], axis=1))

   OrderID  Symbol  Stream  SeqNo     Timestamp
0  5000000  AXBANK       3   1158  1.490250e+18
1  5000001  AXBANK       6   1733           NaN
2  5000002  AXBANK       6   1244           NaN
3  5000003  AXBANK       6   1388           NaN
4  5000004  AXBANK       3   1389  1.490250e+18
d = {'Stream':'StreamId','SeqNo':'SeqNum'}
print (pd.merge(oss.rename(columns=d), p1, how='left'))
   OrderID  Symbol  StreamId  SeqNum     Timestamp
0  5000000  AXBANK         3    1158  1.490250e+18
1  5000001  AXBANK         6    1733           NaN
2  5000002  AXBANK         6    1244           NaN
3  5000003  AXBANK         6    1388           NaN
4  5000004  AXBANK         3    1389  1.490250e+18
oss.join(p1.set_index(['StreamId', 'SeqNum']), on=['Stream', 'SeqNo'])

   OrderID  Symbol  Stream  SeqNo     Timestamp
0  5000000  AXBANK       3   1158  1.490250e+18
1  5000001  AXBANK       6   1733           NaN
2  5000002  AXBANK       6   1244           NaN
3  5000003  AXBANK       6   1388           NaN
4  5000004  AXBANK       3   1389  1.490250e+18