我是python的新手。我正在尝试遍历python中数据框的各个列的行。我正在尝试使用从csv数据(具有3列)中获取的数据帧的前两列来创建邻接表。
以下是用于遍历数据框并为邻接表创建字典的代码:
df1 = pd.read_csv('person_knows_person_0_0_sample.csv', sep=',', index_col=False, skiprows=1)
src_list = list(df1.iloc[:, 0:1])
tgt_list = list(df1.iloc[:, 1:2])
adj_list = {}
for src in src_list:
for tgt in tgt_list:
adj_list[src] = tgt
print(src_list)
print(tgt_list)
print(adj_list)
以下是我得到的输出:
['933']
['4139']
{'933': '4139'}
使用list()
构造函数时,我看不到整个列表。
因此,我无法遍历整个数据。
谁能告诉我我要去哪里错了?
总结一下,这是输入数据:
A,B,C
933,4139,20100313073721718
933,6597069777240,20100920094243187
933,10995116284808,20110102064341955
933,32985348833579,20120907011130195
933,32985348838375,20120717080449463
1129,1242,20100202163844119
1129,2199023262543,20100331220757321
1129,6597069771886,20100724111548162
1129,6597069776731,20100804033836982
我期望的输出:
933: [4139,6597069777240, 10995116284808, 32985348833579, 32985348838375]
1129: [1242, 2199023262543, 6597069771886, 6597069776731]
答案 0 :(得分:2)
使用groupby
并创建Series
个list
,然后创建to_dict
:
#selecting by columns names
d = df1.groupby('A')['B'].apply(list).to_dict()
#seelcting columns by positions
d = df1.iloc[:, 1].groupby(df1.iloc[:, 0]).apply(list).to_dict()
print (d)
{933: [4139, 6597069777240, 10995116284808, 32985348833579, 32985348838375],
1129: [1242, 2199023262543, 6597069771886, 6597069776731]}