In [1]:
import pandas as pd
import numpy as np
fin_nm = input('Input File (*.csv) = ')
dframe = pd.read_csv(fin_nm)
fout_nm = input('Output file name (*.txt) = ')
fout = open(fout_nm, 'w')
fout.write(f'Input File = {fin_nm}\n\n')
print(dframe.keys())
print('\n', dframe)
fout.write(f'\ndframe =\n{dframe}')
Index(['ID', 'Q1', 'Q2', 'Q3', 'Q4', 'Q5', 'Q6', 'Q7'], dtype='object') ID Q1 Q2 Q3 Q4 Q5 Q6 Q7 0 0 3 3 3 3 3 0 0 1 39D 3 1 3 3 1 10 0 2 40D 3 2 2 2 1 40 0 3 41D 3 2 2 2 1 0 90 4 42D 2 1 2 2 1 60 80 5 43D 2 2 2 2 1 80 0 6 44D 1 2 2 2 1 80 80 7 45D 3 1 2 2 1 20 10 8 46D 2 2 2 3 1 20 0 9 47D 3 1 3 3 1 20 50 10 48D 1 2 2 2 1 60 45 11 49D 2 2 1 3 1 50 50 12 50D 2 2 2 2 2 80 30 13 51D 2 3 3 2 2 80 80 14 52D 3 1 2 2 1 80 75 15 53D 3 2 1 2 1 90 60 16 54D 2 2 2 2 3 50 50 17 55D 3 1 1 2 1 50 50 18 56D 3 3 1 1 1 -20 0 19 57D 1 1 1 2 2 70 50 20 58D 2 2 2 2 1 0 0 21 59A 2 1 2 2 1 80 80 22 60A 1 2 2 2 3 80 40 23 61A 2 2 1 2 1 70 50 24 62A 1 2 2 1 1 90 50 25 63A 1 3 3 3 1 0 0 26 64A 3 1 1 1 1 100 90 27 65A 1 2 3 3 1 90 80 28 66A 2 2 3 3 2 -100 -100 29 67A 1 2 2 2 1 80 50 30 68A 1 2 2 2 2 100 80 31 69A 1 2 2 2 3 65 25 32 70A 2 2 2 3 1 60 60 33 71A 2 1 1 2 1 50 50 34 72A 2 3 3 3 2 -90 0 35 73A 2 2 3 2 1 50 70 36 74A 1 2 2 2 1 60 70 37 75A 3 2 3 2 1 80 100 38 76A 3 3 2 2 3 0 -10 39 77A 1 2 2 2 1 50 0
Out[1]:
1567
In [2]:
item_1 = input('Item-1 = ').split()
item_2 = input('Item-2 = ').split()
v1 = [v[0] for v in dframe[item_1].values[1:]]
v2 = [v[0] for v in dframe[item_2].values[1:]]
item_1 = str(item_1)
item_2 = str(item_2)
dframe = pd.DataFrame({item_1: v1, item_2: v2})
print(dframe)
fout.write(f'\ndframe =\n{dframe}')
['Q1'] ['Q2'] 0 3 1 1 3 2 2 3 2 3 2 1 4 2 2 5 1 2 6 3 1 7 2 2 8 3 1 9 1 2 10 2 2 11 2 2 12 2 3 13 3 1 14 3 2 15 2 2 16 3 1 17 3 3 18 1 1 19 2 2 20 2 1 21 1 2 22 2 2 23 1 2 24 1 3 25 3 1 26 1 2 27 2 2 28 1 2 29 1 2 30 1 2 31 2 2 32 2 1 33 2 3 34 2 2 35 1 2 36 3 2 37 3 3 38 1 2
Out[2]:
769
In [3]:
xtable = pd.crosstab(dframe[item_1],dframe[item_2])
print(xtable)
xtable = xtable.values
print('\n', xtable)
fout.write(f'\nxtable =\n{xtable}')
['Q2'] 1 2 3 ['Q1'] 1 1 10 1 2 3 10 2 3 6 4 2 [[ 1 10 1] [ 3 10 2] [ 6 4 2]]
Out[3]:
46
In [4]:
r_order = input('Row order = ')
r_order = np.array(r_order.split(), dtype=int)
print('r_order =', r_order)
print('r_order - 1 =', r_order - 1)
x_table_r = xtable[r_order - 1] # Rearrange rows
print('x_table_r =\n', x_table_r)
r_order = [1 2 3] r_order - 1 = [0 1 2] x_table_r = [[ 1 10 1] [ 3 10 2] [ 6 4 2]]
In [5]:
c_order = input('c_order = ')
c_order = np.array(c_order.split(), dtype=int)
x_table_c = xtable[:,c_order - 1] # Rearrange columns
print('x_table_c =\n', x_table_c)
fout.write(f'\nr_order =\n{r_order}\nx_table_r =\n{x_table_r}\n')
fout.write(f'c_order =\n{c_order}\nx_table_c =\n{x_table_c}\n')
x_table_rc = xtable[r_order - 1][:, c_order -1]
print('\nr_order =', r_order, '\nc_order =', c_order)
print('x_table_rc =\n', x_table_rc)
fout.write(f'\nr_order = {r_order}\nc_order = {c_order}')
fout.write(f'\nx_table_rc =\n{x_table_rc}')
fout.close()
print(f'\n{fout_nm} was saved.\n')
x_table_c = [[ 1 1 10] [ 3 2 10] [ 6 2 4]] r_order = [1 2 3] c_order = [1 3 2] x_table_rc = [[ 1 1 10] [ 3 2 10] [ 6 2 4]] temp.txt was saved.
In [ ]: