[파이썬] pandas DataFrame에 Color 적용
pandas DataFrame에 style을 사용해서 color를 적용할 수 있습니다. style.highlight_min()와 style.highlight_max() 를 사용하는 것을 볼 수 있습니다. 1개만 사용할 수 있고, 여러개를 사용 시 계속 붙여서 적용할 수 있습니다. import pandas as pd dict_item = { 'item1': [0, 1, 0, 0], 'item2': [1, 0, 1, 0], 'item3': [1, 0, 0, 1] } df_item = pd.DataFrame(dict_item) df_style = df_item.style.highlight_min(axis=0, color='blue').highlight_max(axis=0, color='red') df_style
2022. 10. 4.
[파이썬] pandas Dataframe을 이미지로 저장
package 설치 pip install dataframe_image 다음과 같이 dataframe을 이미지로 자장 할 수 있습니다. import pandas as pd import dataframe_image as dfi dict_item = { 'item1': [0, 1, 0, 0], 'item2': [1, 0, 1, 0], 'item3': [1, 0, 0, 1] } df_item = pd.DataFrame(dict_item) print(df_item) dfi.export(df_item, 'pandasToimage01.png', max_cols=-1, max_rows=-1)
2022. 10. 4.
[파이썬] pandas 컬럼이 null 이면 다른 컬럼값 변경
Column의 값이 null 일 때, 다른 Column의 값을 변경하고자 합니다. import pandas as pd import numpy as np raw_data = {'age': ['101', '105', '103', '107','108', np.nan,'110','111','112','113'], 'check': ['', '', '', '', '', '', '', '', '', '']} df = pd.DataFrame(raw_data) df 중간에 Null 값이 있는 것을 확인할 수 있습니다. age값이 null 일 때, check Column의 값을 변경하고자 한다면 df.loc[df['age'].isna(), 'check'] = 0
2022. 9. 22.
[파이썬] pandas 에러(KeyError(f'{not_found} not in index'))
pandas의 get_dummies 사용하는데 다음과 같은 에러가 발생했습니다. import pandas as pd import numpy as np dif2 = pd.DataFrame({'R': ['a', 'c', 'd'], 'T': ['d', 'a', 'c'], 'S': [1, 2, 3]}) pd.get_dummies(dif2, columns=['T','A'], drop_first=True) KeyError(f"{not_found} not in index") 원인 - 없는 column을 columns에 지정하려고 할 때 발생합니다. 위 코드에서 'A' column이 없는데 columns에 지정해서 에러가 발생했습니다.
2022. 9. 6.
[파이썬] pandas DataFrame loc(null값 조회)
새로운 DataFrame을 만듭니다. import pandas as pd raw_data = {'col0': ['a1', 'a2', 'a3', None], 'col1': ['a10', 'a20', 'a30', 'a40'], 'col2': ['a100', None, 'a300', None]} df = pd.DataFrame(raw_data) isna로 null 값을 조회할 수 있습니다. df.loc[df['col0'].isna()] 2개의 column에서 조회는 다음과 같이 합니다. '&' (and)를 사용합니다. 같은 Row에서의 조건이 됩니다. df.loc[df['col0'].isna() & df['col2'].isna()] '|' (or) 사용해서 각각의 다른 Row의 조건을 검색할 수 있습니다. d..
2022. 8. 31.
[파이썬] pandas 행열(Column, Row) 전환
예제로 사용할 데이터를 만듭니다. from pandas import Series, DataFrame raw_data = {'col0': [1, 2, 3, 4], 'col1': [10, 20, 30, 40], 'col2': [100, 200, 300, 400]} data = DataFrame(raw_data) transpose()를 사용하면 Column, Row가 전환됩니다. data2 = data.transpose() Column명을 변경합니다. data2.columns = ['col1','col2','col3','col4'] index를 조회해보면 아래와 같이 나옵니다. 원데이터의 Column 명으로 되어 있습니다. data2.index index 즉 원데이터의 Column명을 다시 'name'이라는 ..
2022. 8. 25.
[파이썬] pandas, DataFrame에서 max, min 찾기
pandas DataFrame 데이터에서 Max 값을 찾아봅니다. import pandas as pd df = pd.DataFrame({'A1': [1, 2, 2, 3], 'A2': [4, 3, 8, 4]}) print("DataFrame:") print(df) maxs = df.max() print("\n") print("Max Column:") print(maxs) 2개 값을 평균값을 사용해서 mean, max, min 값을 찾습니다. 이를 사용해서 주식데이터의 5일 이동평균서의 Open, High, Low 값을 찾을 수 있습니다. df = pd.DataFrame({'A1': [1, 2, 3, 4, 5, 6, 7, 8, 9], 'A2': [11, 12, 13, 14, 15, 16, 17, 18, 19..
2022. 8. 11.