반응형
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- dataframe
- Mac
- PostgreSQL
- Python
- Excel
- Java
- array
- Google Excel
- string
- Kotlin
- google apps script
- PySpark
- math
- matplotlib
- Tkinter
- hive
- SQL
- gas
- 파이썬
- numpy
- list
- Apache
- django
- Google Spreadsheet
- c#
- GIT
- Github
- Redshift
- PANDAS
Archives
- Today
- Total
달나라 노트
Python Pandas : columns (DataFrame의 column 정보 불러오기.) 본문
Python/Python Pandas
Python Pandas : columns (DataFrame의 column 정보 불러오기.)
CosmosProject 2024. 6. 28. 19:01728x90
반응형
Pandas의 columns는 DataFrame에 존재하는 Column의 정보를 출력해줍니다.
import pandas as pd
dict_test = {
'col1': [1, 2, 3, 4, 5],
'col2': [2, 3, 4, 5, 6],
}
df_test = pd.DataFrame(dict_test)
print(df_test)
print(df_test.columns)
-- Result
col1 col2
0 1 2
1 2 3
2 3 4
3 4 5
4 5 6
Index(['col1', 'col2'], dtype='object')
list() method를 이용해서 column을 list의 형태로 만들 수도 있습니다.
import pandas as pd
dict_test = {
'col1': [1, 2, 3, 4, 5],
'col2': [2, 3, 4, 5, 6],
}
df_test = pd.DataFrame(dict_test)
print(df_test)
print(list(df_test.columns))
-- Result
col1 col2
0 1 2
1 2 3
2 3 4
3 4 5
4 5 6
['col1', 'col2']
import pandas as pd
dict_test = {
'col1': [1, 2, 3, 4, 5],
'col2': [2, 3, 4, 5, 6],
}
df_test = pd.DataFrame(dict_test)
print(df_test)
print(df_test.columns)
print(df_test.columns.name)
df_test.columns.name = 'This is columns'
print(df_test)
print(df_test.columns)
print(df_test.columns.name)
-- Result
col1 col2
0 1 2
1 2 3
2 3 4
3 4 5
4 5 6
Index(['col1', 'col2'], dtype='object')
None
This is columns col1 col2
0 1 2
1 2 3
2 3 4
3 4 5
4 5 6
Index(['col1', 'col2'], dtype='object', name='This is columns')
This is columns
- df_test.columns.name = 'This is columns'
또한 이런식으로 Column set의 이름을 조절할 수 있습니다.
column set의 이름은 기본적으로 None이며
이를 별도로 설정할 수 있습니다.
import pandas as pd
dict_test = {
'col1': [1, 2, 3, 4, 5],
'col2': [2, 3, 4, 5, 6],
}
df_test = pd.DataFrame(dict_test)
print(df_test)
print(df_test.columns)
print(df_test.columns.name)
df_test.columns.name = 'This is columns'
print(df_test)
print(df_test.columns)
print(df_test.columns.name)
df_test.columns.name = None
print(df_test)
print(df_test.columns)
print(df_test.columns.name)
-- Result
col1 col2
0 1 2
1 2 3
2 3 4
3 4 5
4 5 6
Index(['col1', 'col2'], dtype='object')
None
This is columns col1 col2
0 1 2
1 2 3
2 3 4
3 4 5
4 5 6
Index(['col1', 'col2'], dtype='object', name='This is columns')
This is columns
col1 col2
0 1 2
1 2 3
2 3 4
3 4 5
4 5 6
Index(['col1', 'col2'], dtype='object')
None
- df_test.columns.name = None
name을 없애고싶으면 위처럼 None을 할당하면 됩니다.
728x90
반응형
'Python > Python Pandas' 카테고리의 다른 글
Python Pandas : cumsum, cumprod (누적합, 누적곱) (0) | 2024.08.01 |
---|---|
Python Pandas : SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. (0) | 2024.07.04 |
Python Pandas : Percentile Rank 계산하기 (백분위 계산하기) (0) | 2024.03.25 |
Python Pandas & openpyxl : alignment (텍스트 정렬) (0) | 2024.02.27 |
Python Pandas & openpyxl : column width autofit (column 너비 자동맞춤) (0) | 2024.02.26 |