Python Import data dictionary and pattern -
if have data as:
code, data_1, data_2, data_3, [....], data204700 a,1,1,0, ... , 1 b,1,0,0, ... , 1 a,1,1,0, ... , 1 c,0,1,0, ... , 1 b,1,0,0, ... , 1 etc. same code different value (0, 1, ?(not known))
i need create big matrix , want analyze.
- how can import data in dictionary?
i want use dictionary column (204.700+1)
there built in function (or package) return me pattern?
(i expect percent pattern). mean 90% of 1 in column 1, 80% of in column 2.
alright going assume want in dictionary storing purposes , tell you don't want kind of data. use pandas dataframe
this how code dataframe:
import pandas pd my_file = 'file_name' df = pd.read_csv(my_file)
now don't need package returning pattern looking for, write simple algorithm returning that!
def one_percentage(data): #get total number of rows calculating percentages size = len(data) #get type grabbing correct rows x = data.columns[1] x = data[x].dtype #list of touples hold amount of 1s , column names ones = [(i,sum(data[i])) in data if data[i].dtype == x] my_dict = {} #create dictionary column names , percent x in ones: percent = x[1]/float(size) my_dict[x[0]] = percent return my_dict
now if want percent of ones in column, do:
percentages = one_percentage(df) column_name = 'any_column_name' print percentages[column_name]
now if want have every single column, can grab of column names , loop through them:
columns = [name name in percentages] name in columns: print str(percentages[name]) + "% of 1 in column " + name
let me know if need else!
Comments
Post a Comment