是否将每个结果存储在不同的数据框中?

时间:2018-11-06 11:02:53

标签: python pandas dataframe

这本词典:

左侧:IAO,INOT_A等是数据框,右侧是列。

dict1 =({'IAO' :  ['N','OBD'] ,'INOT_A' : ['CIN','OTID'], 
          'IA_PST_S' : ['K','OBD'], 'IA' : ['K','jse']})

此代码使df在最后一行(问题)的每个循环中都被替换:

for k, v in dict1.items():
    df = pd.DataFrame.from_records(data=arcpy.da.SearchCursor(k, v))
    df.columns = v
    column_for_count =v[0]
    df['count_of_' + column_for_count]=[df[column_for_count].value_counts().loc[x] for x in df[column_for_count]]

如何使其存储在其他df中?

1 个答案:

答案 0 :(得分:4)

您可以使用原始dict1的键创建DataFrame字典:

dfs = {}
for k, v in dict1.items():
    df = pd.DataFrame.from_records(data=arcpy.da.SearchCursor(k, v))
    df.columns = v
    column_for_count =v[0]
    df['count_of_' + column_for_count]=[df[column_for_count].value_counts().loc[x] for x in df[column_for_count]]
    dfs[k] = df

然后选择每个DataFrame使用:

print (dfs['IAO'])