删除具有相同多索引的行

时间:2015-06-22 17:07:40

标签: python pandas multi-index

是否有一个快捷方式来删除具有相同多索引的行?换句话说,有没有办法比较索引而不将它们转换为列?这就是我到目前为止的工作方式,但我填补的是更好的东西。 提前谢谢。

    public static void main(String[] args) throws IOException {
    StreamFactory factory = StreamFactory.newInstance();
    StreamBuilder builderCSV = new StreamBuilder("Tm")
            .format("fixedlength")
            .parser(new FixedLengthParserBuilder())
            .addRecord(com.mycompany.bio.Team.class)
            .addRecord(com.mycompany.bio.Employee.class);
    factory.define(builderCSV);

    BeanWriter out = factory.createWriter("Tm", new File("C:\\Users\\topSecretUsername\\Desktop\\tm.txt"));

    Employee e1 = new Employee("EmpF1", "EmpL1", "Developer", "1", new Date());
    Employee e2 = new Employee("EmpF2", "EmpL2", "Developer", "2", new Date());
    Team team = new Team();
    team.setTeamName("Great Team");
    team.getEmployees().add(e1);
    team.getEmployees().add(e2);

    out.write(team);
    for (Employee e : team.getEmployees()) {
        out.write(e);
    }

    out.flush();
    out.close();

}

1 个答案:

答案 0 :(得分:1)

您可以使用df.index.get_level_values()直接获取多级索引值。

import pandas as pd
import numpy as np

# replicate your data
a = 'bar bar baz baz foo foo'.split()
b = 'one bar one two foo two'.split()
multi_index = pd.MultiIndex.from_tuples(list(zip(a, b)))
df = pd.DataFrame(np.random.randn(6, 5), index=multi_index)

# do the selection
mask = df.index.get_level_values(0) == df.index.get_level_values(1)
df = df.loc[~mask]

              0       1       2       3       4
bar one -0.0646  0.2245 -0.5863 -0.6400  1.4364
baz one  0.6803  1.6834 -1.0671 -1.0762 -0.8407
    two -0.4484  1.3863 -3.0398 -0.0031 -0.9646
foo two  0.0264  1.4345  0.5046  1.8788 -1.2081