미리 결정된 규칙에 따라 열의 값을 계산하고 계산 된 숫자로 새 열을 만드는 방법은 무엇입니까?

아우 구스토 발도

제목은 요약하기 어려웠지만 다음과 같은 테이블의 값으로 구성된 문제에 갇혀 있습니다.

       Star        Star_planets      Planet_size    
0      star1      star1_planet1         15.2  
1      star2      star2_planet1          3.3
2      star2      star2_planet2          1.8    
3      star2      star2_planet3         13.0
4      star3      star3_planet1         10.3
5      star3      star3_planet2          6.7
6      star3      star3_planet3         12.2

제가하려는 것은 별이 행성의 크기를 기준으로 행성의 수를 세는 4 개의 새로운 열이있는 새 테이블을 만들어 분류하는 것입니다. 또한 'Star_planets'및 'Planet_size'열은 더 이상 사용되지 않기 때문에 존재하지 않아야합니다. 이 테이블은 다음과 같습니다.

       Star        Big_planet   Med_planet   Sml_planet   Tny_planet
0      star1           1            0            0            0
1      star2           1            0            1            1
2      star3           2            1            0            0

행성 크기를 결정하는 데 사용되는 제한은 다음과 같습니다.

Big: >8
Medium: 4<Size<=8
Small: 2<Size<=4
Tiny: <=2

그룹으로 크기를 구분하기 위해이 작업을 수행했지만 여기에서 진행하는 방법을 정말 모르겠습니다.

df = pd.read_csv("Table_planets.csv")

df['Tn'] = df.loc[df.[Planet_size] <= 2, 'Planet_size']
df['Sm'] = df.loc[(df.[Planet_size] > 2)&(df.[Planet_size] <=4), 'Planet_size']
df['Md'] = df.loc[(df.[Planet_size] > 4)&(df.[Planet_size] <=8), 'Planet_size']
df['Bg'] = df.loc[df.[Planet_size] > 8, 'Planet_size']

그 테이블을 어떻게 얻을 수 있습니까?

얼굴

먼저 범주 형 열을 추가해 보겠습니다.

def planet_category(planet_size): 
    if planet_size > 8: 
        return 'Big' 
    elif planet_size > 4: 
        return 'Medium' 
    elif planet_size > 2: 
        return 'Small' 
    return 'Tiny'

df['Planet_category'] = df['Planet_size'].apply(planet_category)
df['Planet_category']

산출:

0       Big
1     Small
2      Tiny
3       Big
4       Big
5    Medium
6       Big
Name: Planet_category, dtype: object

그런 다음 범주를 pd.get_dummies, Star열로 그룹화 하고 결과 값을 다음과 같이 합산 할 수 있습니다.

pd.get_dummies(df, columns=['Planet_category']).groupby('Star').sum()

산출:

       Planet_size  Planet_category_Big  Planet_category_Medium  Planet_category_Small  Planet_category_Tiny
Star                                                                                                        
star1         15.2                    1                       0                      0                     0
star2         18.1                    1                       0                      1                     1
star3         29.2                    2                       1                      0                     0

이렇게하면 Star열이 인덱스로 바뀝니다 . 열로 되돌리려 reset_index()에 호출을 추가하십시오 Star.

pd.get_dummies(df, columns=['Planet_category']).groupby('Star').sum().reset_index()

산출:

    Star  Planet_size  Planet_category_Big  Planet_category_Medium  Planet_category_Small  Planet_category_Tiny
0  star1         15.2                    1                       0                      0                     0
1  star2         18.1                    1                       0                      1                     1
2  star3         29.2                    2                       1                      0                     0

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

Related 관련 기사

뜨겁다태그

보관