import pandas
1. Load DataFrame from CSV File without header
1 2 3 4 5 | names = ['preg', 'plas', 'pres', 'skin', 'test', 'mass', 'pedi', 'age', 'class'] dataframe = pandas.read_csv(url, names=names) array = dataframe.values X = array[:,0:len(array[0])-1] Y = array[:,len(array[0])-1] |
2. Load DataFrame from CSV File with header
1 2 3 4 | dataframe = pandas.read_csv(url) array = dataframe.values X = array[:,0:len(array[0])-1] Y = array[:,len(array[0])-1] |
3 Make Dataframe with Random Numbers
1 2 3 | df = pd.DataFrame(np.random.randint(100, size=(100, 6)), columns=list('ABCDEF'), index=['R{}'.format(i) for i in range(100)]) |
4. Data Handling with Pandas
1 2 3 4 5 6 7 8 9 10 | print(df) print(df.head()) print(df[['C','E']].head()) print(df[df.columns[2:4]].head()) print(df.loc[:,df.columns.isin(list('ACE'))].head()) print(df.loc['R6':'R10','C':'E']) print(df.iloc[2:6,2:5]) print(df.iloc[0:6,0:2].copy()) print(pd.DataFrame(df,columns=['B','E']).head()) print(df.drop(['A','C'],axis=1).head()) |
5 Save DataFrame to CSV File (1)
1 | df.to_csv(output_file_path, sep=',', index=False) |
6. Save DataFrame to CSV File (2)
1 2 3 4 5 | with open(out_file, 'w', newline='\n' ) as csv_file: #windows only(newline='\n') w = csv.writer( csv_file ) #, quoting=csv.QUOTE_ALL) #lineterminator='\n' w.writerow(titles) for line in lines: w.writerow(line) |
댓글 없음:
댓글 쓰기