How can I delete rows for a particular Date in a Pandas dataframe?

时间:2016-02-12 21:29:55

标签: python pandas

I've got a Pandas DataFrame using Date as an index. How can I drop all rows that have the date "2000-01-06"?

Sample code:

$this->add(array( 
    'name' => 'score_1',
    'type' => 'Number',
    'options' => array(
        'label' => 'PPV',
     ),
));

Example DataFrame:

import numpy as np
import pandas as pd

dates = pd.date_range('1/1/2000', periods=8)
df = pd.DataFrame(np.random.randn(8, 3), index=dates, columns=['A', 'B', 'C'])
df.index.name = 'Date'

2 个答案:

答案 0 :(得分:9)

You can pass a datetime to drop to drop that row:

// pass in the full plist file contents
function parse_plist($plist) {
    $result = false;
    $depth = [];
    $key = false;

    $lines = explode("\n", $plist);
    foreach ($lines as $line) {
        $line = trim($line);
        if ($line) {
            if ($line == '<dict>') {
                if ($result) {
                    if ($key) {
                        // adding a new dictionary, the line above this one should've had the key
                        $depth[count($depth) - 1][$key] = [];
                        $depth[] =& $depth[count($depth) - 1][$key];
                        $key = false;
                    } else {
                        // adding a dictionary to an array
                        $depth[] = [];
                    }
                } else {
                    // starting the first dictionary which doesn't have a key
                    $result = [];
                    $depth[] =& $result;
                }

            } else if ($line == '</dict>' || $line == '</array>') {
                array_pop($depth);

            } else if ($line == '<array>') {
                $depth[] = [];

            } else if (preg_match('/^\<key\>(.+)\<\/key\>\<.+\>(.+)\<\/.+\>$/', $line, $matches)) {
                // <key>Major Version</key><integer>1</integer>
                $depth[count($depth) - 1][$matches[1]] = $matches[2];

            } else if (preg_match('/^\<key\>(.+)\<\/key\>\<(true|false)\/\>$/', $line, $matches)) {
                // <key>Show Content Ratings</key><true/>
                $depth[count($depth) - 1][$matches[1]] = ($matches[2] == 'true' ? 1 : 0);

            } else if (preg_match('/^\<key\>(.+)\<\/key\>$/', $line, $matches)) {
                // <key>1917</key>
                $key = $matches[1];
            }
        }
    }
    return $result;
}

答案 1 :(得分:1)

您也可以删除值列表,例如:

date_list = [datetime(2009, 5, 2),
             datetime(2010, 8, 22),
             datetime(2010, 9, 19),
             datetime(2011, 6, 19),
             datetime(2011, 7, 17),
             datetime(2015, 5, 23),
             datetime(2016, 2, 20)]
df = df.drop(date_list)

请注意,通过在drop参数中放入inplace = True,您不必定义新对象,但是它是在同一个对象上完成的

df.drop(date_list, inplace=True)
相关问题