반응형
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 | 31 |
Tags
- hive
- Tkinter
- array
- numpy
- Excel
- Apache
- c#
- dataframe
- google apps script
- Github
- 파이썬
- math
- Redshift
- Mac
- PostgreSQL
- django
- matplotlib
- Kotlin
- PANDAS
- Google Excel
- gas
- list
- GIT
- Google Spreadsheet
- SQL
- PySpark
- string
- Java
- Python
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 : 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 |
Python Pandas & openpyxl : columns, rows (column에 대한 객체 얻어오기, row에 대한 객체 얻어오기) (0) | 2024.02.26 |
Python Pandas & openpyxl : font (글자 서식 설정) (2) | 2024.02.26 |
Comments