Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
129 views
in Technique[技术] by (71.8m points)

python - Getting the date from a comparison of values within two columns through time

I would like to spot dates (so rows) where the number of likes is smaller than retweets. My data looks like

Date               Text    Like     Retweet
28/02/2020         wow!!!   1          0
28/02/2020         I have a baby!!!   1          4
28/02/2020         No words  0          0
...
05/12/2019         I love cooking! 4    2
05/12/2020         Hello world!    1    1 
...

To find the numbers of likes/retweets per date I did as follows:

df.groupby([df.Date])["Like"].sum()
df.groupby([df.Date])["Retweet"].sum()

Now I would like to see when the number of likes is greater than that one of retweet (in the example should be 5/12/2020).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can filter:

grouped = df.groupby('Date')[['Like','Retweet']].sum()
grouped[grouped['Like'] > grouped['Retweet']].index

# similarly
# grouped.query('Like > Retweet').index

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
...