Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
software_development:python_pandas [2022/08/04 05:40] – prgram | software_development:python_pandas [2025/07/07 14:12] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== python pandas ====== | ====== python pandas ====== | ||
{{INLINETOC}} | {{INLINETOC}} | ||
+ | |||
+ | === etc : list === | ||
+ | <code python> | ||
+ | set( [list] ) # unique value | ||
+ | [list].sort() # | ||
+ | [list1] + [list2] | ||
+ | </ | ||
+ | |||
+ | ===== shape of df ===== | ||
+ | === Pivot_table === | ||
+ | <code python> | ||
+ | df.pivot_table(index=[인덱스컬럼], | ||
+ | | ||
+ | | ||
+ | | ||
+ | </ | ||
+ | * string일 때는 aggfunc=' | ||
+ | * index에 NULL 있으면 안됨 | ||
+ | == fillna == | ||
+ | <code python> | ||
+ | df[[' | ||
+ | </ | ||
+ | |||
+ | === group by === | ||
+ | <code python> | ||
+ | df.groupby([컬럼들]).agg({' | ||
+ | |||
+ | df.groupby([COLUMNS])[' | ||
+ | |||
+ | df = df.assign(date=pd.to_numeric(df[' | ||
+ | |||
+ | df = df[[' | ||
+ | df.columns = df.columns.droplevel() | ||
+ | </ | ||
+ | |||
+ | === rank === | ||
+ | <code python> | ||
+ | df[' | ||
+ | </ | ||
+ | |||
+ | === merge === | ||
+ | <code python> | ||
+ | df_out = df_out.merge(df, | ||
+ | </ | ||
+ | |||
+ | |||
+ | ===== modify ===== | ||
+ | === Series to DF === | ||
+ | <code python> | ||
+ | df = df.append(pd.DataFrame(pd.Series(dict_row)).transpose(), | ||
+ | </ | ||
+ | |||
+ | |||
+ | === rename === | ||
+ | <code python> | ||
+ | df.rename(columns = {' | ||
+ | df.rename({1: | ||
+ | |||
+ | df.columns = [컬럼들..] | ||
+ | df.columns = [' | ||
+ | </ | ||
+ | |||
+ | === order of columns === | ||
+ | <code python> | ||
+ | #1 | ||
+ | df = df.sort_index(axis=' | ||
+ | #2 | ||
+ | df.columns | ||
+ | col_order = [' | ||
+ | df = df.reindex(col_order, | ||
+ | </ | ||
+ | |||
+ | |||
+ | === map === | ||
+ | <code python> | ||
+ | df[' | ||
+ | </ | ||
+ | |||
+ | |||
+ | ===== get info ===== | ||
=== Shapes === | === Shapes === | ||
Line 13: | Line 93: | ||
df.loc[df.sex==' | df.loc[df.sex==' | ||
+ | |||
+ | === type === | ||
+ | <code python> | ||
+ | df[' | ||
+ | df[' | ||
+ | </ | ||
+ | |||
+ | |||
+ | ===== get element ===== | ||
=== Selects === | === Selects === | ||
Line 18: | Line 107: | ||
iloc: Select by position | iloc: Select by position | ||
loc: Select by label | loc: Select by label | ||
+ | | ||
+ | df.loc[:, | ||
+ | |||
+ | df[~( df[' | ||
+ | df.loc[~( df[' | ||
</ | </ | ||
+ | === row iteration === | ||
+ | <code python> | ||
+ | for idx,row in anime[: | ||
+ | print(idx, row) | ||
+ | </ | ||
| | ||
| | ||
- | ====I/O file==== | + | =====I/O file===== |
+ | |||
+ | === encoding_errors - ' | ||
+ | Encoding 제대로 했는데도 안되면.. | ||
+ | 공공데이터가 이런 경우가 많음. | ||
+ | |||
+ | Error tokenizing data. C error: EOF inside string starting at row 0 | 판다스 에러 | ||
+ | https:// | ||
+ | quoting=csv.QUOTE_NONE 파라미터 | ||
+ | |||
+ | <code python> | ||
+ | import chardet | ||
+ | with open(file, ' | ||
+ | result = chardet.detect(rawdata.read(100000)) | ||
+ | result | ||
+ | |||
+ | |||
+ | data = pd.read_csv( file, encoding=' | ||
+ | # on_bad_lines=' | ||
+ | # error_bad_lines=False | ||
+ | </ | ||
+ | |||
+ | === to_numberic === | ||
+ | <code python> | ||
+ | #1 | ||
+ | df = pd.read_csv(' | ||
+ | #2 | ||
+ | df[' | ||
+ | </ | ||
=== Excel === | === Excel === | ||
Line 61: | Line 188: | ||
file open/ append lines : https:// | file open/ append lines : https:// | ||
- | === row iteration === | ||
- | <code python> | ||
- | for idx,row in anime[: | ||
- | print(idx, row) | ||
- | </ | ||
- | === Series to DF === | ||
- | <code python> | ||
- | df = df.append(pd.DataFrame(pd.Series(dict_row)).transpose(), | ||
- | </ | ||
- | === rename === | ||
- | <code python> | ||
- | df.rename(columns = {' | ||
- | df.rename({1: | ||
- | df.columns = [컬럼들..] | ||
- | df.columns = [' | ||
- | </ | ||
- | === map === | ||
- | <code python> | ||
- | df[' | ||
- | </ | ||
- | === Pivot_table === | ||
- | <code python> | ||
- | df.pivot_table(index=[인덱스컬럼], | ||
- | | ||
- | | ||
- | | ||
- | </ | ||
- | === group by === | ||
- | <code python> | ||
- | df.groupby([컬럼들]).agg({' | ||
- | |||
- | df = df.assign(date=pd.to_numeric(df[' | ||
- | |||
- | df = df[[' | ||
- | df.columns = df.columns.droplevel() | ||
- | </ | ||
- | |||
- | === merge === | ||
- | <code python> | ||
- | df_out = df_out.merge(df, | ||
- | </ | ||
- | |||
- | === rank === | ||
- | <code python> | ||
- | df[' | ||
- | </ | ||
- | === to_numberic === | ||
- | <code python> | ||
- | #1 | ||
- | df = pd.read_csv(' | ||
- | #2 | ||
- | df[' | ||
- | </ | ||
- | |||
- | |||
- | |||
- | === type === | ||
- | <code python> | ||
- | df[' | ||
- | df[' | ||
- | </ | ||
{{tag> | {{tag> |