0

I am making a site that will allow you to tweet other's messages. Each message will have its tweets in a csv file. I will loop through the csv file, finding all the tweets for the given message and arranging them into a list.

The thing is, when I loop throught the data, the oldest tweets (the ones at the very start of the file) would appear first in the list. Is there a way to either loop through the pandas DataFrame from bottom to top, or when appending the tweets append them to the very start of the file? Here is my code:


current_message_name = 'Draft Message' # This is the name of the message the user is veiwing right now.

tweets = []
df = pd.read_csv('data/tweets.csv')

for index, row in df.iterrows():
    tweets.append({
    'poster':f'{row["poster"]}',
    'date':f'{row["date"]}',
    'message':f'{row["message"]}',
 })

2 Answers2

3

Try not to loop in pandas. (ref: Does pandas iterrows have performance issues?)

If you need a list of dictionary, use to_dict.

tweets = df.iloc[::-1][['poster', 'date', 'message']].to_dict(orient='records')
Emma
  • 8,518
  • 1
  • 18
  • 35
1

Heres how you can approach it:

current_message_name = 'Draft Message'
tweets = []
df = pd.read_csv('data/tweets.csv')

# Reversing the DataFrame
df = df.iloc[::-1]

for index, row in df.iterrows():
    tweets.append({
        'poster':f'{row["poster"]}',
        'date':f'{row["date"]}',
        'message':f'{row["message"]}',
    })
jagmitg
  • 4,236
  • 7
  • 25
  • 59