[英]How to print index (Row number) while conversion error during json.loads Panda Dataframe
[英]Strange error during conversion from Panda Dataframe to numpy array
我有一個帶有兩列的熊貓數據框:“評論”(文本)和“情感”(1/0)
X_train = df.loc[0:25000, 'review'].values
y_train = df.loc[0:25000, 'sentiment'].values
X_test = df.loc[25000:, 'review'].values
y_test = df.loc[25000:, 'sentiment'].values
但是在轉換為numpy數組之后,使用values()
方法。 我得到以下形狀的numpy數組:
print(df.shape) #(50000, 2)
print(X_train.shape) #(25001,)
print(y_train.shape) #(25001,)
print(X_test.shape) # (25000,)
print(y_test.shape) # (25000,)
這樣就可以看到values()
方法,又增加了一行。 這真的很奇怪,我無法檢測到錯誤。
df.loc
基於標簽,即包括上限。 使用iloc
:
df.iloc[:25000, 1].values # here 1 is the column of 'review' for example
如果您想要類似NumPy的切片。
使用iloc
您需要將行和列都提供為整數或整數切片。
>>> df = pd.DataFrame({'a': [1, 2, 3], 'b': [4, 5, 6]})
>>> df
a b
0 1 4
1 2 5
2 3 6
這是基於標簽的,即包括上限在內:
>>> df.loc[:1, 'a']
0 1
1 2
Name: a, dtype: int64
這就像在NumPy中切片一樣,即上限互斥:
>>> df.iloc[:2, 0]
0 1
1 2
Name: a, dtype: int64
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.