列处理——格式转换(考虑非法值)
----------------基于普通python
#first two elements of list legislators. #[['Bassett', 'Richard', '1745-04-02', 'M', 'sen', 'DE', 'Anti-Administration', '1745'], ['Bland', 'Theodorick', '1742-03-21', 'M', 'rep', 'VA', '', '1742']] #1745 is extract from 1745-04-02 as the birth year, but not all elements have valid birth years. Some birth year is ""
- 字符转换成数字格式
for row in legislators: try: row[7]=int(row[7]) except Exception: row[7]=0
----------------基于numpy
matrix: world_alcohol
bad_value = '' alcohol_consumption_float_column = None #replace the bad value of the fifth column to 0 world_alcohol[:,4][world_alcohol[:,4] == bad_value] = '0' #convert string to float alcohol_consumption_float_column = world_alcohol[:,4].astype(float) #but I don't know how to write the float value back to the matrix....