def delete_record(id_): """ Deletes one record based on it's ID. If the ID given doesn't exist, nothing will be deleted. """ df = utils.read_dataframe() df_altered = df[df['ID'] != id_] utils.save_dataframe(data=df_altered) return None
def add_record(name, age, fav_sport): """Adds one record to the database (CSV file)""" df = utils.read_dataframe() df_record_to_add = pd.DataFrame(data={ "ID": utils.generate_random_id(), "Name": name, "Age": age, "FavouriteSport": fav_sport }, index=[0]) df_concatenated = pd.concat(objs=[df, df_record_to_add], ignore_index=True, sort=False) utils.save_dataframe(data=df_concatenated) return None
def generate_first_file_hash(): index = 1 for i in range(1,11): df = read_dataframe('2016-04-01', i) df['hash'] = '000000' for j in df.index: df.set_value(j, 'hash', '{:06d}'.format(index)) index += 1 df.to_csv('/home/bobby/Documents/code/python/qishi/all_csv_with_hash/catagory{}/2016-04-01.csv'.format(i), header = False, index=False) # this index is the not used next index return index
def get_record(id_): """ Gets one record (dictionary) based on it's ID. If the ID given doesn't exist, returns empty dictionary. """ df = utils.read_dataframe() df_record = df[df['ID'] == id_] if df_record.empty: return {} if len(df_record) == 1: dict_record = df_record.iloc[0].to_dict() dict_record['Age'] = int(dict_record['Age']) return dict_record raise Exception("Multiple records with same ID exists")
def process(self, catagory): self.catagory = catagory last_date_data = read_dataframe_with_hash(self.start_date, self.catagory) for current_date in date_range((self.start_date+timedelta(1)), self.end_date): current_date_data = read_dataframe(current_date, self.catagory) if current_date_data.empty: continue else: current_date_data['hash'] = '000000' solver = Solver(last_date_data, current_date_data, current_date, self.max_index) self.max_index += solver.process() solver.write_csv(self.catagory) last_date_data = current_date_data self.logger.info('{}: {}data have been completed'.format(datetime.now(), current_date)) print(self.max_index)
def update_record(id_, name=None, age=None, fav_sport=None): """ Updates one record based on it's ID, and some specified parameters. If the ID given doesn't exist, nothing will be updated. >>> update_record(id_="U0E4CXQSWC4S", name="SomeNewName", age=20, fav_sport="SomeNewFavouriteSport") """ df = utils.read_dataframe() df_record_to_update = df[df['ID'] == id_] if df_record_to_update.empty: return None if len(df_record_to_update) == 1: if name: df.loc[df['ID'] == id_, 'Name'] = name if age: df.loc[df['ID'] == id_, 'Age'] = age if fav_sport: df.loc[df['ID'] == id_, 'FavouriteSport'] = fav_sport utils.save_dataframe(data=df) return None raise Exception("Multiple records with same ID exists")
def get_filtered_records(id_=None, name=None, age=None, fav_sport=None, min_age=None, max_age=None): """Gets list of records filtered by criteria specified""" df = utils.read_dataframe() if id_: df = df[df['ID'] == id_] if name: df = df[df['Name'] == name] if age: df = df[df['Age'] == age] if fav_sport: df = df[df['FavouriteSport'] == fav_sport] if min_age: df = df[df['Age'] >= min_age] if max_age: df = df[df['Age'] <= max_age] if df.empty: return [] return df.to_dict(orient='records')
import pandas import sys import utils def normalize(dataframe): return (dataframe - dataframe.mean()) / dataframe.std() if __name__ == "__main__": filename = sys.argv[1] dataframe = utils.read_dataframe(filename) for value in normalize(dataframe).values: print value[0], value[1], value[2]
def get_all_records(): df = utils.read_dataframe() return df.to_dict(orient='records')