【问题标题】:Listed dictionaries from list inside a pandas column熊猫列内列表中列出的字典
【发布时间】:2021-12-01 09:34:22
【问题描述】:

我有类似的数据

{'message_text': '*प्रशासक समिति*',
'translated_text': '* Administrator ',
'annotation_list': ['Fear speech', 'Fear speech', 'Normal'],
'propagation': [{'group_id': 9087,
'user_id': 229869,
'timestamp': 1538130086000},
{'group_id': 7, 'user_id': 215, 'timestamp': 1550186113000}]}


current code
df=pd.DataFrame.from_dict(fear_speech_data, orient='index')

目前,我只能阅读四列代码

message_text    
translated_text 
annotation_list 
propagation

我想将上述详细信息读入数据框中,数据框中包含以下列

message_text    
translated_text 
annotation_list 
propagation
group_id
user_id
timestamp
group_id
user_id
timestamp

【问题讨论】:

  • 首先你不应该/不能有同名的列。其次,您的数据只有 2 列。你想从哪里得到其他人?如果其他是列,那么这是否意味着您的 df 是多索引的?

标签: python python-3.x pandas dataframe dictionary


【解决方案1】:

已更新基于数据链接

我从提供的 json 中取出了值,因为它不容易使用,但我认为这是你想要的

from flatten_json import flatten
url ='https://raw.githubusercontent.com/hate-alert/Fear-speech-analysis/master/Data/fear_speech_data.json'
r = requests.get(url)
data = json.loads(r.text)

#list(data.values())
    
dic_flattened = (flatten(d, '.') for d in list(data.values()))
df = pd.DataFrame(dic_flattened)

输出

                                           message_text                                    translated_text annotation_list.0 annotation_list.1  ... propagation.241.group_id  propagation.241.user_id  propagation.241.timestamp  propagation
0     *प्रशासक समिति*✊?  ●●●●●●●●●●● ● ● ● ?? *आंतकव...  * Administrator ✊ ?   Committee * ● ●●●●●●●●●●...       Fear speech       Fear speech  ...                      NaN                      NaN                        NaN          NaN
1     हैदराबाद की मक्का मस्जिद में ब्लास्ट 16 मरे 10...  Blast 16 killed and 100 injured in Hyderabad's...            Normal            Normal  ...                      NaN                      NaN                        NaN          NaN
2     *(1) - "इस्लाम धर्म नहीं एक मानसिक रोग है" - श...  * (1) - "Islam is not a religion but a mental ...       Fear speech       Fear speech  ...                      NaN                      NaN                        NaN          NaN
3     पाकिस्तान से अधिक तो जमात-ए-इस्लामी के संसाधन ...  Jamaat-e-Islami has more resources than Pakist...       Fear speech            Normal  ...                      NaN                      NaN                        NaN          NaN
4     शहरी नक्सलवाद माओवाद । बस एक निवेदन ... पढ़ना ...  Urban Naxalism Maoism. Just a request… start r...            Normal            Normal  ...                      NaN                      NaN                        NaN          NaN
...                                                 ...                                                ...               ...               ...  ...                      ...                      ...                        ...          ...
4777  हिन्दुओं से बेशर्म कोई जाति पूरा पढ़े कठोर वचन...  I also apologize to the Hindus for the shamele...       Fear speech            Normal  ...                      NaN                      NaN                        NaN          NaN
4778  अब भी नही जागे तो मधेपुरा पूर्णिया में भी हिंद...  Even if you do not wake up, then in Madhepura ...       Fear speech       Fear speech  ...                      NaN                      NaN                        NaN          NaN
4779  इतिहास का सबसे बड़ा छल हिंदुओं के साथ हुआ  मुस...  The biggest trick of history happened to Hindu...            Normal            Normal  ...                      NaN                      NaN                        NaN           []
4780  फिर कहते हैं #राममन्दिर नहीं बना _____________...  Then it says #Ramandir is not made ___________...       Fear speech       Fear speech  ...                      NaN                      NaN                        NaN          NaN
4781  *उर्दू हिन्दुस्तान के देशभक्त क्रांतिकारियों क...  * Urdu is the tongue of the patriotic revoluti...            Normal            Normal  ...                      NaN                      NaN                        NaN          NaN

[4782 rows x 732 columns]

【讨论】:

  • 你好,字典中的一项可以正常工作,但不能多项,有什么解决方案吗?
  • 请发布更多数据。以及整体结构的确切语法。您的示例很可能不能代表您拥有的数据。但是让我们看看。
猜你喜欢
  • 2019-08-20
  • 2019-05-07
  • 2019-05-02
  • 2020-05-31
  • 2018-05-09
  • 2018-11-21
  • 1970-01-01
  • 1970-01-01
  • 2020-07-22
相关资源
最近更新 更多