使用Python Pandas中的groupby将一列的每个值与其他列的每个值分组

时间:2018-03-21 13:08:00

标签: python pandas pandas-groupby

所以,我有一个包含3列的数据帧,每列有631行,所以我只突出显示每列下的唯一值。

df

Segment Type  Nature of Query     Q1

PRIME         Request             1           
BUSINESS      Complaint           2 
PRIORITY      Critical Request    3
                                  4
                                  5

现在,让我们在'细分类型'我想分组' PRIME'每一行的“自然的自然”和#39;和' Q1'并找到大小,最小值,最大值,平均值

所以尝试使用groupby func,我得到了这个:

 df.groupby(['Segment Type','Nature of Query'])['Q1'].agg([pd.np.size, 
 pd.np.min, pd.np.max, pd.np.mean])

而且,我得到了这个:

    Segment Type    Nature of Query    size     amin    amax    mean            

         BUSINESS       Request          1        4       4     4.000000
           PRIME        Complaint        1        5       5     5.000000
                      Critical Request   3        1       2     1.666667
                        Request          31       1       5     3.387097
          PRIORITY    Critical Request   1        4       4     4.000000
                        Request          3        3       5     4.000000

我想要的输出:

       Segment Type   Nature of Query      size     amin    amax    mean
           BUSINESS       Request            1        4       4     4.000000
                          Complaint          1        5       5     5.000000
                          Critical Request   3        1       2     1.666667


            PRIME       Complaint            1        5       5     5.000000
                        Critical Request     3        1       2     1.666667
                        Request              31       1       5     3.387097

          PRIORITY      Complaint            1        5       5     5.000000
                        Critical Request     1        4       4     4.000000
                        Request              3        3       5     4.000000

忽略Q1的大小,平均值,最大值等。我的主要问题是'细分类型'和查询的性质'。

如果有任何解决方案,请告诉我。 谢谢!

2 个答案:

答案 0 :(得分:0)

我认为需要reindex创建MultiIndex.from_product

// Countries
var country_arr = new Array("Afghanistan", "Albania", "Algeria", "American Samoa", "Angola", "Anguilla", "Antartica", "Antigua and Barbuda", "Argentina", "Armenia", "Aruba", "Ashmore and Cartier Island", "Australia", "Austria", "Azerbaijan", "Bahamas", "Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium", "Belize", "Benin", "Bermuda", "Bhutan", "Bolivia", "Bosnia and Herzegovina", "Botswana", "Brazil", "British Virgin Islands", "Brunei", "Bulgaria", "Burkina Faso", "Burma", "Burundi", "Cambodia", "Cameroon", "Canada", "Cape Verde", "Cayman Islands", "Central African Republic", "Chad", "Chile", "China", "Christmas Island", "Clipperton Island", "Cocos (Keeling) Islands", "Colombia", "Comoros", "Congo, Democratic Republic of the", "Congo, Republic of the", "Cook Islands", "Costa Rica", "Cote d'Ivoire", "Croatia", "Cuba", "Cyprus", "Czeck Republic", "Denmark", "Djibouti", "Dominica", "Dominican Republic", "Ecuador", "Egypt", "El Salvador", "Equatorial Guinea", "Eritrea", "Estonia", "Ethiopia", "Europa Island", "Falkland Islands (Islas Malvinas)", "Faroe Islands", "Fiji", "Finland", "France", "French Guiana", "French Polynesia", "French Southern and Antarctic Lands", "Gabon", "Gambia, The", "Gaza Strip", "Georgia", "Germany", "Ghana", "Gibraltar", "Glorioso Islands", "Greece", "Greenland", "Grenada", "Guadeloupe", "Guam", "Guatemala", "Guernsey", "Guinea", "Guinea-Bissau", "Guyana", "Haiti", "Heard Island and McDonald Islands", "Holy See (Vatican City)", "Honduras", "Hong Kong", "Howland Island", "Hungary", "Iceland", "India", "Indonesia", "Iran", "Iraq", "Ireland", "Ireland, Northern", "Israel", "Italy", "Jamaica", "Jan Mayen", "Japan", "Jarvis Island", "Jersey", "Johnston Atoll", "Jordan", "Juan de Nova Island", "Kazakhstan", "Kenya", "Kiribati", "Korea, North", "Korea, South", "Kuwait", "Kyrgyzstan", "Laos", "Latvia", "Lebanon", "Lesotho", "Liberia", "Libya", "Liechtenstein", "Lithuania", "Luxembourg", "Macau", "Macedonia, Former Yugoslav Republic of", "Madagascar", "Malawi", "Malaysia", "Maldives", "Mali", "Malta", "Man, Isle of", "Marshall Islands", "Martinique", "Mauritania", "Mauritius", "Mayotte", "Mexico", "Micronesia, Federated States of", "Midway Islands", "Moldova", "Monaco", "Mongolia", "Montserrat", "Morocco", "Mozambique", "Namibia", "Nauru", "Nepal", "Netherlands", "Netherlands Antilles", "New Caledonia", "New Zealand", "Nicaragua", "Niger", "Nigeria", "Niue", "Norfolk Island", "Northern Mariana Islands", "Norway", "Oman", "Pakistan", "Palau", "Panama", "Papua New Guinea", "Paraguay", "Peru", "Philippines", "Pitcaim Islands", "Poland", "Portugal", "Puerto Rico", "Qatar", "Reunion", "Romainia", "Russia", "Rwanda", "Saint Helena", "Saint Kitts and Nevis", "Saint Lucia", "Saint Pierre and Miquelon", "Saint Vincent and the Grenadines", "Samoa", "San Marino", "Sao Tome and Principe", "Saudi Arabia", "Scotland", "Senegal", "Seychelles", "Sierra Leone", "Singapore", "Slovakia", "Slovenia", "Solomon Islands", "Somalia", "South Africa", "South Georgia and South Sandwich Islands", "Spain", "Spratly Islands", "Sri Lanka", "Sudan", "Suriname", "Svalbard", "Swaziland", "Sweden", "Switzerland", "Syria", "Taiwan", "Tajikistan", "Tanzania", "Thailand", "Tobago", "Toga", "Tokelau", "Tonga", "Trinidad", "Tunisia", "Turkey", "Turkmenistan", "Tuvalu", "Uganda", "Ukraine", "United Arab Emirates", "United Kingdom", "Uruguay", "USA", "Uzbekistan", "Vanuatu", "Venezuela", "Vietnam", "Virgin Islands", "Wales", "Wallis and Futuna", "West Bank", "Western Sahara", "Yemen", "Yugoslavia", "Zambia", "Zimbabwe");

//country codes
var c_a = new Array();
c_a[0] = "";
c_a[1] = "AFG";
c_a[2] = "ALB";
c_a[3] = "DZA";
c_a[4] = "ASM";
c_a[5] = "AGO";
c_a[6] = "AIA";
c_a[7] = "ATA";
c_a[8] = "ATG";
c_a[9] = "ARG";
c_a[10] = "ARM";
c_a[11] = "ABW";
c_a[12] = "AUS";
c_a[13] = "AUS";
c_a[14] = "AUT";
c_a[15] = "AZE";
c_a[16] = "BHS";
c_a[17] = "BHR";
c_a[18] = "BGD";
c_a[19] = "BRB";
c_a[20] = "BLR";
c_a[21] = "BEL";
c_a[22] = "BLZ";
c_a[23] = "BEN";
c_a[24] = "BMU";
c_a[25] = "BTN";
c_a[26] = "BOL";
c_a[27] = "BIH";
c_a[28] = "BWA";
c_a[29] = "BRA";
c_a[30] = "VGB";
c_a[31] = "BRN";
c_a[32] = "BGR";
c_a[33] = "BFA";
c_a[34] = "BUR";
c_a[35] = "BDI";
c_a[36] = "KHM";
c_a[37] = "CMR";
c_a[38] = "CAN";
c_a[39] = "CPV";
c_a[40] = "CYM";
c_a[41] = "CAF";
c_a[42] = "TCD";
c_a[43] = "CHL";
c_a[44] = "CHN";
c_a[45] = "CXR";
c_a[46] = "IP";
c_a[47] = "CCK";
c_a[48] = "COL";
// <!-- -->
c_a[49] = "COM";
c_a[50] = "COD";
c_a[51] = "COG";
c_a[52] = "COK";
c_a[53] = "CRI";
c_a[54] = "CIV";
c_a[55] = "HRV";
c_a[56] = "CUB";
c_a[57] = "CYP";
c_a[58] = "CZE";
c_a[59] = "DNK";
c_a[60] = "DJI";
c_a[61] = "DMA";
c_a[62] = "DOM";
// <!-- -->
c_a[63] = "ECU";
c_a[64] = "EGY";
c_a[65] = "SLV";
c_a[66] = "GNQ";
c_a[67] = "ERI";
c_a[68] = "EST";
c_a[69] = "ETH";
c_a[70] = "EU";
c_a[71] = "FLK";
c_a[72] = "FRO";
c_a[73] = "FJI";
c_a[74] = "FIN";
c_a[75] = "FRA";
c_a[76] = "GUF";
c_a[77] = "PYF";
c_a[78] = "ATF";
c_a[79] = "GAB";
c_a[80] = "GMB";
c_a[81] = "GZ";
c_a[82] = "GEO";
c_a[83] = "DEU";
c_a[84] = "GHA";
c_a[85] = "GIB";
c_a[86] = "GO";
c_a[87] = "GRC";
c_a[88] = "GRL";
c_a[89] = "GRD";
c_a[90] = "GLP";
c_a[91] = "GUM";
c_a[92] = "GTM";
c_a[93] = "GGY";
c_a[94] = "GIN";
c_a[95] = "GNB";
c_a[96] = "GUY";
c_a[97] = "HTI";
c_a[98] = "HMD";
c_a[99] = "VAT";
c_a[100] = "HND";
c_a[101] = "HKG";
c_a[102] = "HQ";
c_a[103] = "HUN";
c_a[104] = "ISL";
c_a[105] = "IND";
c_a[106] = "IDN";
c_a[107] = "IRN";
c_a[108] = "IRQ";
c_a[109] = "IRL";
c_a[110] = "NIR";
c_a[111] = "ISR";
c_a[112] = "ITA";
c_a[113] = "JAM";
c_a[114] = "SJM";
c_a[115] = "JPN";
c_a[116] = "DQ";
c_a[117] = "JEY";
c_a[118] = "JQ";
c_a[119] = "JOR";
c_a[120] = "JU";
c_a[121] = "KAZ";
c_a[122] = "KEN";
c_a[123] = "KIR";
c_a[124] = "PRK";
c_a[125] = "KOR";
c_a[126] = "KWT";
c_a[127] = "KGZ";
c_a[128] = "LAO";
c_a[129] = "LVA";
c_a[130] = "LBN";
c_a[131] = "LSO";
c_a[132] = "LBR";
c_a[133] = "LBY";
c_a[134] = "LIE";
c_a[135] = "LTU";
c_a[136] = "LUX";
c_a[137] = "MAC";
c_a[138] = "MKD";
c_a[139] = "MDG";
c_a[140] = "MWI";
c_a[141] = "MYS";
c_a[142] = "MDV";
c_a[143] = "MLI";
c_a[144] = "MLT";
c_a[145] = "IMN";
c_a[146] = "MHL";
c_a[147] = "MTQ";
c_a[148] = "MRT";
c_a[149] = "MUS";
c_a[150] = "MYT";
c_a[151] = "MEX";
c_a[152] = "FSM";
c_a[153] = "MQ";
c_a[154] = "MDA";
c_a[155] = "MCO";
c_a[156] = "MNG";
c_a[157] = "MSR";
c_a[158] = "MAR";
c_a[159] = "MOZ";
c_a[160] = "NAM";
c_a[161] = "NRU";
c_a[162] = "NPL";
c_a[163] = "NLD";
c_a[164] = "ANT";
c_a[165] = "NCL";
c_a[166] = "NZL";
c_a[167] = "NIC";
c_a[168] = "NER";
c_a[169] = "NGA";
c_a[170] = "NIU";
c_a[171] = "NFK";
c_a[172] = "MNP";
c_a[173] = "NOR";
c_a[174] = "OMN";
c_a[175] = "PAK";
c_a[176] = "PLW";
c_a[177] = "PAN";
c_a[178] = "PNG";
c_a[179] = "PRY";
c_a[180] = "PRY";
c_a[181] = "PHL";
c_a[182] = "PCN";
c_a[183] = "POL";
c_a[184] = "PRT";
c_a[185] = "PRI";
c_a[186] = "QAT";
c_a[187] = "REU";
c_a[188] = "ROU";
c_a[189] = "RUS";
c_a[190] = "RWA";
c_a[191] = "SHN";
c_a[192] = "KNA";
c_a[193] = "LCA";
c_a[194] = "SPM";
c_a[195] = "VCT";
c_a[196] = "ASM";
c_a[197] = "SMR";
c_a[198] = "STP";
c_a[199] = "SAU";
c_a[200] = "SCT";
c_a[201] = "SEN";
c_a[202] = "SYC";
c_a[203] = "SLE";
c_a[204] = "SGP";
c_a[205] = "SVK";
c_a[206] = "SVN";
c_a[207] = "SLB";
c_a[208] = "SOM";
c_a[209] = "ZAF";
c_a[210] = "SGS";
c_a[211] = "ESP";
c_a[212] = "PG";
c_a[213] = "LKA";
c_a[214] = "SDN";
c_a[215] = "SUR";
c_a[216] = "SJM";
c_a[217] = "SWZ";
c_a[218] = "SWE";
c_a[219] = "CHE";
c_a[220] = "SYR";
c_a[221] = "TWN";
c_a[222] = "TJK";
c_a[223] = "TZA";
c_a[224] = "THA";
c_a[225] = "TTO";
c_a[226] = "TGO";
c_a[227] = "TKL";
c_a[228] = "TON";
c_a[229] = "TTO";
c_a[230] = "TUN";
c_a[231] = "TUR";
c_a[232] = "TKM";
c_a[233] = "TUV";
c_a[234] = "UGA";
c_a[235] = "UKR";
c_a[236] = "ARE";
c_a[237] = "GBR";
c_a[238] = "URY";
c_a[239] = "USA";
c_a[240] = "UZB";
c_a[241] = "VUT";
c_a[242] = "VEN";
c_a[243] = "VNM";
c_a[244] = "VGB";
c_a[245] = "WAL";
c_a[246] = "WLF";
c_a[247] = "WE";
c_a[248] = "ESH";
c_a[249] = "YEM";
c_a[250] = "SCG";
c_a[251] = "ZMB";
c_a[252] = "ZWE";

function populateCountryCode(countryElementId, countrycodeElementId) {

  var selectedCountryIndex = document.getElementById(countryElementId).selectedIndex;

  var countryCodeElement = document.getElementById(countrycodeElementId);

  countryCodeElement.length = 0;
  countryCodeElement.options[0] = new Option(countryCodeElement);
  countryCodeElement.selectedIndex = 0;

  var countrycode_arr = c_a[selectedCountryIndex];

  for (var i = 0; i < countrycode_arr.length; i++) {
    countryCodeElement.options[countryCodeElement.length] = new Option(countrycode_arr[i]);
  }
}

function populateCountryCodes(countryElementId, countrtycodeElementId) {
  // given the id of the <select> tag as function argument, it inserts <option> tags
  var countryElement = document.getElementById(countryElementId);
  countryElement.length = 0;
  countryElement.options[0] = new Option('Select Country', '-1');
  countryElement.selectedIndex = 0;
  for (var i = 0; i < country_arr.length; i++) {
    countryElement.options[countryElement.length] = new Option(country_arr[i], country_arr[i]);
  }

  // Assigned all countries. Now assign event listener for the states.

  if (countrtycodeElementId) {
    countryElement.onchange = function() {
      populateCountryCode(countryElementId, countrtycodeElementId);
    };
  }
}

答案 1 :(得分:0)

您可以使用数据透视表功能,请参阅此处的教程:

http://pbpython.com/pandas-pivot-table-explained.html