Pandas Groupby: Groupby conditional statementConverting a Pandas GroupBy object to DataFrameSelecting multiple columns in a pandas dataframeRenaming columns in pandasAdding new column to existing DataFrame in Python pandasDelete column from pandas DataFrame by column name“Large data” work flows using pandasHow to iterate over rows in a DataFrame in Pandas?Select rows from a DataFrame based on values in a column in pandasGet list from pandas DataFrame column headersgrouping rows in list in pandas groupby
What is the incentive for curl to release the library for free?
Will tsunami waves travel forever if there was no land?
Error message with tabularx
Do I have to worry about players making “bad” choices on level up?
Does this extra sentence in the description of the warlock's Eyes of the Rune Keeper eldritch invocation appear in any official reference?
Is DC-DC (24v to 12v) Buck Conversion typically more efficient than AC-DC (110v to 12v) conversion?
What is the strongest case that can be made in favour of the UK regaining some control over fishing policy after Brexit?
Any examples of headwear for races with animal ears?
Mac Pro install disk keeps ejecting itself
Sci-fi novel series with instant travel between planets through gates. A river runs through the gates
What is the difference between `command a[bc]d` and `command `ab,cd`
How can Republicans who favour free markets, consistently express anger when they don't like the outcome of that choice?
How do I use proper grammar in the negation of "have not" for the following sentence translation?
a sore throat vs a strep throat vs strep throat
Why don't other Westeros houses use wildfire?
Why do Computer Science majors learn Calculus?
Why was Germany not as successful as other Europeans in establishing overseas colonies?
What does KSP mean?
Do I have an "anti-research" personality?
Is contacting this expert in the field something acceptable or would it be discourteous?
What is the relationship between spectral sequences and obstruction theory?
How to verbalise code in Mathematica?
How much cash can I safely carry into the USA and avoid civil forfeiture?
how to sum variables from file in bash
Pandas Groupby: Groupby conditional statement
Converting a Pandas GroupBy object to DataFrameSelecting multiple columns in a pandas dataframeRenaming columns in pandasAdding new column to existing DataFrame in Python pandasDelete column from pandas DataFrame by column name“Large data” work flows using pandasHow to iterate over rows in a DataFrame in Pandas?Select rows from a DataFrame based on values in a column in pandasGet list from pandas DataFrame column headersgrouping rows in list in pandas groupby
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I am trying to identify the location of stops from gps data but need to account for some gps drift.
I have identified stops and isolated them into a new dataframe:
df['Stopped'] = (df.groupby('DAY')['LAT'].diff().abs() <= 0.0005) & (df.groupby('DAY')['LNG'].diff().abs() <= 0.0005)
df2 = df.loc[(df['Stopped'] == True)]
Now I can label groups that have the exact match in coordinates using:
df2['StoppedEvent'] = df2.groupby(['LAT','LNG']).ngroup()
But I want to group by the same conditions of Stopped. Something like this but that works:
df2['StoppedEvent'] = df2.groupby((['LAT','LNG']).diff().fillna(0).abs() <= 0.0005).ngroup()
pandas group-by pandas-groupby latitude-longitude
add a comment |
I am trying to identify the location of stops from gps data but need to account for some gps drift.
I have identified stops and isolated them into a new dataframe:
df['Stopped'] = (df.groupby('DAY')['LAT'].diff().abs() <= 0.0005) & (df.groupby('DAY')['LNG'].diff().abs() <= 0.0005)
df2 = df.loc[(df['Stopped'] == True)]
Now I can label groups that have the exact match in coordinates using:
df2['StoppedEvent'] = df2.groupby(['LAT','LNG']).ngroup()
But I want to group by the same conditions of Stopped. Something like this but that works:
df2['StoppedEvent'] = df2.groupby((['LAT','LNG']).diff().fillna(0).abs() <= 0.0005).ngroup()
pandas group-by pandas-groupby latitude-longitude
add a comment |
I am trying to identify the location of stops from gps data but need to account for some gps drift.
I have identified stops and isolated them into a new dataframe:
df['Stopped'] = (df.groupby('DAY')['LAT'].diff().abs() <= 0.0005) & (df.groupby('DAY')['LNG'].diff().abs() <= 0.0005)
df2 = df.loc[(df['Stopped'] == True)]
Now I can label groups that have the exact match in coordinates using:
df2['StoppedEvent'] = df2.groupby(['LAT','LNG']).ngroup()
But I want to group by the same conditions of Stopped. Something like this but that works:
df2['StoppedEvent'] = df2.groupby((['LAT','LNG']).diff().fillna(0).abs() <= 0.0005).ngroup()
pandas group-by pandas-groupby latitude-longitude
I am trying to identify the location of stops from gps data but need to account for some gps drift.
I have identified stops and isolated them into a new dataframe:
df['Stopped'] = (df.groupby('DAY')['LAT'].diff().abs() <= 0.0005) & (df.groupby('DAY')['LNG'].diff().abs() <= 0.0005)
df2 = df.loc[(df['Stopped'] == True)]
Now I can label groups that have the exact match in coordinates using:
df2['StoppedEvent'] = df2.groupby(['LAT','LNG']).ngroup()
But I want to group by the same conditions of Stopped. Something like this but that works:
df2['StoppedEvent'] = df2.groupby((['LAT','LNG']).diff().fillna(0).abs() <= 0.0005).ngroup()
pandas group-by pandas-groupby latitude-longitude
pandas group-by pandas-groupby latitude-longitude
asked Mar 22 at 18:39
acrowacrow
62
62
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
I would do something like the following:
df['Stopped'] = (df.groupby('DAY')['LAT'].diff().abs() <= 0.0005)
& (df.groupby('DAY')['LNG'].diff().abs() <= 0.0005)
df["Stopped_Group"] = (~df["Stopped"]).cumsum()
df2 = df.loc[df['Stopped']]
Now you'll have a column, "Stopped_Group"
, which is constant within a set of rows that are close to each other as determined by your logic. In the original dataframe, df
, this column won't have any meaning for rows that correspond to motion.
To get your desired output (if I understand you correctly), do something like the following:
df2["Stopped_Duration"] = df2.groupby("Stopped_Group").transform("size")
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55305938%2fpandas-groupby-groupby-conditional-statement%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I would do something like the following:
df['Stopped'] = (df.groupby('DAY')['LAT'].diff().abs() <= 0.0005)
& (df.groupby('DAY')['LNG'].diff().abs() <= 0.0005)
df["Stopped_Group"] = (~df["Stopped"]).cumsum()
df2 = df.loc[df['Stopped']]
Now you'll have a column, "Stopped_Group"
, which is constant within a set of rows that are close to each other as determined by your logic. In the original dataframe, df
, this column won't have any meaning for rows that correspond to motion.
To get your desired output (if I understand you correctly), do something like the following:
df2["Stopped_Duration"] = df2.groupby("Stopped_Group").transform("size")
add a comment |
I would do something like the following:
df['Stopped'] = (df.groupby('DAY')['LAT'].diff().abs() <= 0.0005)
& (df.groupby('DAY')['LNG'].diff().abs() <= 0.0005)
df["Stopped_Group"] = (~df["Stopped"]).cumsum()
df2 = df.loc[df['Stopped']]
Now you'll have a column, "Stopped_Group"
, which is constant within a set of rows that are close to each other as determined by your logic. In the original dataframe, df
, this column won't have any meaning for rows that correspond to motion.
To get your desired output (if I understand you correctly), do something like the following:
df2["Stopped_Duration"] = df2.groupby("Stopped_Group").transform("size")
add a comment |
I would do something like the following:
df['Stopped'] = (df.groupby('DAY')['LAT'].diff().abs() <= 0.0005)
& (df.groupby('DAY')['LNG'].diff().abs() <= 0.0005)
df["Stopped_Group"] = (~df["Stopped"]).cumsum()
df2 = df.loc[df['Stopped']]
Now you'll have a column, "Stopped_Group"
, which is constant within a set of rows that are close to each other as determined by your logic. In the original dataframe, df
, this column won't have any meaning for rows that correspond to motion.
To get your desired output (if I understand you correctly), do something like the following:
df2["Stopped_Duration"] = df2.groupby("Stopped_Group").transform("size")
I would do something like the following:
df['Stopped'] = (df.groupby('DAY')['LAT'].diff().abs() <= 0.0005)
& (df.groupby('DAY')['LNG'].diff().abs() <= 0.0005)
df["Stopped_Group"] = (~df["Stopped"]).cumsum()
df2 = df.loc[df['Stopped']]
Now you'll have a column, "Stopped_Group"
, which is constant within a set of rows that are close to each other as determined by your logic. In the original dataframe, df
, this column won't have any meaning for rows that correspond to motion.
To get your desired output (if I understand you correctly), do something like the following:
df2["Stopped_Duration"] = df2.groupby("Stopped_Group").transform("size")
answered Mar 22 at 18:48
PMendePMende
1,9101613
1,9101613
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55305938%2fpandas-groupby-groupby-conditional-statement%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown