Dataframe과 Series합치기
(1)DataFrame에 Series 좌+우로 합치기: pd.concat([df,Series],axis = 1)
pd.concat([df_1,Series_1],axis=1)
DataFrame에 Series를 좌+우로 합칠 때 열이름 무시하고 정수번호 자동 부여
Series끼리 좌+우로 합치기: pd.concat([Series1,Series2, ...] , axis=1)
In [8]: Series_1 = pd.Series(['S1', 'S2', 'S3'], name='S')
In [9]: Series_2 = pd.Series([0, 1, 2]) # without name
In [10]: Series_3 = pd.Series([3, 4, 5]) # without name
In [11]: Series_1
Out[11]:
0 S11 S22 S3
Name: S, dtype: object
In [12]: Series_2
Out[12]:
0 01 12 2dtype: int64
In [13]: Series_3
Out[13]:
0 31 42 5dtype: int64
# name of Series will be used as the column name of concatenated DataFrame
In [14]: pd.concat([Series_1, Series_2, Series_3], axis=1)
Out[14]:
S 0 10 S1 0 31 S2 1 42 S3 2 5
출처: https://rfriend.tistory.com/tag/ignore_index=True) [R, Python 분석과 프로그래밍의 친구 (by R Friend):티스토리]
Series끼리 합칠 때 열 이름(column name 덮어쓰기) : keys = ['xx','xx',...]
In [15]: pd.concat([Series_1, Series_2, Series_3], axis=1, keys=['C0', 'C1', 'C1'])
Out[15]:
C0 C1 C10 S1 0 31 S2 1 42 S3 2 5
DataFrame에 Series를 '위+아래'로 합치기: df.append(Series,ignore_index=True)
In [16]: df_1
Out[16]:
A B C D0 A0 B0 C0 D01 A1 B1 C1 D12 A2 B2 C2 D2
In [17]: Series_4 = pd.Series(['S1', 'S2', 'S3', 'S4'], index=['A', 'B', 'C', 'E'])
In [18]: Series_4
Out[18]:
A S1B S2C S3E S4dtype: object
In [19]: df_1.append(Series_4, ignore_index=True)
Out[19]:
A B C D E0 A0 B0 C0 D0 NaN1 A1 B1 C1 D1 NaN2 A2 B2 C2 D2 NaN3 S1 S2 S3 NaN S4
출처: https://rfriend.tistory.com/tag/ignore_index=True) [R, Python 분석과 프로그래밍의 친구 (by R Friend):티스토리]
'개념 정리 > 문법 정리' 카테고리의 다른 글
JOIN함수(2022.06.15) (0) | 2022.06.15 |
---|---|
데이터 베이스 / 명령어 실행 (0) | 2022.06.14 |
튜플(tuple) (0) | 2022.06.14 |
strftime함수 (0) | 2022.06.10 |
헷갈리는 문법 정리 (0) | 2022.06.09 |