![](/img/trans.png)
[英]How to extract tweets location which contain specific keyword using twitter API in Python
[英]how can I extract specific row which contain specific keyword from my json dataset using pandas in python?
抱歉,這可能是一個非常簡單的問題,但我對 python/json 和一切都是新手。 我正在嘗試根據 user_location/country_code/gb 過濾我的 twitter json 數據集。 但我不知道該怎么做。 我嘗試了幾種方法,但仍然沒有機會。 我附上了我的數據集和我在這里使用的一些代碼。 我將不勝感激任何幫助。
這是我為獲得最佳結果所做的事情,但是我不知道如何將整個數據集告訴 go 並打印出 tweet_id 的結果:
import json
import pandas as pd
df = pd.read_json('example.json', lines=True)
if df['user_location'][4]['country_code'] == 'th':
print (df.tweet_id[4])
else:
print('false')
此代碼向我顯示了 tweet_id: 1223489829817577472
但是,我無法將其擴展到整個數據集。
我也試過了theis代碼,仍然沒有機會:
dataset = df[df['user_location'].isin([ "gb" ])].copy()
print (dataset)
我會使用以下方法將user_location
列分成多列
df = pd.concat([df, df.pop('user_location').apply(pd.Series)], axis=1)
運行它應該為您提供一個列,每個列包含在user_location
json 中的鍵。 然后應該很容易使用以下命令根據 country_code 打印出 tweet_ids:
df[df['country_code']=='th']['tweet_id']
df.pop('user_location')
從 df 中移除 'user_location' 列並同時返回.apply
方法將 function 應用於列pd.Series
將 JSON 數據/字典轉換為 DataFramepd.concat
將原始 df(現在沒有 'user_location' 列)與從 'user_location' 數據創建的新列連接起來
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.