如何根据其他pandas数据框

时间:2018-05-24 08:19:38

标签: python pandas dataframe series

我想基于其他pandas数据框更新数据,因为我只是收集更好的数据,这里是数据集的例子

继承我的数据

No  Country    Language    Capital
1   Indonesia  Bahasa      Jakarta
2   Malaysia   Bahasa      Kuala Lumpur
3   Brunei     Bahasa      Bandar Seri Begawan
4   Nigeria    English     Abuja

我的第二个数据框

Nation     Capital City
Malaysia   Lagos
Nigeria    Putrajaya

我想要的是

No  Country    Language    Capital
1   Indonesia  Bahasa      Jakarta
2   Malaysia   Bahasa      Putrajaya
3   Brunei     Bahasa      Bandar Seri Begawan
4   Nigeria    English     Lagos

2 个答案:

答案 0 :(得分:3)

首先从新映射创建一个系列:

s = df2.set_index('Nation')['Capital City']

然后使用pd.Series.update就地更新您的系列:

df1['Capital'].update(df1['Country'].map(s))

答案 1 :(得分:1)

您可以将新的Seriesmap一起使用,该Nation为不存在的NaN s s = df2.set_index('Nation')['Capital City'] df1['Capital'] = df1['Country'].map(s).fillna(df1['Capital']) print (df1) No Country Language Capital 0 1 Indonesia Bahasa Jakarta 1 2 Malaysia Bahasa Lagos 2 3 Brunei Bahasa Bandar Seri Begawan 3 4 Nigeria English Putrajaya 创建,并由fillna替换:

df1['Capital'] = df1['Country'].replace(s)

使用replace的更慢的解决方案:

 


$(function() {
      const json = [
  {
    "name": "Park Hotel",
    "address1": "327 Cranbrook Road",
    "rating": 2,
    "lng": 0.065,
    "lat": 51.568,
    "pictures": [
      {
        "url": "https://aobtravel.s3.amazonaws.com/hotelpictures/003/623/222/LON-91B-1.jpg?1387213390",
        "description": "Exterior"
      },
      {
        "url": "https://aobtravel.s3.amazonaws.com/hotelpictures/003/623/225/LON-91B-2.jpg?1387213390",
        "description": "Lobby"
      },
      {
        "url": "https://aobtravel.s3.amazonaws.com/hotelpictures/003/623/227/LON-91B-3.jpg?1387213390",
        "description": "Lobby"
      },
      {
        "url": "https://aobtravel.s3.amazonaws.com/hotelpictures/003/623/230/LON-91B-4.jpg?1387213391",
        "description": "Lobby"
      },
      {
        "url": "https://aobtravel.s3.amazonaws.com/hotelpictures/003/623/232/LON-91B-5.jpg?1387213391",
        "description": "Guest Room"
      }
    ],
    "sales_price": 2847
  }];

      const token = '.....';
     
      mapboxgl.accessToken = token;

      let map = new mapboxgl.Map({
        container: 'map-canvas',
        style: 'mapbox://styles/mapbox/streets-v9',
        center: [0.065, 51.568],
        zoom: 3
      });

      map.addControl(new mapboxgl.NavigationControl());
      $.each(json, function(i, item) {
        let myLatlng = new mapboxgl.LngLat(json[i].lng, json[i].lat);
        let marker = new mapboxgl.Marker()
          .setLngLat(myLatlng)
          .setPopup(new mapboxgl.Popup({
              offset: 25
            })
            .setHTML('<h3 class="hotel_name">' + json[i].name + '</h3><p class="adress-text-hotel">' + json[i].address1 + '</p>' + '</h3><p class="rating-title">Reting: ' + json[i].rating + '</p>' + '</h3><p class="price-text-hotel" >Price ' + json[i].sales_price + '</p>'))
          .addTo(map);


      });
    });