Python子集基于变量名称的数据框

时间:2017-04-22 13:48:22

标签: python-3.x pandas subset

您好我的国家/地区市场中的所有库存数据都有一个数据框,数据看起来像这样

Ticker  Date/Time   Open    High    Low Close   Volume
AAA     7/15/2010   19.581  20.347  18.429  18.698  174100.0
BBB     7/16/2010   19.002  19.002  17.855  17.855  109200.0
BBB     7/19/2010   19.002  19.002  17.777  17.777  104900.0
CCC     7/20/2010   18.429  18.429  17.084  17.354  328700.0
CCC     7/21/2010   17.354  17.431  16.895  17.316  75800.0

列Ticker具有库存名称,每行是一个特定日期的数据。 我想编写一个循环代码,创建变量名称的变量是股票名称,变量是整个数据框的子集,包含该股票的数据。

例如,

当我调用变量BBB时,我会得到这个数据帧:

BBB

Ticker  Date/Time   Open    High    Low Close   Volume
BBB     7/16/2010   19.002  19.002  17.855  17.855  109200.0
BBB     7/19/2010   19.002  19.002  17.777  17.777  104900.0

请问您如何编写此代码

1 个答案:

答案 0 :(得分:1)

您可以dictionary创建DataFrames dict comprehensiondfs = {idx:x for idx, x in df.groupby('Ticker')} print (dfs) {'BBB': Ticker Date/Time Open High Low Close Volume 1 BBB 7/16/2010 19.002 19.002 17.855 17.855 109200.0 2 BBB 7/19/2010 19.002 19.002 17.777 17.777 104900.0, 'CCC': Ticker Date/Time Open High Low Close Volume 3 CCC 7/20/2010 18.429 18.429 17.084 17.354 328700.0 4 CCC 7/21/2010 17.354 17.431 16.895 17.316 75800.0, 'AAA': Ticker Date/Time Open High Low Close Volume 0 AAA 7/15/2010 19.581 20.347 18.429 18.698 174100.0} print (dfs['BBB']) Ticker Date/Time Open High Low Close Volume 1 BBB 7/16/2010 19.002 19.002 17.855 17.855 109200.0 2 BBB 7/19/2010 19.002 19.002 17.777 17.777 104900.0 groupbydfs = {x:df[df['Ticker'] == x] for x in df['Ticker'].unique()} print (dfs['BBB']) Ticker Date/Time Open High Low Close Volume 1 BBB 7/16/2010 19.002 19.002 17.855 17.855 109200.0 2 BBB 7/19/2010 19.002 19.002 17.777 17.777 104900.0 代码为<{1}}:

dfs = dict(list(df.groupby("Ticker")))

另一种解决方案:

 include('config.php');

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    // Get username and password from the form as variables
    $username = $_POST['username'];
    $firstname = $_POST['firstname'];
    $lastname = $_POST['lastname'];
    $passwords = $_POST['passwords'];
    $email = $_POST['email'];
    $birthdate = $_POST['birthdate'];

    // Query records that have usernames and passwords that match those in the customers table

    $sql = file_get_contents('sql/insertUsers.sql');
    $params = array(
        'username' => $username,
        'firstname' => $firstname,
        'lastname' => $lastname,
        'passwords' => $passwords,
        'email' => $email,
        'birthdate' => $birthdate,
    );
    $statement = $database->prepare($sql);
    $statement->execute($params);
    $users = $statement->fetchAll(PDO::FETCH_ASSOC);

    if(!empty($users)) {
        // Set $user equal to the first result of $users
        $user = $users[0];

        // Set a session variable with a key of customerID equal to the customerID returned
        $_SESSION['userid'] = $user['userid'];

        // Redirect to the index.php file
        header('location: index.php');
    }
}

编辑:

感谢DSM提出了很好的建议:

CREATE TABLE users_final (
    userid INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    username VARCHAR(50) UNIQUE,
    firstname CHAR(50),
    lastname CHAR(50),
    passwords VARCHAR(50),
    email VARCHAR(100) UNIQUE,
    birthdate DATE
);