基于第二个Dataframe订购Dataframe Index

时间:2018-05-27 14:57:22

标签: python pandas dataframe kaggle

我在Python中有两个DataFrame,但是那些用作索引(CodeNumber)的列的顺序不一样。需要平等地订购它们;遵循代码:

 public void onMapReady(GoogleMap googleMap) {
    // 구글 맵 객체를 불러온다.
    mMap = googleMap; //메인 마커

    // 초기 위치 설정
    LatLng seoul = new LatLng(37.56, 126.97);
    MarkerOptions makerOptions = new MarkerOptions();
    makerOptions.position(seoul)
            .title("서울")
            .snippet("포토존")
            .icon(BitmapDescriptorFactory.fromResource(R.drawable.point));

    // 마커를 생성한다.
    mMap.addMarker(makerOptions);

    mMap.moveCamera(CameraUpdateFactory.newLatLng(seoul)); //카메라 위치 이동
    mMap.animateCamera(CameraUpdateFactory.zoomTo(10)); //카메라줌

    mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
        @Override
        public boolean onMarkerClick(Marker marker) {
            //Do what ever you need here
            //Note:  the return value depends on how you wish to handler the clicks on the Marker
            // See Google Docs for more information
            return false;
        }
    });

}

然后我将CodeNumber设置为索引:

#generating DataFrames:
d3 = {'CodeNumber': [1234, 1235, 111, 101], 'Date': [20150808, 20141201, 20180119, 20120720], 'Weight': [26, 32, 41, 24]}
d4 = {'CodeNumber': [1235, 1234, 101, 111], 'Date': [20160808, 20151201, 20180219, 20130720], 'Weight': [28, 25, 47, 3]}

data_SKU3 = pd.DataFrame(data=d3)
data_SKU4 = pd.DataFrame(data=d4)

如果我们打印生成的DataFrames,请注意data_SKU3具有以下代码序列号:1234 1235 111 101,而data_SKU4:1235 1234 101 111

有没有办法订购代码编号,以便两个DataFrames的顺序相同?

2 个答案:

答案 0 :(得分:1)

您还可以在设置为CodeNumber之前调用.sort_values(by = 'CodeNumber'),在每个数据框架上按index对值进行排序:

d3 = {'CodeNumber': [1234, 1235, 111, 101], 'Date': [20150808, 20141201, 20180119, 20120720], 'Weight': [26, 32, 41, 24]}
d4 = {'CodeNumber': [1235, 1234, 101, 111], 'Date': [20160808, 20151201, 20180219, 20130720], 'Weight': [28, 25, 47, 3]}

data_SKU3 = pd.DataFrame(data=d3).sort_values(by = 'CodeNumber')
data_SKU4 = pd.DataFrame(data=d4).sort_values(by = 'CodeNumber')

data_SKU3.set_index('CodeNumber', inplace = True)
data_SKU4.set_index('CodeNumber', inplace = True)

答案 1 :(得分:0)

如果indices

中的值相同,请使用sort_index
data_SKU3 = data_SKU3.set_index('CodeNumber').sort_index()
data_SKU4 = data_SKU4.set_index('CodeNumber').sort_index()

print (data_SKU3)
                Date  Weight
CodeNumber                  
101         20120720      24
111         20180119      41
1234        20150808      26
1235        20141201      32

print (data_SKU4)
                Date  Weight
CodeNumber                  
101         20180219      47
111         20130720       3
1234        20151201      25
1235        20160808      28

另一种方法是使用reindex另一个index值,但这是必要的唯一值,只有差异是不同的顺序:

data_SKU3 = data_SKU3.set_index('CodeNumber')
data_SKU4 = data_SKU4.set_index('CodeNumber').reindex(index=data_SKU3.index)
print (data_SKU3)
                Date  Weight
CodeNumber                  
1234        20150808      26
1235        20141201      32
111         20180119      41
101         20120720      24

print (data_SKU4)
                Date  Weight
CodeNumber                  
1234        20151201      25
1235        20160808      28
111         20130720       3
101         20180219      47