Python Pandas庫 常見使用錯誤總結

weixin_34337265發表於2016-07-16

DataFrame物件的.ix[idx] 與 .ix[[idx]] 區別

# 示例
import numpy as np
import pandas as pd

# 建立一個10*3的DataFrame物件
df = pd.DataFrame(np.random.rand(10, 3))
''' 
df:       0         1         2
0  0.619620  0.281018  0.449918
1  0.039654  0.778771  0.457885
2  0.905944  0.345189  0.859692
3  0.027250  0.676622  0.829580
4  0.230326  0.015882  0.556705
5  0.302833  0.121845  0.556206
6  0.944984  0.333118  0.291924
7  0.103753  0.688007  0.954185
8  0.326636  0.393403  0.153469
9  0.815440  0.519231  0.262114
'''

# 取出第4行(pandas.Series物件)
print(df.ix[4])
'''
0    0.230326
1    0.015882
2    0.556705
Name: 4, dtype: float64
'''

# 取出第4行,(pandas.DataFrame物件)
print(df.ix[[4]])
'''
          0         1         2
4  0.230326  0.015882  0.556705
'''

相關文章