2017-05-24 44 views
0

我有pandas.DataFrame,我只对最后一列的值感兴趣。Handeling在熊猫中的特定列的形状

np.shape(dataframe.iloc[:,:]) # the output is (2190,460) 
# Now here is the shape of one cell in the last column 

np.shape(dataframe.iloc[0,-1]) # the output is (20,) 

dataframe.iloc[0,-1] # the output [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 

我的问题是如何能得到此列保存在以下形态:(2190, 20) 自运行:

np.shape(dataframe.iloc[:,-1]) # the output (2190,) 

而这种形状使用造成了我一个巨大的问题

解决方案loop:

Test_Labels = [] 
for i in range(len(dataframe)): 
    Test_Labels.append(dataframe.iloc[i,-1]) 
np.shape(Test_Labels) 

如果有人能用熊猫功能解决它,很高兴看到它。

+0

'数据帧[dataframe.columns [-1]' –

+0

@StephenRauch你测试? – Engine

+0

在一个小的(2x2)数据帧上。 –

回答

0

你可以从最后一列一个pandas.DataFrame,如:

代码:

df[df.columns[-1]] 

测试代码:

df = pd.DataFrame({"a": [dt.datetime(2017, 1, 3), 
         dt.datetime(2017, 2, 4), 
         dt.datetime(2017, 3, 5)], 
        "b": [[2, 4], [6, 8], [10, 12]]}) 

print(df) 
print(df[df.columns[-1]]) 

结果:

  a   b 
0 2017-01-03 [2, 4] 
1 2017-02-04 [6, 8] 
2 2017-03-05 [10, 12] 

0  [2, 4] 
1  [6, 8] 
2 [10, 12] 
Name: b, dtype: object 

但我需要的结果是数组没有列出

如果您需要在数组列表的转换,以一个阵列阵列的数组,那么整个转换为一个numpy.array,numpy将进入并将内部列表转换为数组。

last_col = np.array(list(df[df.columns[-1]])) 
print(last_col) 
print(last_col.shape) 

结果:

[[ 2 4] 
[ 6 8] 
[10 12]] 

(3, 2) 
+0

感谢您的回复我可以访问专栏,问题是关于我在阅读时获得的形状! – Engine

+0

很明显,我不明白你的问题...建议你尝试创建一个MCVE .... –

+0

抱歉,我拥有的代码对于这个问题太大了,我不知道如何才能解决问题更清楚! – Engine