Search code examples
pythonpandasjsonlines

Try to Select jsonl data column in another columns with .loc but got KeyError even though the key exists


this is my data structure in jsonl

"content": "Not yall gassing up a gay boy with no rhythm", "place": {"_type": "snscrape.modules.twitter.Place", "fullName": "Manhattan, NY", "name": "Manhattan", "type": "city", "country": "United States", "countryCode": "US"}

i try to select countryCode from place column with this code

country_df = test_df.loc[test_df['place'].notnull(), ['content', 'place']]
countrycode_df = country_df["place"].loc["countryCode"]

but it gave me this error

KeyError: 'countryCode'

how do i fix this?

I had try this method but it doesnt fit my situation


Solution

  • You can access it with str:

    country_df['place'].str['countryCode']
    

    Output:

    0    US
    Name: place, dtype: object