我键入这一行:
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.发布于 2018-11-02 11:38:07
列标签可以修改。只需在创建dataframe之后显式地命名列:
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']]https://stackoverflow.com/questions/53117624
复制相似问题