[英]creating list from dataframe
I am a newbie to python. 我是python的新手。 I am trying iterate over rows of individual columns of a dataframe in python.
我正在尝试遍历python中数据框的各个列的行。 I am trying to create an adjacency list using the first two columns of the dataframe taken from csv data (which has 3 columns).
我正在尝试使用从csv数据(具有3列)中获取的数据帧的前两列来创建邻接表。
The following is the code to iterate over the dataframe and create a dictionary for adjacency list: 以下是用于遍历数据框并为邻接表创建字典的代码:
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)
and the following is the output I am getting: 以下是我得到的输出:
['933']
['4139']
{'933': '4139'}
I see that I am not getting the entire list when I use the list()
constructor. 当使用
list()
构造函数时,我看不到整个列表。 Hence I am not able to loop over the entire data. 因此,我无法遍历整个数据。
Could anyone tell me where I am going wrong? 谁能告诉我我要去哪里错了?
To summarize, Here is the input data: 总结一下,这是输入数据:
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
the output that I am expecting: 我期望的输出:
933: [4139,6597069777240, 10995116284808, 32985348833579, 32985348838375]
1129: [1242, 2199023262543, 6597069771886, 6597069776731]
Use groupby
and create Series
of list
s and then to_dict
: 使用
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]}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.