简体   繁体   English

迭代命名 Pandas 列

[英]Iteratively naming Pandas columns

I feel like this should be super easy to do, but I can't find a solution.我觉得这应该非常容易做到,但我找不到解决方案。

I have a data file with 400 columns, none of the columns are labeled.我有一个包含 400 列的数据文件,所有列都没有标记。 Each set of four columns is a different experiment with different numbers of rows.每组四列是具有不同行数的不同实验。

What I would like to do is label the columns something like 1.1, 1.2, 1.3, 1.4, 2.1, 2.2, 2.3... etc.我想做的是将列标记为 1.1、1.2、1.3、1.4、2.1、2.2、2.3 等。

All of the solutions I've seen seem to presuppose that you have prelabeled but poorly named labels and that you just want to rename them.我见过的所有解决方案似乎都预先假设您已经预先标记了但名称不佳的标签,并且您只想重命名它们。 There must be an iterative method for doing something like this.必须有一种迭代方法来做这样的事情。

You could use a DataFrame with a MultiIndex for the columns:您可以将 DataFrame 与 MultiIndex 用于列:

import pandas as pd
data = pd.DataFrame(<your data here>, 
                    columns = pd.MultiIndex.from_product([range(1, 101), range(1, 5)]))       

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM