我有一个超过900列的巨 Big Data 集.部分栏目是世界语言.我想按字母顺序对数据集中的这部分列进行排序.我的解决方案是对数据集进行切片,将此子集分配给另一个变量,并对列进行排序.现在,我试图用排序的子集替换未排序的子集,但下面的代码没有起到作用.

lang_columns1=survey.iloc[:,19:104] #assigning subset of columns to a variable

lang_columns_sorted1 =lang_columns1.reindex(sorted(lang_columns1.columns), axis=1) 
#sorting columns in alphabetical order by name

survey.iloc[:,19:104] = lang_columns_sorted1 #replacing the unsorted columns by the sorted columns 

对如何解决这个问题有什么建议吗?

推荐答案

通过连接所有列使用DataFrame.reindex:

s = survey.columns.to_series()
survey.columns = [s, s.groupby(s).cumcount()]

cols = survey.columns[:19].tolist() + 
       sorted(survey.columns[19:104], key=lambda x: (x[0], x[1])) + 
       survey.columns[104:].tolist()

survey = survey.reindex(cols, axis=1).droplevel(1, axis=1)

Python相关问答推荐

如何输入提示抽象方法属性并让mypy高兴?

使用Python从HTTP打印值

Pandas数据帧处理Pandas表中Json内的嵌套列表以获取后续Numpy数组

手动为pandas中的列上色

预期LP_c_Short实例而不是_ctyles.PyCStructType

在Python中使用readline函数时如何向下行

Django序列化器没有验证或保存数据

Docker-compose:为不同项目创建相同的容器

是pandas.DataFrame使用方法查询后仍然排序吗?

具有症状的分段函数:如何仅针对某些输入值定义函数?

根据条件将新值添加到下面的行或下面新创建的行中

当使用keras.utils.Image_dataset_from_directory仅加载测试数据集时,结果不同

Vectorize多个头寸的止盈/止盈回溯测试pythonpandas

Python库:可选地支持numpy类型,而不依赖于numpy

Polars:用氨纶的其他部分替换氨纶的部分

如何在Python中并行化以下搜索?

如何在solve()之后获得症状上的等式的值

如何调整QscrollArea以正确显示内部正在变化的Qgridlayout?

不能使用Gekko方程'

使用Python从rotowire中抓取MLB每日阵容