import pandas as pd
import numpy as np
from lib_001_decorator_log_funcname import decorator_log_funcname
from env_config import *
csv_a = data_path + 'a.csv'
csv_b = data_path + 'b.csv'
csv_r = data_path + 'r.csv'
csv_t = data_path + 't.csv'
csv_p = data_path + 'p.csv'
csv_x = data_path + 'x.csv'
df = pd.DataFrame()
print(type(df))
df_x = pd.read_csv(csv_x)
df_p = pd.read_csv(csv_p)
@decorator_log_funcname
def _test_load_csv():
global df
df = pd.read_csv( csv_a, names=["col1","col2","col3"] )
print(type(df))
print(df)
@decorator_log_funcname
def _test_dump_csv():
df.to_csv(csv_r)
@decorator_log_funcname
def _test_load_table_csv():
df = pd.read_csv(csv_t, index_col='rowtitle' )
print(df)
@decorator_log_funcname
def _test_load_panda_csv():
global df
df = pd.read_csv(csv_p)
print(df)
@decorator_log_funcname
def _test_dump_panda_csv():
global df
print(df)
df.to_csv(csv_r, index=False)
@decorator_log_funcname
def _test_load_panda_csv_change_colname():
global df
lname = ["g1","g2","g3"]
df = pd.read_csv(csv_p, header=0, names=lname )
#? for del header, header=0
# for add header, names=lname
print(df)
@decorator_log_funcname
def _test_df_count_col():
pass
global df_p
print(len(df_p.columns.to_list()))
@decorator_log_funcname
def _test_df_count_row():
global df_p
row = df_p.count()
print(type(row))
print(row)
print(row.to_list())
rr = df_p['col1'].count()
print(rr)
@decorator_log_funcname
def _test_df_insert_row():
df1 = df_p
dfi = pd.DataFrame( [[44,55,66],], columns=['col1','col2','col3'] )
print('dfi==\n',dfi)
xx = _func_df_insert_row(1,df1,dfi)
print("xx==\n",type(xx), '\n', xx )
@decorator_log_funcname
def _func_df_insert_row(idx, df_base, df_insert):
df1 = df_base[:idx]
df2 = df_base[idx:]
dff = df1.append(df_insert).append(df2).reset_index(drop=True)
return dff
@decorator_log_funcname
def _func_df_insert_row_at_0(df_base, ls_in):
df_base.loc[-1] = ls_in
df_base.index = df_base.index + 1
print(type(df_base.index))
print(df_base.index)
print(df_base)
df_base.sort_index( inplace=True)
print(df_base)
return df_base
@decorator_log_funcname
def _test_df_update_append_row():
df1 = df_p
print(df1)
df1.loc[0] = ['11','22','33']
print(df1)
df1.loc[2] = ['33','333','3333']
print(df1)
@decorator_log_funcname
def _test_df_get_some_rows_as_sub_df():
df1 = df_p
print(df1)
print(df1[0:1])
@decorator_log_funcname
def _test_df_del_row():
global df_p
df1 = df_p
df2 = df1.drop([1])
print(df2)
print(df1)
print(df_p)
df1.drop([1], inplace=True)
print(df1)
pass
@decorator_log_funcname
def _test_df_update_col():
df1 = df_p
print(df1)
df1['col1'] = 88
print(df1)
df1['col2'] = [55,44]
print(df1)
print(df1.index.stop) #
@decorator_log_funcname
def _test_df_insert_col():
global df_p
print(df_p)
df1 = df_p
ll = [10,11]
print(df1)
df1.insert( loc=0, column='col0', value=ll)
print(df1)
@decorator_log_funcname
def _test_df_del_col():
global df_p
print(df_p)
df1 = df_p
print(df1)
df1.drop('col1', axis=1, inplace=True)
print(df1)
@decorator_log_funcname
def _test_df_del_row_with_drop():
global df_p
print(df_p)
df1 = df_p
print(df1)
df1.drop(1, axis=0, inplace=True)
print(df1)
@decorator_log_funcname
def _test_df_del_col_with_del():
global df_p
print(df_p)
df1 = df_p
print(df1)
del df1['col1']
print(df1)
@decorator_log_funcname
def _test_df_set_cell():
global df_p
print(df_p)
df1 = df_p
print(df1)
df1.loc[1,'col2'] = 88
print(df1)
@decorator_log_funcname
def _test_df_check_NaN():
global df_p
print(df_p)
df1 = df_p
print(df1)
df1.loc[1,'col2'] = np.nan
print(df1)
@decorator_log_funcname
def _test_df_get_sub_col():
global df_p
print(df_p)
df1 = df_p
print(df1)
df2 = df1[['col1','col3']]
print(df2)
@decorator_log_funcname
def _test_df_get_sub_row():
pass
@decorator_log_funcname
def _test_df_set_col_type():
global df_p
print(df_p)
df1 = df_p
print(df1)
print(df1.dtypes)
df1['col1'] = df1['col1'].astype(float)
print(df1)
print(df1.dtypes)
@decorator_log_funcname
def _test_df_sort_value():
global df_p
print(df_p)
df1 = df_p.sort_values( 'col1', ascending=False )
print(df_p)
print(df1)
@decorator_log_funcname
def _test_df_get_header():
print(df_p)
head = df_p.columns.to_list()
print(type(head))
print(head)
pass
@decorator_log_funcname
def _test_other():
# global df
# df = pd.read_csv(csv_x)
# print(df)
# print(len(df.columns.to_list()))
# print(df.columns.to_list())
#df.to_csv(csv_r, index=False)
pass
@decorator_log_funcname
def _test( ):
#_test_load_csv()
#_test_dump_csv()
#_test_load_table_csv()
#_test_load_panda_csv()
#_test_dump_panda_csv()
#_test_load_panda_csv_change_colname()
#_test_df_count_col()
# _test_df_count_row()
# _test_df_insert_row()
# _func_df_insert_row_at_0(df_p,[100,101,102])
# _test_df_update_append_row()
#_test_df_get_some_rows_as_sub_df()
# _test_df_del_row()
#_test_df_update_col()
#_test_df_insert_col()
# _test_df_del_col()
# _test_df_del_row_with_drop()
# _test_df_del_col_with_del()
# _test_df_set_cell()
# _test_df_check_NaN()
# _test_df_get_sub_col()
_test_df_set_col_type()
# _test_df_sort_value()
#_test_df_get_header()
# _test_other()
if __name__ == '__main__':
_test()