我正在创建一个计数器列,该计数器列仅在(i-1)的值与(i)的值不同时才增加,但在user_id更改时会重置。
输入:
func foo(bar: Any) {}
var faz = foo
foo(1) // Missing argument label 'bar:' in call
foo(bar: 1)
faz(1)
faz(bar: 1) // Extraneous argument label 'bar:' in call
下面是我的代码:
@GET
@Path("get-oauth-google-drive/{email}/{token}")
@Produces(MediaType.TEXT_PLAIN)
public String getOauthGoogleDrive(
@PathParam("email") String email,
@PathParam("token") String token
) {
String linkForOauth = "https://accounts.google.com/o/oauth2/v2/auth?response_type=code&client_id=" + client_id + "&redirect_uri=" + domain + "/rest/myresource/callback&scope=https://www.googleapis.com/auth/drive.appdata&access_type=offline";
return linkForOauth;
}
@POST
@Consumes({MediaType.APPLICATION_JSON})
@Path("granted-callback")
@Produces(MediaType.TEXT_PLAIN)
public String callbackGranted(
String msg
) {
System.out.println("Drive Access GRANTED");
return "";
}
@GET
@Path("callback")
@Produces(MediaType.TEXT_PLAIN)
public String callbackOauth(
@QueryParam("code") String code,
@QueryParam("scope") String scope
) {
try
{
HttpClient httpclient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost("https://www.googleapis.com/oauth2/v4/token");
httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded");
InputStream inputStream = getResourceAsStream(CREDENTIALS_FILE_PATH);
GoogleClientSecrets googleClientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(inputStream));
String clientId = googleClientSecrets.getDetails().getClientId();
String clientSecret = googleClientSecrets.getDetails().getClientSecret();
String redirectURI = domain + "/rest/myresource/granted-callback";
List<NameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair("code", code));
params.add(new BasicNameValuePair("client_id", clientId));
params.add(new BasicNameValuePair("client_secret", clientSecret));
params.add(new BasicNameValuePair("redirect_uri", URLEncoder.encode(redirectURI, "UTF-8")));
params.add(new BasicNameValuePair("grant_type", "authorization_code"));
httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
HttpResponse response = httpclient.execute(httpPost);
System.out.println(response);
}
catch (Exception ex)
{
ex.printStackTrace();
}
return "Merci de votre inscription vous allez être redirigé incessamment sous peu.";
}
下面是所需的输出:
user_id sc_id
1 100
1 100
1 101
1 102
2 100
2 101
3 101
3 103
答案 0 :(得分:2)
我们可以将groupby
与cumcount
一起使用,在此之前重复复制,因此user_id
和sc_id
都得到相同的subcat_counter
。之后,我们fillna
和forwardfill (ffill)
:
df['subcat_counter'] = df.drop_duplicates(['user_id', 'sc_id'])\
.groupby(['user_id'])['sc_id']\
.cumcount()+1
df.fillna(method='ffill', inplace=True)
print(df)
user_id sc_id subcat_counter
0 1 100 1.0
1 1 100 1.0
2 1 101 2.0
3 1 102 3.0
4 2 100 1.0
5 2 101 2.0
6 3 101 1.0
7 3 103 2.0
答案 1 :(得分:0)
您可以使用groupby
df['subcat_counter'] = (df.groupby('user_id')['sc_id']
.transform(lambda x: x.diff().gt(0).cumsum() + 1))
输出:
user_id sc_id subcat_counter
0 1 100 1
1 1 100 1
2 1 101 2
3 1 102 3
4 2 100 1
5 2 101 2
6 3 101 1
7 3 103 2