熊猫数据框中的圆柱

时间:2015-07-06 13:56:05

标签: python pandas

我有以下pandas数据框

package com.example.pack;

import android.os.AsyncTask;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

public class VariabileLikeTask extends AsyncTask<URL, Void, CharSequence>
{
    TextView textView;

    public VariabileLikeTask(TextView textView)
    {
        this.textView = textView;
    }

    @Override
    protected String doInBackground(URL... urls)
    {
        URL url = urls[0];
        InputStream in = null;
        int risposta = -1;
        String text = "";

        try
        {
            URLConnection conn = url.openConnection();

            if (!(conn instanceof HttpURLConnection))
              throw new IOException("No connection");

            HttpURLConnection httpConn = (HttpURLConnection) conn;
            httpConn.setAllowUserInteraction(false);
            httpConn.setInstanceFollowRedirects(true);
            httpConn.setRequestMethod("GET");
            httpConn.connect();

            risposta = httpConn.getResponseCode();

            if (risposta == HttpURLConnection.HTTP_OK)
                in = httpConn.getInputStream();

            BufferedReader bf = new BufferedReader(new InputStreamReader(in));
            text = bf.readLine();

            in.close();
            bf.close();

        }
        catch (Exception ex) {}

        return text;
    }

    @Override
    protected void onPostExecute(CharSequence text)
    {
        textView.setText(text);
    }
}

我想用pandas围绕Y和X列。 我怎么能这样做?

3 个答案:

答案 0 :(得分:8)

您可以apply round

In [142]:
df[['Y','X']].apply(pd.Series.round)

Out[142]:
    Y  X
0  36 -3
1  36 -3
2  35 -3
3  35 -4
4  36 -4
5  36 -3
6  36 -3

如果您想申请特定数量的地方:

In [143]:
df[['Y','X']].apply(lambda x: pd.Series.round(x, 3))

Out[143]:
        Y      X
0  35.973 -2.735
1  35.592 -2.904
2  35.330 -3.391
3  35.393 -3.929
4  35.579 -3.943
5  35.520 -3.409
6  35.759 -3.079

修改 您可以将以上内容分配给要修改的列,如下所示:

In [144]:
df[['Y','X']] = df[['Y','X']].apply(lambda x: pd.Series.round(x, 3))
df

Out[144]:
        Y      X  id WP_NER
0  35.973 -2.735   1  WP_01
1  35.592 -2.904   2  WP_02
2  35.330 -3.391   3  WP_03
3  35.393 -3.929   4  WP_04
4  35.579 -3.943   5  WP_05
5  35.520 -3.409   6  WP_06
6  35.759 -3.079   7  WP_07

答案 1 :(得分:8)

您现在可以在数据框上使用round

选项1

In [661]: df.round({'Y': 2, 'X': 2})
Out[661]:
       Y     X  id WP_NER
0  35.97 -2.73   1  WP_01
1  35.59 -2.90   2  WP_02
2  35.33 -3.39   3  WP_03
3  35.39 -3.93   4  WP_04
4  35.58 -3.94   5  WP_05
5  35.52 -3.41   6  WP_06
6  35.76 -3.08   7  WP_07

选项2

In [662]: cols = ['Y', 'X']

In [663]: df[cols] = df[cols].round(2)

In [664]: df
Out[664]:
       Y     X  id WP_NER
0  35.97 -2.73   1  WP_01
1  35.59 -2.90   2  WP_02
2  35.33 -3.39   3  WP_03
3  35.39 -3.93   4  WP_04
4  35.58 -3.94   5  WP_05
5  35.52 -3.41   6  WP_06
6  35.76 -3.08   7  WP_07

答案 2 :(得分:0)

Round 非常聪明,它只适用于浮动列,所以最简单的解决方案是:

df = df.round(2)