[英]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.