1

How do i select column "id" within my CSV and add a string to it?

Column ID is a number like "5474548384". I would like to select this column and alter it to add the following: 'https://twitter.com/twitter/statuses/' to create a link. Screenshot of CSV is at the bottom of this post

Any suggestions on how to alter this column in my CSV?

import requests
import expansions
import os
import json
import pandas as pd
import csv
import sys
import time


bearer_token = "removed for privacy "

search_url = "https://api.twitter.com/2/tweets/search/all"

query_params = {'query': '("covid") i -is:retweet -is:verified -baby -lotion -shampoo lang:en has:geo place_country:US',
                'tweet.fields':'created_at,lang,text,geo,author_id,id,public_metrics,referenced_tweets',
                'expansions':'geo.place_id,author_id', 
                'place.fields':'contained_within,country,country_code,full_name,geo,id,name,place_type',
                'user.fields':'description,username,id',
                'start_time':'2021-02-27T00:00:01.000Z',
                'end_time':'2021-02-27T23:30:00.000Z',
                'max_results':'10'}

def create_headers(bearer_token):
    headers = {"Authorization": "Bearer {}".format(bearer_token)}
    return headers


def connect_to_endpoint(url, headers, params):
    response = requests.request("GET", search_url, headers=headers, params=params)

    if response.status_code != 200:
        raise Exception(response.status_code, response.text)
    return response.json()


def main():
    headers = create_headers(bearer_token)
    json_response = connect_to_endpoint(search_url, headers, query_params)

    json_response = expansions.flatten(json_response) 
    df = pd.json_normalize(json_response['data'])
    drop = df['author.description'].str.contains('news')
    
    result = df[-drop]
    result.to_csv("mydata.csv", encoding="utf-8-sig")
    
if __name__ == "__main__":
    main()

enter image description here

1 Answer 1

2

After getting the dataframe, you can add the prefix like this:

prefix = 'https://twitter.com/twitter/statuses/'
df['id'] = prefix + df['id'].astype(str)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.