Python 将字典列表作为列标题和值添加到dataframe

Python 将字典列表作为列标题和值添加到dataframe,python,pandas,dataframe,Python,Pandas,Dataframe,我对pandas有点陌生,我有一个项目,其中我有一个bitly链接及其各自度量的数据框架。我还收集了每个bitly链接的国家数据,当解析后者时,它会返回一个包含县代码及其相应点击次数的词典列表 我想做的是将国家代码作为列添加到现有的bitly链接数据框中,然后将每个国家的点击次数保存到其特定的bitly链接行中。 如果有人能在这方面帮助我,那就太好了 bitly_链路的数据帧: index | link | long_url | created_at

我对pandas有点陌生,我有一个项目,其中我有一个bitly链接及其各自度量的数据框架。我还收集了每个bitly链接的国家数据,当解析后者时,它会返回一个包含县代码及其相应点击次数的词典列表

我想做的是将国家代码作为列添加到现有的bitly链接数据框中,然后将每个国家的点击次数保存到其特定的bitly链接行中。 如果有人能在这方面帮助我,那就太好了

bitly_链路的数据帧:

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          |
一个特定bitly(例如bit.ly/aaaa)链接的Python国家/地区列表:

countries\u数据=[
{'country':'US','clicks':150},{'country':'UK','clicks':20},
{'country':'AU','clicks':45},{'country':'ZS','clicks':31}
]
索引|国家|点击次数|
------|---------|--------|
0 | US | 150|
1 |英国| 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 |

我认为您需要做的是整理每次单击的国家信息数据:

# 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
你得到的结果是:

             link_clicks  AU  UK   US  ZS
link                                     
bit.ly/aaaa          150  45  20  150  31
bit.ly/bbbb           20  45  20  150  31

不知道为什么值会改变,你能解释一下为什么
150
变成
110
?因为我不太在意复制粘贴数据。我刚刚输入了所有可用的数字。非常感谢。这是一个很棒的方法。我不知道Pandas中的pivot_表方法。我会仔细看看的。再次感谢^^@Aditya Bholah欢迎您的光临,如果您觉得合适,您能将其标记为已接受的答案吗?谢谢
             link_clicks  AU  UK   US  ZS
link                                     
bit.ly/aaaa          150  45  20  150  31
bit.ly/bbbb           20  45  20  150  31