我输入以下行:
df = pd.DataFrame.from_records(data=arcpy.da.SearchCursor("file1", ['colA','colB'])
#this line reads a file with the specified columns.
结果将第一列重命名为0,将另一列重命名为1。
如果在文档中看到。还有一个额外的columns
参数。
要使用的列名。如果传递的数据没有关联的名称 对于它们,此参数为列提供名称。除此以外 该参数指示结果中列的顺序。
问题在于整个代码处于循环中,无法指定此列,因为在每个循环中将读取不同的列。如何保留初始列而不将其变为0和1。
更新
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))
df['count_of_a_column']=[df['colA'].value_counts().loc[x] for x in df['colA']]
#When the df is made the column names are already ruined.
#They are called 0 and 1 if they are two. Thus the last line of code won't
#find the columns that imported.
答案 0 :(得分:1)
列标签可能会被修改。创建数据框后,只需明确命名列即可:
for k, v in dict1.items():
df = pd.DataFrame.from_records(data=arcpy.da.SearchCursor(k, v))
df.columns = v
df['count_of_a_column']=[df['colA'].value_counts().loc[x] for x in df['colA']]