我有一个数据框,使用Data = pandas.read_csv
数据框的其中一列是日期,例如'14/09/2015'
,数据类型为str
。
我需要创建一个子集,我使用它:NewDataFrame = DataFrame['DatesColumn'][DataFrame['DatesColumn']==desired date]
但我有两个主要问题:
KeyError : -1L
我尝试使用此代码选择2014:
NewDataFrame = DataFrame['DatesColumn'][DataFrame['DatesColumn'][-1]==4]
for
循环来转换数据,我会收到错误: TypeError: 'float' object has no attribute '__getitem__'
问:我如何按年对数据进行子集化(或清理)?
非常感谢。
答案 0 :(得分:1)
对于# to fill NaNs with zeros
noNans = withNans.fillna(0)
值,您可以使用fillna()
。
read_csv()
关于日期问题,
而不是自己处理日期字符串,你应该已经
现有的库为您处理它们。在这种情况下1,14/09/2016,dataa
1,14/09/2015,dataa
2,14/10/2014,dataa2
函数
能为你做到。
请参阅文档
here
这是一个小例子:
Csv文件:
import pandas as pd
from datetime import date
df = pd.read_csv("test.csv", header=None, parse_dates=[1])
df[df[1] > date.today()]
代码:
0 1 2
0 1 2016-09-14 dataa
仅打印
try {
$pdo = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbusername, $dbpassword);
} catch (PDOException $e){
exit('Datebase error.');
}
// db login info is already defined, just didnt post it here
$username = $_GET["user"];
$password = $_GET["passwd"];
//$data = mysqli_query($mysqli, "SELECT * FROM users WHERE username='".$username."'");
//$hash = mysqli_fetch_object($data);
$query = "SELECT username, password, loginreqkey, banned FROM users WHERE username='$username'";
//if (password_verify('rasmuslerdorf', $hash)) {
if ($stmt = $pdo->prepare($query)) {
$stmt->execute(array($username, $password, $loginreqkey, $banned));
//$stmt->bind_result($username, $password, $loginreqkey, $gbanned);
// $result = $stmt->fetch(PDO::FETCH_LAZY);
//$dt = $stmt->fetchAll() ;
//$query->execute(array($username, $password));
if (password_verify($password, $result['password'])) {
while($r = $stmt->fetchAll(PDO::FETCH_ASSOC)){
echo "{";
echo '"state": "success",';
echo '"loginreqkey": "' . $r['loginreqkey'] . '",';
echo '"banstatus": "' . $r['banned'] . '"';
echo "}";
}
/* close statement */
$stmt = null;
} else {
die("fake pw lol");
}
/* close connection */
$pdo = null;
}
//}