-
Notifications
You must be signed in to change notification settings - Fork 2
/
ConverS.py
103 lines (80 loc) · 2.18 KB
/
ConverS.py
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# -*- coding: utf-8 -*-
#!/usr/bin/python
# Author: Niam Moltta
# UY - 2017
# MIT License
# Converting str('Clave') to int ('value')
import math
import re
import numpy as np
import pandas as pd
print ' '
print ' '
print ' '
print ' Welcome to ConverS.py'
print ' --by Niam Moltta--'
print ' ~~/\//V\ '
print ' '
print ' '
print ' '
print 'Application: STRINGS TO NUMBERS TRANSFORMATION.\n\nINSTRUCTIONS:\n\n-You need to modify the code itself in order to convert your own data.\n\n'
# I wrote this to transform strings to numbers so I could analyze them.
# I used it to set up my ProA score ("Pro Environment" from words in spanish).
fhand = raw_input('File name: ')
filecsv = str(fhand)
if fhand == '':
print ' '
print 'Arrivederci!'
print ' '
exit()
data = pd.read_csv(fhand)
print ' '
frame = pd.DataFrame(data)
coolist = frame.columns
columns = np.asarray(coolist)
print ' '
print 'Columns in', re.findall('(.+?).csv', filecsv), 'are:\n'
print columns
print ' '
hand = raw_input('Enter column header:\n\n')
column = str(hand)
if (column == 'ya') | (column == ''):
print '\nHasta la vista, human.\n\n'
exit()
# Replace missing values with zeros in the selected [column]
data[column].fillna(0,inplace=True)
numbers = data[column]
keeps = 0
replaces = 0
total = 0
numeros = list() # converted list
for line in numbers:
line = str(line)
if len(line) <= 3: # values of -1 and 1 are considered
valor = str(line)
numeros.append(valor)
keeps = keeps + 1
total = total + 1
print 'Keeping value'
else : # values of 4 characters, starting with letters that need to be converted to 0
valor = int(0)
numeros.append(valor)
replaces = replaces + 1
total = total + 1
print 'Replacing value'
print ' '
print 'New list created'
print ' '
print 'Number of replaced values =', replaces
print 'Number of kept values =', keeps
print 'Total =', total
print ' '
nfile = open('ChangedValues.txt', 'w')
array = list()
for numero in numeros:
value = str(numero)
nfile.write(value)
nfile.write('\n')
nfile.close()
print 'File created as "ChangedValues.txt"'
print ' '