Как выбрать конкретный столбец в excel с помощью pandas [duplicate]

вы можете использовать IComparable

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
              List<string> data = new List<string>{
                "3.5.0.1", "3.4.1.9", "3.4.1.56", "3.4.1.55", "3.4.1.46",
                "3.4.1.45", "3.4.1.44", "3.4.1.30", "3.4.1.3", "3.4.1.22",
                "3.4.1.2", "3.4.1.11", "3.4.1.0", "3.4.0.7", "3.4.0.3",
                "3.4.0.1", "3.3.0.8", "3.3.0.4", "3.3.0.0", "3.2.0.9",
                "3.2.0.6", "3.2.0.3", "3.2.0.27", "3.2.0.20", "3.2.0.15",
                "3.2.0.1", "3.2.0.0", "3.1.0.7", "3.1.0.15", "3.1.0.14"
              };
              List<SortPara> sortPara = data.Select(x => new SortPara(x)).ToList();
              data = sortPara.OrderBy(x => x).Select(x => x.strNumbers).ToList();
              data = sortPara.OrderByDescending(x => x).Select(x => x.strNumbers).ToList();
        }

    }
    public class SortPara : IComparable<SortPara>
    {
        public List<int> numbers { get; set; }
        public string strNumbers { get; set; }
        public SortPara(string strNumbers)
        {
            this.strNumbers = strNumbers;
            numbers = strNumbers.Split(new char[] { '.' }).Select(x => int.Parse(x)).ToList();

        }
        public int CompareTo(SortPara other)
        {
            int shortest = this.numbers.Count < other.numbers.Count ? this.numbers.Count : other.numbers.Count;
            int results = 0;
            for (int i = 0; i < shortest; i++)
            {
                if (this.numbers[i] != other.numbers[i])
                {
                    results = this.numbers[i].CompareTo(other.numbers[i]);
                    break;
                }
            }
            return results;
        }
    }
}
13
задан Martin Vseticka 14 November 2015 в 15:27
поделиться

2 ответа

parse_cols устарел, используйте usecols вместо

, который есть:

df = pd.read_excel(file_loc, index_col=None, na_values=['NA'], usecols = "A,C:AA")

3
ответ дан Leoli 18 August 2018 в 18:20
поделиться

Вы можете использовать индексы столбцов (буквы) следующим образом:

import pandas as pd
import numpy as np
file_loc = "path.xlsx"
df = pd.read_excel(file_loc, index_col=None, na_values=['NA'], parse_cols = "A,C:AA")
print(df)

Соответствующая документация :

parse_cols : int or list, default None
If None then parse all columns,
If int then indicates last column to be parsed
If list of ints then indicates list of column numbers to be parsed
If string then indicates comma separated list of column names and column ranges (e.g. “A:E” or “A,C,E:F”)
17
ответ дан Martin Vseticka 18 August 2018 в 18:20
поделиться
  • 1
    Следует отметить, что "имена" следует читать как «имена в excel», а не те, которые вы можете выбрать или использовать в качестве заголовков. Документы не совсем понятны, но это стоит упомянуть, это дало мне некоторые головные боли. – Ando Jurai 2 June 2017 в 12:16
Другие вопросы по тегам:

Похожие вопросы: