티스토리 뷰
Stack Overflow에 자주 검색, 등록되는 문제들과 제가 개발 중 찾아 본 문제들 중에서 나중에도 찾아 볼 것 같은 문제들을 정리하고 있습니다.
Stack Overflow에서 가장 먼저 확인하게 되는 가장 높은 점수를 받은 Solution과 현 시점에 도움이 될 수 있는 가장 최근에 업데이트(최소 점수 확보)된 Solution을 각각 정리하였습니다.
아래 word cloud를 통해 이번 포스팅의 주요 키워드를 미리 확인하세요.
How to change the order of DataFrame columns?
데이터 프레임 열 순서 변경하기
문제 내용
I have the following DataFrame
(df
):
다음과 같은 데이터 프레임(df)이 있습니다.
import numpy as np
import pandas as pd
df = pd.DataFrame(np.random.rand(10, 5))
I add more column(s) by assignment:
할당을 통해 열을 추가합니다.
df['mean'] = df.mean(1)
How can I move the column mean
to the front, i.e. set it as first column leaving the order of the other columns untouched?
열 mean을 앞쪽으로 이동하려면 어떻게 해야 합니까? 즉, 다른 열의 순서를 그대로 둔 상태에서 첫 번째 열로 설정하면 됩니까?
높은 점수를 받은 Solution
One easy way would be to reassign the dataframe with a list of the columns, rearranged as needed.
한 가지 쉬운 방법은 데이터 프레임을 필요에 따라 재정렬된 열 목록으로 재할당하는 것입니다.
This is what you have now:
지금은 다음과 같은 기능을 참조하십시오.
In [6]: df
Out[6]:
0 1 2 3 4 mean
0 0.445598 0.173835 0.343415 0.682252 0.582616 0.445543
1 0.881592 0.696942 0.702232 0.696724 0.373551 0.670208
2 0.662527 0.955193 0.131016 0.609548 0.804694 0.632596
3 0.260919 0.783467 0.593433 0.033426 0.512019 0.436653
4 0.131842 0.799367 0.182828 0.683330 0.019485 0.363371
5 0.498784 0.873495 0.383811 0.699289 0.480447 0.587165
6 0.388771 0.395757 0.745237 0.628406 0.784473 0.588529
7 0.147986 0.459451 0.310961 0.706435 0.100914 0.345149
8 0.394947 0.863494 0.585030 0.565944 0.356561 0.553195
9 0.689260 0.865243 0.136481 0.386582 0.730399 0.561593
In [7]: cols = df.columns.tolist()
In [8]: cols
Out[8]: [0L, 1L, 2L, 3L, 4L, 'mean']
Rearrange cols
in any way you want. This is how I moved the last element to the first position:
원하는 방식으로 열을 재배열하십시오. 마지막 요소를 첫 번째 위치로 이동한 방법은 다음과 같습니다.
In [12]: cols = cols[-1:] + cols[:-1]
In [13]: cols
Out[13]: ['mean', 0L, 1L, 2L, 3L, 4L]
Then reorder the dataframe like this:
그런 다음 다음과 같이 데이터 프레임 순서를 변경합니다.
In [16]: df = df[cols] # OR df = df.ix[:, cols]
In [17]: df
Out[17]:
mean 0 1 2 3 4
0 0.445543 0.445598 0.173835 0.343415 0.682252 0.582616
1 0.670208 0.881592 0.696942 0.702232 0.696724 0.373551
2 0.632596 0.662527 0.955193 0.131016 0.609548 0.804694
3 0.436653 0.260919 0.783467 0.593433 0.033426 0.512019
4 0.363371 0.131842 0.799367 0.182828 0.683330 0.019485
5 0.587165 0.498784 0.873495 0.383811 0.699289 0.480447
6 0.588529 0.388771 0.395757 0.745237 0.628406 0.784473
7 0.345149 0.147986 0.459451 0.310961 0.706435 0.100914
8 0.553195 0.394947 0.863494 0.585030 0.565944 0.356561
9 0.561593 0.689260 0.865243 0.136481 0.386582 0.730399
가장 최근 달린 Solution
You can reorder the dataframe columns using a list of names with:
다음 이름 목록을 사용하여 데이터 프레임 열의 순서를 변경할 수 있습니다.
df = df.filter(list_of_col_names)
출처 : https://stackoverflow.com/questions/13148429/how-to-change-the-order-of-dataframe-columns
'개발 > 파이썬' 카테고리의 다른 글
빈 Pandas 데이터프레임 만든 후 한 행씩 추가하기 (0) | 2022.12.07 |
---|---|
Pandas DataFrame 열 전체 리스트 가져오기 (0) | 2022.12.06 |
Tensorflow 2.0 - AttributeError: module 'tensorflow' has no attribute 'Session' 에러 수정하기 (0) | 2022.12.06 |
딕셔너리 복사 후 사본만 편집하기 (0) | 2022.12.06 |
Python을 사용하여 리스트 내용을 파일에 쓰기 (0) | 2022.12.06 |