亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將字典列表作為列標題和值添加到數據幀

將字典列表作為列標題和值添加到數據幀

慕田峪9158850 2022-09-27 09:31:41
我對熊貓有點陌生,我有一個項目,我有一個位鏈接及其各自指標的數據框架。我還收集了每個比特鏈接的國家/地區數據,當解析后者時,它會返回包含縣代碼及其相應點擊次數的字典列表。我想做的是將國家代碼作為列添加到現有的位鏈接數據幀中,然后將每個國家/地區的點擊次數保存到其特定的位鏈接行中。如果有人能在這方面幫助我,那就太好了。熊貓bitly_links數據幀:index | link        | long_url            | created_at          | link_clicks |------|-------------|---------------------|---------------------|-------------|0     | bit.ly/aaaa | https://example.com | 2020-04-01 10:54:33 | 150         |1     | bit.ly/bbbb | https://example.com | 2020-04-01 10:54:33 | 20          |2     | bit.ly/cccc | https://example.com | 2020-04-01 10:54:33 | 15          |3     | bit.ly/dddd | https://example.com | 2020-04-01 10:54:33 | 13          |Python國家/地區列表為一個特定的位(例如 bit.ly/aaaa)鏈接:countries_data = [                   {'country': 'US', 'clicks': 150}, {'country': 'UK', 'clicks': 20},                    {'country': 'AU', 'clicks': 45}, {'country': 'ZS', 'clicks': 31}                 ]index | country | clicks |------|---------|--------|0     | US      | 150    |1     | UK      | 20     |2     | AU      | 45     |3     | ZS      | 31     |我想制作的新數據幀:index | link        | long_url            | created_at          | link_clicks | US | UK | AU | ZS |------|-------------|---------------------|---------------------|-------------|----|----|----|----|0     | bit.ly/aaaa | https://example.com | 2020-04-01 10:54:33 | 110         | 20 | 30 | 10 | 50 |1     | bit.ly/bbbb | https://example.com | 2020-04-01 10:54:33 | 89          | 25 | 41 | 11 | 12 |2     | bit.ly/cccc | https://example.com | 2020-04-01 10:54:33 | 81          | 10 | 27 | 31 | 14 |3     | bit.ly/dddd | https://example.com | 2020-04-01 10:54:33 | 126         | 11 | 74 | 31 | 10 |
查看完整描述

1 回答

?
搖曳的薔薇

TA貢獻1793條經驗 獲得超6個贊

我認為您要做的是整理每次點擊的國家/地區信息數據:


# I take the example with two lists for link-level data related to countries, but

#  it extends to more :

import pandas as pd

countries_data1 = [

                   {'country': 'US', 'clicks': 150}, {'country': 'UK', 'clicks': 20},

                   {'country': 'AU', 'clicks': 45}, {'country': 'ZS', 'clicks': 31}

                 ]

countries_data2 = [

                   {'country': 'US', 'clicks': 150}, {'country': 'UK', 'clicks': 20},

                   {'country': 'AU', 'clicks': 45}, {'country': 'ZS', 'clicks': 31}

                 ]

# transform to dataframe, add variable link, and concat

countries_data1 = pd.DataFrame(countries_data1).assign(link="bit.ly/aaaa")

countries_data2 = pd.DataFrame(countries_data2).assign(link="bit.ly/bbbb")

df = pd.concat([countries_data1, countries_data2]) # you will concat the list of all 

# your dataframes with link information regarding countries, here I only have 2 in

#  this example


# then go in wide format with pivot_table

df = df.pivot_table(index="link", values="clicks", columns="country")

你得到這個表:


country      AU  UK   US  ZS

link                        

bit.ly/aaaa  45  20  150  31

bit.ly/bbbb  45  20  150  31


# assume your first table (simplified) is : 

table = pd.DataFrame({"link": ["bit.ly/aaaa", "bit.ly/bbbb"],

                      "link_clicks": [150,20]})

# set the index for link

table = table.set_index("link")


# then do an outer join on link 

merge_df = pd.concat([table, df], join="outer", axis=1)

merge_df.head()

您得到的結果:


             link_clicks  AU  UK   US  ZS

link                                     

bit.ly/aaaa          150  45  20  150  31

bit.ly/bbbb           20  45  20  150  31


查看完整回答
反對 回復 2022-09-27
  • 1 回答
  • 0 關注
  • 106 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號