熊猫:从元组生成器

时间:2015-11-10 19:26:34

标签: python pandas generator

有没有办法从元组生成器创建pd.Series? 我的代码如下所示,但我确信有更好的方法:

import numpy as np
import pandas as pd
g = ((n, s) for n, s in [("A", 1), ("B", 2), ("C", 3), ("D", 4), ("E", 5)])
arr = np.array(list(g))
ind, val = arr[:, 0], arr[:, 1]

pd.Series(val, index=ind)

2 个答案:

答案 0 :(得分:4)

以下是使用DataFrame构造函数的替代方法:

private class yourTestApp {
....
public static void getStringOrWhatEver() {
    try {
        URL url = new URL("http://localhost:8080/<nameOfYourService>/rest/message");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        conn.setRequestProperty("Accept", "application/xml");

        if (conn.getResponseCode() != 200) {
            throw new RuntimeException("Failed : HTTP error code : "
                    + conn.getResponseCode());
        }

        BufferedReader br = new BufferedReader(new InputStreamReader(
                (conn.getInputStream())));

        String output;
        System.out.println("...and the message is: ");
        while ((output = br.readLine()) != null) {
            System.out.println(output);
        }
        System.out.println(".... End of content\n");
        conn.disconnect();

    } catch (MalformedURLException ex) {
        System.err.println("MalformedURLException, " + ex);
    } catch (IOException ex) {
        System.err.println("IOException, " + ex);
    }
}
....
}

构造DataFrame后,我们设置索引列并通过选择第1列返回Series。

这避免了对任何临时列表的需要,因此可能更有效(我还没有测试过)。它还为每个列使用适当的dtypes(在本例中为int64),因此它可以避免首先创建对象数组。

答案 1 :(得分:3)

您只需创建两个列表

import numpy as np
import pandas as pd

val, ind = zip(*[(s, n) for n, s in [("A", 1), ("B", 2), ("C", 3), ("D", 4), ("E", 5)]])

print pd.Series(val, index=ind)

A    1
B    2
C    3
D    4
E    5
dtype: object