返回多列Pandas中的相应值

时间:2016-04-27 06:59:01

标签: python pandas

我一直试图以干净的方式解决这个问题,但在这项任务上却一直没有成功。

直言不讳......我有2个数据帧:

df1
     anID   siID   daID
0       A      0      0
1       D      0      0
2       E      0      0
3       F      0      0
4       G      A      D
5       H      E      D
6       B      0      0
7       I      E      M
8       L      F      0
9       M      B      H
10      N      A      D

df2
   map_id  renum_id
0       A         1
1       C         2
2       B         3
3       E         4
4       D         5
5       F         6
6       Q         7
7       V         8
8       H         9
9       G        10
10      N        11

基本上,df1是带有识别码的数据框,df2$renum_id具有替换现有代码的新代码,df2$map_id是指导{{1}重新编码的地图}列。

现在我需要一种干净,轻快/快速的方式来获取df1 renum_id中所有3列的相应map_id值(使用df1作为键) ,anIDsiID)。基本上,我希望它为每个字母提供相应的renum_ID,并在df1中将daID(零)保留为零。

所以,我需要这样的0

df3

这可能是非常简单的事情,但我一直在尝试使用 anID siID daID anID/ siID/ daID/ 0 A G 0 1 10 0 1 D E A 5 4 1 2 E 0 0 4 0 0 3 F B D 6 3 5 4 G A D 10 1 5 5 H E D 9 4 5 6 B 0 0 2 0 0 7 N A D 11 1 5 而无法想出解决问题的方法。

数据帧有大约数十万行,这就是为什么我坚持需要一个" light"解决问题的方法。

提前感谢大家。

1 个答案:

答案 0 :(得分:1)

我认为您可以先dictionary创建df2,然后df1 dictionary创建d DataFrame df3 df1 d = df2.set_index('map_id').to_dict() print d['renum_id'] {'A': 1, 'C': 2, 'B': 3, 'E': 4, 'D': 5, 'G': 10, 'F': 6, 'H': 9, 'N': 11, 'Q': 7, 'V': 8} df3 = df1.replace(d['renum_id']) df3.columns = [col + '/' for col in df3.columns] print df3 anID/ siID/ daID/ 0 1 0 0 1 5 0 0 2 4 0 0 3 6 0 0 4 10 1 5 5 9 4 5 6 3 0 0 7 I 4 M 8 L 6 0 9 M 3 9 10 11 1 5 print pd.concat([df1, df3], axis=1) anID siID daID anID/ siID/ daID/ 0 A 0 0 1 0 0 1 D 0 0 5 0 0 2 E 0 0 4 0 0 3 F 0 0 6 0 0 4 G A D 10 1 5 5 H E D 9 4 5 6 B 0 0 3 0 0 7 I E M I 4 M 8 L F 0 L 6 0 9 M B H M 3 9 10 N A D 11 1 5 }。按列列出的最新to_dicte.preventDefault() $(function(){ $('body').on('click','.leka-button',function(e){ e.preventDefault(); $.ajax({ type: 'POST', url: 'http://mail.ayaami.com/freebook_10x10.php', crossDomain: true, data: {email:$(this).parent().find('input').val()}, success: function(responseData, textStatus, jqXHR) { alert("Thank you for joining Ayaami mailing list."); window.open("http://dev.ayaami.com/site/free-book-success"); // var value = responseData.someKey; // console.log(' log d: '+responseData); // console.log(' log d: '+textStatus); // console.log(' log d: '+jqXHR); // var obj = jQuery.parseJSON(responseData); // hide the email box // text_respond="<p>"+obj.result+"</p>"; // set cookies var cookieName = 'ayaami_newsletter_lightbox'; var cookieValue = 'true'; var myDate = new Date(); myDate.setMonth(myDate.getMonth() + (500 + 12)); document.cookie = cookieName + "=" + cookieValue + ";expires=" + myDate + ";domain=.ayaami.com;path=/"; }, error: function(responseData, textStatus, errorThrown) { alert('You already subscribed in the mailing list.'); console.log('log e:' + responseData); console.log('log e:' + textStatus); console.log('log e:' + errorThrown); } }); }); }); <button type="button">SIGN UP</button>

Intent intent = new Intent("custom-event-name");
  // You can also include some extra data.
  intent.putExtra("message", "This is my message!");
  LocalBroadcastManager.getInstance(this).sendBroadcast(intent);