I have a dataframe and I want to get all rows grouped in id where after row with country = russia and month = march is followed by a line with country != russia
input dataframe:
import pandas as pd
import numpy as np
data = {'fruit': ['pear','pear','pear','banana', 'banana', 'banana', 'apricot', 'pear','watermelon','pear','banana', 'banana', 'banana','banana', 'melon', 'cherry','banana', 'kiwi', 'kiwi', 'kiwi'],
'country': ['france','france', 'france', 'russia', 'russia', 'russia','russia', 'france','russia','usa', 'russia', 'ghana','ghana','ghana', 'ghana', 'albania','andorra', 'russia', 'russia', 'russia'],
'id': ['01','01','01','01','01','01','02','02','03','03','011', '011', '011','011', '6', '6','6', '5', '5', '5'],
'id1': ['01','01','01','01','01','01','02','02','03','03','011', '011', '011','011', '6', '6','6', '5', '5', '5'],
'month': ['january','september','january','january','september','january','january', 'september','march','march', 'november', 'march', 'january','january', 'march', 'january','july', 'march', 'march', 'april']
}
df = pd.DataFrame(data, columns = ['fruit','country', 'id','id1', 'month'])
I made pivot table with pd.pivot_table(df, values='id', index=['fruit','country'], columns='id1', aggfunc='count') and i got many unuseless rows, where i have NaN or very small figures.
How to sort pivot table to get rows with figures no less than 3? Can anyone see the problem
I need to get this dataframe
data = {'fruit': ['banana', 'banana', 'kiwi','pear'],
'country': [ 'ghana','russia','russia','france'],
'01': [np.nan,3,np.nan,3],
'011': [3,1,np.nan,np.nan],
'5': [np.nan,np.nan,3,np.nan]
}
df = pd.DataFrame(data, columns = ['fruit','country', '01', '011','5'])


