我有一个像下面这样的数据框架。
id1 ids1 Name1 Name2 ids2 ID col1 Goal col2 col3
0 85643 234,34,11223,345,345_2 aasd1 vaasd1 2234,354,223,35,3435 G-0001 1 NaN 3 1
1 85644 2343,355,121,34 aasd2 G-0001 2 56.0000 4 22
2 8564312 24 , 23 ,244 ,2421 ,567 ,789 aabsd1 G-0002 3 NaN 32 33
3 8564314 87 ,35 ,67_1 aabsd2 averabsd 387 ,355 ,667_1 G-00002 4 89.0000 43 44
我想基于name1、ids1和id1列以及name2、ids2和id1列创建列表。在列ids1和ids2中有一些额外的空间,需要在传输到列表时删除。例如,最终结果如下所示:
aasd1 = ['85643','234','34','11223','345','345_2']
vaasd1 = ['85643','2234','354','223','35','3435']
aasd2 = ['85644','2343','355','121','34']
aabsd1= ['8564312','24','23','244','2421','567','789']
...........
另外,我想基于name1和name2列创建一个字典。就像下面
dic = {"aasd1":aasd1, "vaasd1":vaasd1, "aasd2":aasd2, "vaasd1":vaasd1, "aabsd1":aabsd1, "aabsd2":aabsd2, "aabsd2":aabsd2}
任何帮助都会很感激。提前感谢!!
使用说明:
df['ids1'] = df['ids1'].str.split(',').apply(lambda x: [val.strip() for val in x]).tolist()
data = df.set_index('Name1').apply(lambda x: [x['id1']] + x['ids1'], axis=1).to_dict()
OUTPUT:
for key in data.keys():
print(key, ': ', data[key])
#OUTPUT
aasd1 : [85643, '234', '34', '11223', '345', '345_2']
aasd2 : [85644, '2343', '355', '121', '34']
aabsd1 : [8564312, '24', '23', '244', '2421', '567', '789']
averabsd : [8564314, '87', '35', '67_1']
For Second Part use:
data = df.assign(d=df['Name1']).set_index('d')['Name1'].to_dict()
OUTPUT:
{'aasd1': 'aasd1', 'aasd2': 'aasd2', 'aabsd1': 'aabsd1', 'averabsd': 'averabsd'}