pandas的read_csv踩到的坑
read_csv要注意,如果没有设置index_col时,读出来的会在索引上方加上Unnamed:0.可以通过设置index_col来解决这个问题.
import pandas as pd import numpy as np df = pd.DataFrame(np.random.randn(5,2) ,columns=['a', 'b']) df.to_csv('2.csv') s= pd.read_csv('2.csv') #可以看出自动加了一列:Unnamed:0 print(s) print(s.columns) print(s.values) print(type(s.values), s.values.shape) s= pd.read_csv('2.csv', index_col=0) print(s)
Unnamed: 0 a b 0 0 0.917146 1.833755 1 1 1.054247 -2.106368 2 2 -1.418065 3.640767 3 3 -1.840910 -1.098465 4 4 -1.122171 0.061347 Index(['Unnamed: 0', 'a', 'b'], dtype='object') [[ 0. 0.91714592 1.83375508] [ 1. 1.0542468 -2.10636783] [ 2. -1.41806503 3.6407667 ] [ 3. -1.84091041 -1.0984646 ] [ 4. -1.12217057 0.06134697]] <class 'numpy.ndarray'> (5, 3)
a b
0 1.731023 -0.795587
1 -0.500358 0.171487
2 -1.407322 -0.213170
3 -0.226573 0.287079
4 -0.483686 -0.692467
新战场:https://blog.csdn.net/Stephen___Qin