I want to create a new data frame with sort Rank cur.
, while Rank prev.
equal NaN
The point is to get All Current Rank where it didn't have Previous rank, while sorting current rank
Dataframe as following:
Searchterm | Rank cur. | Rank prev. | Change | Volume |
---|---|---|---|---|
Word1 | 5 | 29 | ↑ -28 | 20 |
Word2 | 3 | NaN | ↑ -5 | 500 |
Word3 | 2 | 9 | ↑ -6 | 35 |
Word4 | 4 | NaN | ↑ -4 | 40 |
Word5 | 1 | NaN | ↑ -6 | 210 |
My approach:
Where dfMerged
is the original df
*Note that the code works, but it get Volume value set to NaN (I would like to keep it-"at
same or at extra step")
dfTopNewcomer = dfMerged[dfMerged['Rank prev.'].isnull()]
dfTopNewcomer = dfTopNewcomer.drop(['Rank prev.'], 1)
dfTopNewcomer = dfTopNewcomer.sort_values(by=['Rank cur.'], ascending=True)
dfTopNewcomer
The output i need should be:
Searchterm Rank cur. Rank prev. Change Volume
Word5 1 NaN ↑ -6 210
Word3 2 NaN ↑ -6 35
Word1 5 NaN ↑ -28 20
CodePudding user response:
df = dfMerged[dfMerged['Rank prev.'].isnull()].sort_values(by=['Rank cur.'])
Is this what you are looking for?
CodePudding user response:
The only way I can figure to get your desired output is by shifting the Rank prev.
column...
dfTopNewcomer = dfMerged[dfMerged['Rank prev.'].shift(1).isnull()]
dfTopNewcomer = dfTopNewcomer.drop(['Rank prev.'], axis=1)
dfTopNewcomer = dfTopNewcomer.sort_values(by=['Rank cur.'], ascending=True)
print(dfTopNewcomer)
Output:
Searchterm Rank cur. Change Volume
4 Word5 1 ↑ -6 210
2 Word3 2 ↑ -6 35
0 Word1 5 ↑ -28 20