首页 > 解决方案 > 从python中的csv中查找字符串长度并附加字典格式的另一列

问题描述

我的数据框 - 在此处输入图像描述

基本上-我想附加房屋和地区专栏

我的预期输出格式是我的预期输出-[('House 263 dhaka', {'entities': [[(0, 8)], 'holding_number'], [(10,14), 'district']})

我该怎么做?

标签: pythonpandaslistdataframedictionary

解决方案


尝试使用此列表理解:

>>> [(k, {'entities': [[[0, len(k.rpartition(' ')[0]) - 1], v['label1']], [(k.rfind(' ') + 1, len(k) - 1), v['label2']]]}) for k, v in df.set_index(['house', 'district']).set_axis(df[['house', 'district']].agg(' '.join, axis=1)).to_dict('index').items()]
[('House 163 dhaka', {'entities': [[[0, 8], 'holding_number'], [(10, 14), 'district']]}), ('House 31 comilla', {'entities': [[[0, 7], 'holding_number'], [(9, 15), 'district']]}), ('House 193/A chittagong', {'entities': [[[0, 10], 'holding_number'], [(12, 21), 'district']]})]
>>> 

推荐阅读