Python 在循环中使用成对的文件和列?

Python 在循环中使用成对的文件和列?,python,pandas,Python,Pandas,我想在一个循环中创建一对文件和所需的列 我想买本字典可以,但不确定 例如: files: file1,file2,file3,etc dict1 = ({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(d

我想在一个循环中创建一对文件和所需的列

我想买本字典可以,但不确定

例如:

files: file1,file2,file3,etc

dict1 = ({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']]
我怎样才能做到这一点

每个文件中的列并不总是相同的。
对于一个文件,我们需要两个特定的列,而对于另一个完全不同的列。这就是我考虑使用字典的原因。

你在找这个吗

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

向上投票!这是基于问题中的想法。你认为不同的方法可能更好吗?我不这么认为。简单胜于复杂问题在于,由于有不同的列,我问题中的最后一行代码在循环中如何工作?我的意思是,在每个循环中,列的名称将不同。如何解决此问题?但您不会在每个步骤中都有一个列的名称吗?我说得对吗?