열 순서 변경

열의 순서를 변경해보자. 열을 리스트로 추출하고 원하는데로 정렬한 후 다시 DF에 넣어주면 열이 변경된다.

import pandas as pd
import seaborn as sns

titanic = sns.load_dataset('titanic')
df = titanic.loc[0:4, 'survived':'age']

columns = list(df.columns.values) # Col을 List로 추출
print(columns)

columns_sorted = sorted(columns)
print(columns_sorted)

df_sorted = df[columns_sorted]
print(df_sorted)
['survived', 'pclass', 'sex', 'age']
['age', 'pclass', 'sex', 'survived']
    age  pclass     sex  survived
0  22.0       3    male         0
1  38.0       1  female         1
2  26.0       3  female         1
3  35.0       1  female         1
4  35.0       3    male         0

열 분리

어떤 열이 많은 정보를 담고 있으면 각 정보를 분리해서 사용하는 경우가 있다. 예를 들어 Timestamp를 연, 월, 일로 분리할 수 있고 사람의 이름을 성과 이름으로 구분할 수 있다.

Series.str을 사용할텐데 자세한 기능들은 아래의 doc에 있다. 구글에 찾아봐도 정보가 많이 나온다.

참고 : string handling doc

Series - pandas 1.2.5 documentation

str.split('-')으로 문자열을 분리시켜 리스트에 저장할 수 있고. str.get(index)로 리스트를 인덱싱하여 선택할 수 있다.

import pandas as pd

df = pd.read_excel('주가데이터.xlsx')
print("주가데이터 : ")
print(df.head())
print()

print(df.dtypes)
print()

df['연월일'] = df['연월일'].astype('str')
**dates = df['연월일'].str.split('-')**
print(dates.head())

print()
**df['연'] = dates.str.get(0)
df['월'] = dates.str.get(1)
df['일'] = dates.str.get(2)**
print(df[['연월일', '연', '월', '일']].head())
주가데이터 : 
         연월일   당일종가  전일종가  ...     고가    
 저가     거래량
0 2018-07-02  10100   600  ...  10900  10000  137977  
1 2018-06-29  10700   300  ...  10900   9990  170253  
2 2018-06-28  10400   500  ...  10950  10150  155769  
3 2018-06-27  10900   100  ...  11050  10500  133548  
4 2018-06-26  10800   350  ...  11000  10700   63039  

[5 rows x 7 columns]

연월일     datetime64[ns]
당일종가             int64
전일종가             int64
시가               int64
고가               int64
저가               int64
거래량              int64
dtype: object

0    [2018, 07, 02]
1    [2018, 06, 29]
2    [2018, 06, 28]
3    [2018, 06, 27]
4    [2018, 06, 26]
Name: 연월일, dtype: object

          연월일     연   월   일
0  2018-07-02  2018  07  02
1  2018-06-29  2018  06  29
2  2018-06-28  2018  06  28
3  2018-06-27  2018  06  27
4  2018-06-26  2018  06  26

Reference : 문자열 관련 함수

[Python] pandas 문자열 관련 함수 str


back_to_top