我想制作一对文件和循环中需要的列。我想一本字典可以,但不确定。例子:files: file1,file2,file3,etcdict1 = ({file1:its needed columns,file2:its needed columns})后面会用到这个函数:for i in dict1: # below it reads the files from arcpy - not important df = pd.DataFrame.from_records(data=arcpy.da.SearchCursor("key_of_dict", ['the_one_column','the_other_column']) #then make a new column that will apply the value_counts to a certain column df['count_of_a_col']=[df['one_col'].value_counts().loc[x] for x in df['one_col']]我怎样才能使这项工作?笔记每个文件中的列并不总是相同的。对于一个文件,我们需要两个特定的列,而对于另一个则完全不同。这就是我考虑使用字典的原因。
1 回答
德玛西亚99
TA贡献1770条经验 获得超3个赞
你在找这个吗?
dict1 = {'file_name_1': ['on_column', 'another_column'], 'file_name_2': ['again_column']}
for k, v in dict1.items():
df = pd.DataFrame.from_records(data=arcpy.da.SearchCursor(k, v))
column_for_count = v[1] if len(v) >= 2 else v[0]
df['count_of_' + column_for_count]=[df[column_for_count].value_counts().loc[x] for x in df[column_for_count]]
# do what you want with v
添加回答
举报
0/150
提交
取消