Dataframe.applymap运行正常 - 列消失了

时间:2018-03-19 23:32:57

标签: python

以下是测试数据框

public class UserControllerServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

    private UserDao userDao;
    private ReservationDao reservationDao;
    private DBConnection dbConnection;


    @Override
    public void init() throws ServletException {
        super.init();

        try {
            userDao = new UserDao(dbConnection);
            reservationDao = new ReservationDao(dbConnection);
        }catch(Exception e) {
            throw new ServletException(e);
        }
    }




    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
    {

        try {
            // read the "command" parameter
            String theCommand = request.getParameter("command");
            String theCommand2 = request.getParameter("command2");
            if(theCommand == null) {
                theCommand = "LIST";
            }

            if(theCommand2 == null) {
                theCommand2 = "LIST";
            }

            switch(theCommand2) {
            case "LIST":
                listReservations(request, response);
                return;

            case "ADD":
                addReservation(request, response);
                return;


            case "LOAD":
                loadReservation(request, response);
                return;


            case "UPDATE":
                updateReservation(request, response);
                return;



            case "DELETE":
                deleteReservation(request, response);
                return;


            default:
                listReservations(request, response);
        }

            //route to the appropriate method
            switch(theCommand) {
                case "LIST":
                    listUsers(request, response);
                    break;

                case "ADD":
                    addUser(request, response);
                    break;


                case "LOAD":
                    loadUser(request, response);
                    break;


                case "UPDATE":
                    updateUser(request, response);
                    break;



                case "DELETE":
                    deleteUser(request, response);
                    break;


                default:
                    listUsers(request, response);
            }

        }catch(Exception e) {
            throw new ServletException(e);
        }

    }

    private void deleteUser(HttpServletRequest request, HttpServletResponse response) 
    throws Exception {
        String theUserId = request.getParameter("userId");

        userDao.deleteUser(theUserId);

        listUsers(request, response);
    }

    private void updateUser(HttpServletRequest request, HttpServletResponse response) 
            throws Exception {

        int id = Integer.parseInt(request.getParameter("userId"));
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String role = request.getParameter("role");
        String nume = request.getParameter("nume");
        String prenume = request.getParameter("prenume");
        String email = request.getParameter("email");
        String adresa = request.getParameter("adresa");
        UserBean theUser = new UserBean(id, username, password, role,nume,prenume,email,adresa);
        userDao.updateUser(theUser);
        listUsers(request, response);
    }

    private void loadUser(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String theUserId = request.getParameter("userId");
        UserBean theUser = userDao.getUser(theUserId);
        request.setAttribute("THE_USER", theUser);
        RequestDispatcher dispatcher = 
                request.getRequestDispatcher("/update-user-form.jsp");
        dispatcher.forward(request, response);
        return;
    }

    private void addUser(HttpServletRequest request, HttpServletResponse response) throws Exception {

        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String role = request.getParameter("role");
        String nume = request.getParameter("nume");
        String prenume = request.getParameter("prenume");
        String email = request.getParameter("email");
        String adresa = request.getParameter("adresa");
        UserBean theUser = new UserBean(username, password, role,nume,prenume,email,adresa);
        userDao.addUser(theUser);
        listUsers(request, response);

    }

    private void listUsers(HttpServletRequest request, HttpServletResponse response) 
    throws Exception {
        List<UserBean> users = userDao.getUsers();
        request.setAttribute("USER_LIST", users);
        RequestDispatcher dispatcher = request.getRequestDispatcher("/list-users.jsp");
        dispatcher.forward(request, response);
        return;
    }


    //Reservations Methods

    private void deleteReservation(HttpServletRequest request, HttpServletResponse response) 
            throws Exception {
                String theReservationId = request.getParameter("reservationId");
                reservationDao.deleteReservation(theReservationId);
                listReservations(request, response);
            }

            private void updateReservation(HttpServletRequest request, HttpServletResponse response) 
                    throws Exception {

                int idReservation = Integer.parseInt(request.getParameter("reservationId"));
                int idUser = Integer.parseInt(request.getParameter("userId"));
                String dataCheckin = request.getParameter("dataCheckin");
                String dataCheckout = request.getParameter("dataCheckout");
                int nrPersoane = Integer.parseInt(request.getParameter("nrPersoane"));
                int nrCamere = Integer.parseInt(request.getParameter("nrCamere"));
                ReservationBean theReservation = new ReservationBean(idReservation, idUser, dataCheckin, dataCheckout,nrPersoane,nrCamere);
                reservationDao.updateReservation(theReservation);
                listReservations(request, response);
            }

            private void loadReservation(HttpServletRequest request, HttpServletResponse response) throws Exception {
                String theReservationId = request.getParameter("reservationId");
                ReservationBean theReservation = reservationDao.getReservation(theReservationId);
                request.setAttribute("THE_RESERVATION", theReservation);
                RequestDispatcher dispatcher = 
                        request.getRequestDispatcher("/update-reservation-form.jsp");
                dispatcher.forward(request, response);
                return;
            }

            private void addReservation(HttpServletRequest request, HttpServletResponse response) throws Exception {

                int userId = Integer.parseInt(request.getParameter("userId"));
                String dataCheckin = request.getParameter("dataCheckin");
                String dataCheckout = request.getParameter("dataCheckout");
                int nrPersoane = Integer.parseInt(request.getParameter("nrPersoane"));
                int nrCamere = Integer.parseInt(request.getParameter("nrCamere"));
                ReservationBean theReservation = new ReservationBean(userId, dataCheckin, dataCheckout,nrPersoane,nrCamere);
                reservationDao.addReservation(theReservation);
                listReservations(request, response);

            }

            private void listReservations(HttpServletRequest request, HttpServletResponse response) 
                throws Exception {
                    List<ReservationBean> reservations = reservationDao.getReservations();
                    request.setAttribute("RESERVATION_LIST", reservations);
                     request.getRequestDispatcher("/list-users.jsp").forward(request, response);
                    return;
                }


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}

我想更改列b和d,以便元素是整数,但不是列e。

In [32]: frame = pd.DataFrame(np.random.randn(4, 3)*1000000, columns=list('bde'), index=['Utah', 'Ohio', 'Texas', 'Oregon'])

In [33]: frame
Out[33]: 
                   b              d             e
Utah    1.582808e+05 -351731.845560 -5.832029e+04
Ohio   -1.653296e+06 -336185.349586 -1.170889e+05
Texas  -4.741239e+04 -964691.055175 -9.489544e+05
Oregon -1.103707e+06  523821.598282 -1.245662e+06

该帧更改但列消失了。我怎样才能把它拿回最后一栏?

2 个答案:

答案 0 :(得分:2)

您可以使用assign

frame.assign(b = frame.b.astype(int), d = frame.d.astype(int))

输出:

             b       d             e
Utah    524658 -965098  2.762532e+04
Ohio   -980245 -629015  1.042148e+06
Texas  -180861  -60601 -5.128917e+05
Oregon -752839  469190 -5.036541e+05

答案 1 :(得分:0)

答案就是

In [36]: frame[['b','d']] = frame[['b','d']].applymap(int)

In [37]: frame
Out[37]: 
              b       d             e
Utah     158280 -351731 -5.832029e+04
Ohio   -1653296 -336185 -1.170889e+05
Texas    -47412 -964691 -9.489544e+05
Oregon -1103707  523821 -1.245662e+06