Every line of 'convert dataframe to csv file python' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.
57 def _dataframe_to_txt(writer, dataframe): 58 encoding_writer = codecs.getwriter('utf-8')(writer) 59 for row in dataframe.iterrows(): 60 encoding_writer.write("".join(row[1].tolist())) 61 encoding_writer.write('\n')
35 def csvToDataFrame(self, sqlCtx, rdd, columns=None, sep=",", parseDate=True): 36 """Converts CSV plain text RDD into SparkSQL DataFrame (former SchemaRDD) 37 using PySpark. If columns not given, assumes first row is the header. 38 If separator not given, assumes comma separated 39 """ 40 if self.py_version < 3: 41 def toRow(line): 42 return self.toRowSep(line.encode('utf-8'), sep) 43 else: 44 def toRow(line): 45 return self.toRowSep(line, sep) 46 47 rdd_array = rdd.map(toRow) 48 rdd_sql = rdd_array 49 50 if columns is None: 51 columns = rdd_array.first() 52 rdd_sql = rdd_array.zipWithIndex().filter( 53 lambda r_i: r_i[1] > 0).keys() 54 column_types = self.evaluateType(rdd_sql, parseDate) 55 56 def toSqlRow(row): 57 return self.toSqlRowWithType(row, column_types) 58 59 schema = self.makeSchema(zip(columns, column_types)) 60 61 return sqlCtx.createDataFrame(rdd_sql.map(toSqlRow), schema=schema)
290 def dict_to_csv(data=None, filename='test.csv'): 291 r''' 292 Utility function to write a dictionary to a csv file 293 Uses dictionary keys as column headers 294 Data should all be same length 295 ''' 296 data_list = [data[key] for key in data.keys()] 297 zipper = list(zip(*data_list)) 298 with open(filename, 'w') as f: 299 w = csv.writer(f, lineterminator='\n') 300 w.writerow(data.keys()) 301 w.writerows(zipper)
53 def row2_dict2csv(raw_dict = {}, csv_file = ""): 54 with open(csv_file,'w') as f: 55 w = csv.writer(f) 56 for k,v in raw_dict.items(): 57 w.writerows([k,v])
37 def readcsv(filename, header=True): 38 return pd.read_csv(filename, header=None) if not header else pd.read_csv(filename)
8 def convert(csv_path): 9 with open(csv_path) as f: 10 data = Data(f) 11 12 table = Table() 13 table.set_header(["procedure", "time [s]", "speedup", ""]) 14 15 reference_time = None 16 for name, time in data: 17 if reference_time is None: 18 reference_time = time 19 20 speedup = reference_time/time 21 22 time = '%0.5f' % time 23 bar = unicode_bar(speedup * 10) 24 speedup = '%0.2f' % speedup 25 table.add_row([name, time, speedup, bar]) 26 27 def get_path(): 28 basename = os.path.splitext(os.path.basename(csv_path))[0] 29 return basename + ".txt" 30 31 path = get_path() 32 with open(path, 'wt', encoding='utf-8') as f: 33 f.write(unicode(table)) 34 35 print "%s created" % path
554 def _csv_to_pandas_df(filepath, 555 separator=DEFAULT_SEPARATOR, 556 quote_char=DEFAULT_QUOTE_CHARACTER, 557 escape_char=DEFAULT_ESCAPSE_CHAR, 558 contain_headers=True, 559 lines_to_skip=0, 560 date_columns=None, 561 rowIdAndVersionInIndex=True): 562 test_import_pandas() 563 import pandas as pd 564 565 # DATEs are stored in csv as unix timestamp in milliseconds 566 def datetime_millisecond_parser(milliseconds): return pd.to_datetime(milliseconds, unit='ms', utc=True) 567 568 if not date_columns: 569 date_columns = [] 570 571 line_terminator = str(os.linesep) 572 573 df = pd.read_csv(filepath, 574 sep=separator, 575 lineterminator=line_terminator if len(line_terminator) == 1 else None, 576 quotechar=quote_char, 577 escapechar=escape_char, 578 header=0 if contain_headers else None, 579 skiprows=lines_to_skip, 580 parse_dates=date_columns, 581 date_parser=datetime_millisecond_parser) 582 if rowIdAndVersionInIndex and "ROW_ID" in df.columns and "ROW_VERSION" in df.columns: 583 # combine row-ids (in index) and row-versions (in column 0) to 584 # make new row labels consisting of the row id and version 585 # separated by a dash. 586 zip_args = [df["ROW_ID"], df["ROW_VERSION"]] 587 if "ROW_ETAG" in df.columns: 588 zip_args.append(df['ROW_ETAG']) 589 590 df.index = row_labels_from_id_and_version(zip(*zip_args)) 591 del df["ROW_ID"] 592 del df["ROW_VERSION"] 593 if "ROW_ETAG" in df.columns: 594 del df['ROW_ETAG'] 595 596 return df
1153 def to_csv(self, file_name, sep=',', encoding=None): 1154 start = time.time() 1155 init = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss 1156 1157 self._data.to_csv(file_name, sep, encoding) 1158 1159 finish = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss 1160 self._last_operation_dict['time'] = time.time() - start 1161 self._last_operation_dict['name'] = 'to_csv' 1162 self._last_operation_dict['mem_usage'] = finish - init
305 def to_csv(self, f): 306 is_path = False 307 if isinstance(f, str): 308 is_path = True 309 f = open(f, mode='w') 310 writer = csv.DictWriter(f, self.columns) 311 writer.writerows(self._data) 312 if is_path: 313 f.close()
75 def writecsv(filename, rows, separator="\t", encoding="utf-8-sig"): 76 """Write the rows to the file. 77 78 :param filename: (str) 79 :param rows: (list) 80 :param separator: (str): 81 :param encoding: (str): 82 83 """ 84 with codecs.open(filename, "w+", encoding) as f: 85 for row in rows: 86 tmp = [] 87 for s in row: 88 if isinstance(s, (float, int)): 89 s = str(s) 90 else: 91 s = '"%s"' % s 92 tmp.append(s) 93 f.write('%s\n' % separator.join(tmp))