Help

How to create records - in Python, iterating over a pandas dataframe to send data to a table - processing error 422

Topic Labels: Automations
1136 0
cancel
Showing results for 
Search instead for 
Did you mean: 
Reinaldo_Chaves
4 - Data Explorer
4 - Data Explorer

In python and pandas I have the following dataframe:

tweet_text.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 11 entries, 0 to 10
Data columns (total 8 columns):
 #   Column       Non-Null Count  Dtype 
---  ------       --------------  ----- 
 0   tweet_id     11 non-null     object
 1   created_at   11 non-null     object
 2   source       11 non-null     object
 3   geo          0 non-null      object
 4   coordinates  0 non-null      object
 5   entities     11 non-null     object
 6   user         11 non-null     object
 7   text         11 non-null     object
dtypes: object(8)
memory usage: 832.0+ bytes

I then made this routine to iterate through the dataframe and include data in a table in the Airtable, which have the same columns


AIRTABLE_BASE_ID = ""
AIRTABLE_TABLE_NAME = ""
AIRTABLE_API_KEY = ""
ENDPOINT = f'https://api.airtable.com/v0/{AIRTABLE_BASE_ID}/{AIRTABLE_TABLE_NAME}'
headers = {
    "Authorization": f"Bearer {AIRTABLE_API_KEY}",
    "Content-Type": "application/json"
}

def add_to_airtable(tweet_id, created_at, source, geo, coordinates, entities, user, text):
    data = {
        "records": [
            {
                "fields": {
                    "tweet_id": tweet_id,
                    "created_at": created_at,
                    "source": source,
                    "geo": geo,
                    "coordinates": coordinates,
                    "entities": entities,
                    "user": user,
                    "text": text
                    }
                }
            ]
        }
    
    data = json.dumps(data, indent=4, sort_keys=True, default=str)
    
    r = requests.post(ENDPOINT, json=data, headers=headers)
    return r.status_code 

for num, row in tweet_text.iterrows():
    tweet_id = row['tweet_id']
    created_at = row['created_at']
    source = row['source']
    geo = row['geo']
    coordinates = row['coordinates']
    entities = row['entities']
    user = row['user']
    text = row['text']
    
    add_to_airtable(tweet_id, created_at, source, geo, coordinates, entities, user, text)

The script runs without errors. But it doesn’t add anything to the Airtable table
Please does anyone know what could be wrong?
The returned r.status_code is being 422

Below is an example of the date (the JSON) that I send

{
    "records": [
        {
            "fields": {
                "coordinates": null,
                "created_at": "2022-10-25",
                "entities": {
                    "hashtags": [],
                    "symbols": [],
                    "urls": [
                        {
                            "display_url": "twitter.com/i/web/status/1\u2026",
                            "expanded_url": "https://twitter.com/i/web/status/1585051831402909696",
                            "indices": [
                                116,
                                139
                            ],
                            "url": "https://t.co/b78PGeNpEK"
                        }
                    ],
                    "user_mentions": []
                },
                "geo": null,
                "source": "Twitter for iPhone",
                "text": "queria que essa vagabunda dessa ex \u201cjornalista\u201d da record que faz propaganda pro bolsonaro tomasse um t*ro na boca\u2026 https://t.co/b78PGeNpEK",
                "tweet_id": "1585051831402909696",
                "user": "User(_api=<tweepy.api.API object at 0x7f8a5249d120>, _json={'id': 197990491, 'id_str': '197990491', 'name': 'irm\u00e3 vape', 'screen_name': 'matheusisonline', 'location': 's\u00e3o paulo', 'description': 'tonelzinho de plut\u00f4nio', 'url': 'https://t.co/MDSDCeet1R', 'entities': {'url': {'urls': [{'url': 'https://t.co/MDSDCeet1R', 'expanded_url': 'http://instagram.com/matheusisthecowboy', 'display_url': 'instagram.com/matheusistheco\u2026', 'indices': [0, 23]}]}, 'description': {'urls': []}}, 'protected': False, 'followers_count': 251, 'friends_count': 295, 'listed_count': 13, 'created_at': 'Sun Oct 03 01:32:35 +0000 2010', 'favourites_count': 112504, 'utc_offset': None, 'time_zone': None, 'geo_enabled': False, 'verified': False, 'statuses_count': 100136, 'lang': None, 'contributors_enabled': False, 'is_translator': False, 'is_translation_enabled': False, 'profile_background_color': '1A1B1F', 'profile_background_image_url': 'http://abs.twimg.com/images/themes/theme1/bg.png', 'profile_background_image_url_https': 'https://abs.twimg.com/images/themes/theme1/bg.png', 'profile_background_tile': True, 'profile_image_url': 'http://pbs.twimg.com/profile_images/1567659610185637890/Ep35e-zx_normal.jpg', 'profile_image_url_https': 'https://pbs.twimg.com/profile_images/1567659610185637890/Ep35e-zx_normal.jpg', 'profile_banner_url': 'https://pbs.twimg.com/profile_banners/197990491/1652726900', 'profile_link_color': '000000', 'profile_sidebar_border_color': 'FFFFFF', 'profile_sidebar_fill_color': 'FFFFFF', 'profile_text_color': '000000', 'profile_use_background_image': True, 'has_extended_profile': True, 'default_profile': False, 'default_profile_image': False, 'following': False, 'follow_request_sent': False, 'notifications': False, 'translator_type': 'none', 'withheld_in_countries': []}, id=197990491, id_str='197990491', name='irm\u00e3 vape', screen_name='matheusisonline', location='s\u00e3o paulo', description='tonelzinho de plut\u00f4nio', url='https://t.co/MDSDCeet1R', entities={'url': {'urls': [{'url': 'https://t.co/MDSDCeet1R', 'expanded_url': 'http://instagram.com/matheusisthecowboy', 'display_url': 'instagram.com/matheusistheco\u2026', 'indices': [0, 23]}]}, 'description': {'urls': []}}, protected=False, followers_count=251, friends_count=295, listed_count=13, created_at=datetime.datetime(2010, 10, 3, 1, 32, 35, tzinfo=datetime.timezone.utc), favourites_count=112504, utc_offset=None, time_zone=None, geo_enabled=False, verified=False, statuses_count=100136, lang=None, contributors_enabled=False, is_translator=False, is_translation_enabled=False, profile_background_color='1A1B1F', profile_background_image_url='http://abs.twimg.com/images/themes/theme1/bg.png', profile_background_image_url_https='https://abs.twimg.com/images/themes/theme1/bg.png', profile_background_tile=True, profile_image_url='http://pbs.twimg.com/profile_images/1567659610185637890/Ep35e-zx_normal.jpg', profile_image_url_https='https://pbs.twimg.com/profile_images/1567659610185637890/Ep35e-zx_normal.jpg', profile_banner_url='https://pbs.twimg.com/profile_banners/197990491/1652726900', profile_link_color='000000', profile_sidebar_border_color='FFFFFF', profile_sidebar_fill_color='FFFFFF', profile_text_color='000000', profile_use_background_image=True, has_extended_profile=True, default_profile=False, default_profile_image=False, following=False, follow_request_sent=False, notifications=False, translator_type='none', withheld_in_countries=[])"
            }
        }
    ]
}
0 Replies 0