不幸的是,我正在努力执行以下计划:
我刮了一个大字典,用用户名作为键,并用键的Instagram关注者作为值。
看起来像这样:
dict = {
"User1": ['Follower_A', 'Follower_B', 'Follower_C', 'Follower_D', 'Follower_E', 'Follower_F', 'Follower_G'],
"User2": ['Follower_Z', 'Follower_D', 'Follower_V', 'Follower_E' 'Follower_O'],
"User3": ['Follower_P', 'Follower_Q'],
"User4": ['Follower_M', 'Follower_A', 'Follower_Z', 'Follower_P' 'Follower_L'],
.
.
.,
"UserX": ['Follower_Z', 'Follower_D', 'Follower_V', 'Follower_E' 'Follower_O','Follower_Q']}
为了创建一个网络,我需要一个看起来像这样的数据框:
User Follower
0 User1 Follower_A
1 User1 Follower_B
2 User1 Follower_C
3 User1 Follower_D
4 User1 Follower_E
5 User1 Follower_F
6 User1 Follower_G
7 User2 Follower_Z
8 User2 Follower_D
9 User2 Follower_V
10 User2 Follower_E
11 User2 Follower_O
12 User3 Follower_P
13 User3 Follower_Q
有人有想法以有效的方式创建它吗? 我所有的尝试都没有成功...
谢谢!
答案 0 :(得分:5)
对具有DataFrame
构造函数的元组使用列表理解和扁平化。
也不要使用变量名dict
,因为它是python代码保留字。
d = {
"User1": ['Follower_A', 'Follower_B', 'Follower_C', 'Follower_D', 'Follower_E', 'Follower_F', 'Follower_G'],
"User2": ['Follower_Z', 'Follower_D', 'Follower_V', 'Follower_E', 'Follower_O'],
"User3": ['Follower_P', 'Follower_Q'],
"User4": ['Follower_M', 'Follower_A', 'Follower_Z', 'Follower_P', 'Follower_L'],
"UserX": ['Follower_Z', 'Follower_D', 'Follower_V', 'Follower_E', 'Follower_O','Follower_Q']}
df = pd.DataFrame([(oldk, k) for oldk, oldv in d.items() for k in oldv],
columns=['User','Follower'])
print (df)
User Follower
0 User1 Follower_A
1 User1 Follower_B
2 User1 Follower_C
3 User1 Follower_D
4 User1 Follower_E
5 User1 Follower_F
6 User1 Follower_G
7 User2 Follower_Z
8 User2 Follower_D
9 User2 Follower_V
10 User2 Follower_E
11 User2 Follower_O
12 User3 Follower_P
13 User3 Follower_Q
14 User4 Follower_M
15 User4 Follower_A
16 User4 Follower_Z
17 User4 Follower_P
18 User4 Follower_L
19 UserX Follower_Z
20 UserX Follower_D
21 UserX Follower_V
22 UserX Follower_E
23 UserX Follower_O
24 UserX Follower_Q