I can find the matching row with this but how do I get the row number of the df?
y = df.loc [ df [ 'month' ] == df3 [ 'month' ] ]
I need y to be the row count.
I can get the index value, but the index for df is also a date.
Thanks.
One method is reindexing since you want the index to be integers rather than dates.
y = df.loc[df['month'] == df3['month' ]].reset_index().index
If you don't want to reset the index you can simply add a column with a row count:
import pandas as pd
df = pd.DataFrame({'month':[1,1,1,1,2,2,2]})
df.insert(0, 'row_num', range(0,len(df))) # here you insert the row count
df3 = pd.DataFrame({'month':[2,2,2,1,2,2,2]})
y = df.loc [ df [ 'month' ] == df3 [ 'month' ] ]
The content of the df
>>> df
row_num month
0 0 1
1 1 1
2 2 1
3 3 1
4 4 2
5 5 2
6 6 2
Result
>>> y['row_num']
3 3
4 4
5 5
6 6
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With