通过分隔符熊猫将列拆分为未知的列数

时间:2018-04-27 20:14:36

标签: python pandas dataframe data-science

我正在尝试根据逗号/空格分隔将列拆分为多个列。

我的数据框目前看起来像

    Item                                          Colors
0   ID-1                                          Red, Blue, Green
1   ID-2                                          Red, Blue
2   ID-3                                          Blue, Green
3   ID-4                                          Blue
4   ID-5                                          Red

我想将'Colors'列转换为红色,蓝色和绿色,如下所示:

    Item                                           Red  Blue  Green
0   ID-1                                           1    1     1
1   ID-2                                           1    1     0
2   ID-3                                           0    1     1
3   ID-4                                           0    1     0
4   ID-5                                           1    0     1

我真的不知道该怎么做。 任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:8)

您可以使用get_dummies

HttpHost httpHostTag = getMMCHost(prop);
System.out.println(httpHostTag);
//@SuppressWarnings("deprecation")
DefaultHttpClient httpclient = (DefaultHttpClient) verifiedClient(new DefaultHttpClient());
HttpGet httpostTag = verifiedGet(urlTagUpdate, Mmc_user, Mmc_password);


HttpResponse response = httpclient.execute(httpHostTag, httpostTag);

System.out.println(response.getParams());
int respCode = response.getStatusLine().getStatusCode();

System.out.println("response Code for Tag Update API Call  : " + respCode);
String Code = Integer.toString(respCode);
HttpEntity entity = response.getEntity();



if (entity != null) {
  InputStream instream = entity.getContent();
  result = convertStreamToString(instream);
  //resultMsg = result.split(",")[0].split(":")[1];
  resultMsg = result.split(",")[0].split("msg")[1];
  System.out.println(resultMsg);
  System.out.println("RESPONSE: " + result);
  instream.close();
}



if (Code.equals("200") || Code.equals("202") || Code.equals("204")) {
  System.out.println(resultMsg);

  //				return "RESPONSE : " + resultMsg;
} else {
  return "Error : " + result;
}

} catch (Exception e) {
  System.out.println("error in code ");
  e.printStackTrace();
}

System.out.println("Tag Update to setenv.sh .................. This will take around 10 sec ");
try {

  Thread.sleep(10000);
} catch (InterruptedException e) {
 
  Thread.currentThread().interrupt();
  // you probably want to quit if the thread is interrupted

}





return null;

}