我有一个期刊标题和纪元主题(该期刊的学科)的数据框架,它不整洁,在era_subjects
系列的同一单元格中包含多个值
df = pd.DataFrame({
'title':['Veterinary pathology', 'Clothing and textiles research journal'],
'era_subjects':["[['07', 'Agricultural and Veterinary Sciences'], ['04', 'Fisheries Sciences'], ['0707', 'Veterinary Sciences']]","[['1203', 'Design Practice and Management'], ['12', 'Built Environment and Design']]"],
'cpu_rank': ['1', '2'],
'subscribed': ['True', 'False'],
'downloads': ['800', '550']})
我编写了一个函数,只从era_subjects
中提取并返回最宽级别的两位数主题字符串(可以有多个)。例如,我在row 0
上的函数的结果是一个包含以下内容的单元格:
['Agricultural and Veterinary Sciences', 'Fisheries Sciences']
然后,我使用媒体文章here中概述的技术将生成的单元格分解为一个新的_df,在必要时重复多行日志名称:
现在,我想用原始的df中的信息来补充这个新的,例如subscribed
作为期刊标题。我不能使用new_dftitle
作为索引进行查找,因为它是重复的(例如,第0行和第1行)
经过大量的尝试和错误,以及我无法理解的join
和merge
方法的死胡同,我已经做到了:
for i in df.set_index('title').index:
temp_sub = df.set_index('title').loc[i, 'subscribed']
temp_filt = (new_df['title'] == i)
new_df.loc[temp_filt, 'subscribed'] = temp_sub
使用原始df中的标题(每行都是唯一的)保存该标题的订阅状态,然后过滤该标题上的新的\u df,并设置订阅状态
问题:
subscribed
是我想带过来的七个左右的专栏之一
根据期刊标题,从原始df。我能有效地做到这一点吗
要执行七个单独的临时变量和赋值李>new_df = pd.DataFrame({
'title':['Veterinary pathology', 'Veterinary pathology', 'Clothing and textiles research journal'],
'era_subjects':["Agricultural and Veterinary Sciences", 'Fisheries Sciences', 'Built Environment and Design'],
'cpu_rank': ['1', '1', '2'],
'subscribed': ['True', 'True', 'False'],
'downloads': ['800', '800', '550']})
我能够通过使用
.explode
来实现这一点df['era_split']
我的函数后面看起来像"Agricultural and Veterinary Sciences', 'Fisheries Sciences"
相关问题 更多 >
编程相关推荐