2016-04-25 3 views
2

У меня есть этот набор данных, который представляет информацию о кратерах Марса. Меня интересуют только широта, долгота и количество столбцов видимых слоев. Я пытаюсь получить широту в группы по 10 градусов: от -90 до -80, от -80 до -70 и т. Д., Принимая значения столбца слоя (0,1,2,3,4,5) и превращая их в сами столбцы, чтобы получить таблицу значений_числений каждого столбца столбца для каждой группы 10 градусов.Очень специфическая задача с pandas

What I want

What I have

вытягивать мои волосы на этом я, кажется, попытался все, что я мог понять.

+3

Не могли бы вы опубликовать пример _input_ и _output_ наборы данных (5- 7 строк в формате CSV/dict/JSON/Python __as text__, поэтому его можно использовать при кодировании) и описать, что вы хотите делать с входными данными i n для получения набора выходных данных? [Как создать минимальный, полный и проверяемый пример] (http://stackoverflow.com/help/mcve) – MaxU

ответ

3

Это работает для вас?

import pandas as pd 
import random 

# generate random data 
N = 100 
longitudes = [random.randint(-20, 89) for _ in xrange(N)] 
layers = [random.randint(0, 5) for _ in xrange(N)] 
data = pd.DataFrame({'LONGITUDE_CIRCLE_IMAGE': longitudes, 'NUMBER_LAYERS': layers}) 

def get_degree_group(longitude, mn=-20, mx=90, delta_deg=10): 
    """calculate the layer from the given longitude""" 
    return (longitude - mn)/delta_deg 

def make_table(df): 
    # make a new column by calculating the degree group from longitude column 
    df['degree_group'] = df.LONGITUDE_CIRCLE_IMAGE.apply(get_degree_group) 
    # count the number of craters with properties (deg_grp, num_lyr) 
    s = df.groupby(['degree_group', 'NUMBER_LAYERS']).size() 
    # s is a pandas Series where the index is in the form: (deg_grp, num_lyr) 
    # and the values are the counts of crates in that category 
    # 
    # We want to convert the series into a table where num_lyr values are columns 
    # This task is done with unstack method 
    table = s.unstack('NUMBER_LAYERS') 
    # there are some (deg_grp, num_lyr) cases for which there are no existing craters 
    # Pandas put NaN for those cases. It might be better to put 0 into those cells 
    table.fillna(0, inplace = True) 
    return table 

make_table(data) 
1

использование pd.cut сделать групп, и pivot_table сосчитать.

Образец данных:

lat=rand(3000)*180-90 
layers=randint(0,6,3000) 
data=pd.DataFrame({'lat':lat,'layers':layers}) 

сделать 18 групп:

data['groups'] = pd.cut(lat,linspace(-90,90,19)) 

И стол:

data.pivot_table(index='groups',columns='layers',aggfunc='count',fill_value=0) 

      lat    
layers  0 1 2 3 4 5 
groups      
(-90, -80] 4 1 2 1 1 0 
(-80, -70] 1 0 0 2 2 3 
(-70, -60] 4 3 2 4 3 4 
(-60, -50] 6 2 1 1 2 3 
(-50, -40] 2 3 4 2 2 4 
(-40, -30] 4 3 4 2 4 4 
(-30, -20] 2 5 2 2 3 2 
(-20, -10] 4 2 6 3 5 2 
(-10, 0]  3 4 2 3 2 1 
(0, 10]  5 3 4 3 4 7 
(10, 20]  3 3 2 2 2 3 
(20, 30]  2 1 1 4 3 5 
(30, 40]  1 2 0 2 2 3 
(40, 50]  1 3 3 2 3 4 
(50, 60]  6 0 2 4 1 6 
(60, 70]  3 3 2 5 1 5 
(70, 80]  1 4 5 3 2 2 
(80, 90]  1 7 3 2 4 2 
+0

Что такое linspace в pd.cut? – Holmesjr

+0

о диапазоне: http://docs.scipy.org/doc/numpy-1.10.0/reference/generated/numpy.linspace.html –

+0

данные ['LatGroups'] = pd.cut ("LATITUDE_CIRCLE_IMAGE", numpy. linspace (-90,90,19)) Я получаю «TypeError: невозможно передать данные массива из dtype ('float64') в dtype (' Holmesjr

Смежные вопросы