1

When i put my data to groupby function with the following code

x =x.groupby(['Time', 'Distance'],as_index=True,observed=False).size().reset_index() x.columns=['Time','Distance','Flow'] x.head(3) 

i get such output:

 Time Distance Flow 0 0 5 1 1 0 7 170 2 0 8 10 

However, i need to do some smoothing, thus i need the skipped values such as:

 Time Distance Flow 0 0 0 0 1 0 1 0 2 0 2 0 3 0 3 0 4 0 4 0 5 0 5 1 

etc. In short, i need also the missed grouping values. How can i do this?


1 Answer 1

2

Use:

x = pd.DataFrame({ 'Time':[0,1,1,1,1,0], 'Distance':[4,5,4,5,5,3], }) df = x.groupby(['Time', 'Distance'],as_index=True,observed=False).size() print (df) Time Distance 0 3 1 4 1 1 4 1 5 3 dtype: int64 df1 = df.unstack(fill_value=0).stack().reset_index(name='Flow') print (df1) Time Distance Flow 0 0 3 1 1 0 4 1 2 0 5 0 3 1 3 0 4 1 4 1 5 1 5 3 

Or:

m = pd.MultiIndex.from_product(df.index.levels, names=df.index.names) df1 = df.reindex(m, fill_value=0).reset_index(name='Flow') 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.