我想创建一个列,其中包含给定小时内发生的所有金额的总和。例如,如果我要查看的行的第0列下有0,我希望该行的“交易量”列为该小时内发生的所有金额的总交易量。
所以:
dat.groupby('Hours')['Amount'].sum()
通过按小时分组并求和,我得到了每小时的总交易额。
Hours
0 257101.87
1 146105.69
2 108819.17
....
45 532181.83
46 448887.69
47 336343.60
Name: Amount, dtype: float64
问题是我的数据库包含1000行,我不能简单地用groupby中的值创建一个新列,我需要一个条件,规定如果hour列上的值是0,则返回所有值的总和小时为0的金额。
所以期望的结果将是这样
Hours Amount Total
0 20 100
0 20 100
0 60 100
1 10 20
1 10 20
2 50 50
在这种情况下,我想创建合计列并返回给定小时内发生的所有金额的总和
答案 0 :(得分:2)
Groupby +转换应该做到
df["Total"] = df.groupby("Hours")["Amount"].transform(sum)
答案 1 :(得分:0)
我将使用var data2 = {
UserPoolId: 'xxxxxxxx',
ClientId: 'xxxxxxxx',
};
var userPool = new AmazonCognitoIdentity.CognitoUserPool(data2);
var cognitoUser = userPool.getCurrentUser();
var userData = {
Username : 'xxxxxxxx',
Pool : userPool
};
var authenticationData = {
Username : 'xxxxxxxx',
Password : 'xxxxxxxx',
};
var authenticationDetails = new AmazonCognitoIdentity.AuthenticationDetails(authenticationData);
var cognitoUser2 = new AmazonCognitoIdentity.CognitoUser(userData);
cognitoUser2.authenticateUser(authenticationDetails, {
onSuccess: function (result) {
var accessToken = result.getAccessToken().getJwtToken(); //user sign-in success and you can get accessToken and IdToken
//POTENTIAL: Region needs to be set if not already set previously elsewhere.
AWS.config.region = 'xxxxxxxx';
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
IdentityPoolId : 'xxxxxxxx', // your identity pool id here
Logins : {
// Change the key below according to the specific region your user pool is in.
// Here you need to map your idToken with your user pool domain for Cognito Identity Pool to generate a Identity with credential.
'cognito-idp.xxxxxxxx.amazonaws.com/xxxxxxxx' : result.getIdToken().getJwtToken()
}
});
//refreshes credentials using AWS.CognitoIdentity.getCredentialsForIdentity()
AWS.config.credentials.refresh((error) => {
if (error) {
console.error(error);
} else {
console.log('Successfully logged!');
// Instantiate aws sdk service objects now that the credentials have been updated. So put your DynamoDB client here
var docClient = new AWS.DynamoDB.DocumentClient({ region: AWS.config.region });
var params = {
ExpressionAttributeValues: {
":v1": {
N: 123456788
}
},
KeyConditionExpression: "userId = :v1",
TableName: "user"
};
docClient.query(params, function(err, data2) {
if (err) {
console.error(err);
}else{
console.log(data2);
}
});
}
});
},
onFailure: function(err) {
alert(err.message || JSON.stringify(err));
},
});
的输出,并将其与dat.groupby('Hours')['Amount'].sum()
列上的原始设置一起使用merge
:
Hours