Python:打印Pandas数据帧返回numpy.ndarray属性错误

时间:2016-10-31 06:15:19

标签: python pandas numpy dataframe

我正在尝试使用numpy数组创建一个pandas数据帧。 数据,索引和列数组都是numpy' ndarrays' (分别为2D,1D和1D),为了这个例子,所有都是float64。

import pandas as pd
import numpy as np

data = np.zeros((100, 15))
index = np.zeros((100, 1))
columns = np.zeros ((15, 1))

df1 = pd.DataFrame(data=data, index=index, columns=columns)
print(df1)

当我打印df1时,我收到此属性错误,我无法解决:

  

AttributeError:' numpy.ndarray'对象没有属性' endswith'

我打印print(df1.to_string())时会返回相同的错误,但如果我打印print(df1.values)print(df1.index)print(df1.columns),则会返回预期的值。

我在这里遗漏了什么吗?不可否认,我对使用Pandas很新,但我认为这个简单的例子可以正常使用。

2 个答案:

答案 0 :(得分:1)

如果来源为<%= devise_error_messages! %><div class="container"> <div class="section section-signup"> <%= semantic_form_for(@resource, :as => resource_name, :url => user_session_path, :remote => true, :format => :json, :html => { :id => 'mainLogin' }) do |f| %> <%= f.inputs do %> <%= f.input :email, :label => 'Your email address', :input_html => { :placeholder => "Email"} %> <%= f.input :password, :label => 'Your password', :input_html => { :placeholder => "Password"} %> <% end %> <%= f.buttons do %> <% if devise_mapping.rememberable? %> <%= f.input :remember_me, :as => :boolean, :label => "Remember me on this computer", :required => false, :input_html => {:class => "remember-me"} %> <% end %> <%= f.commit_button :label => 'Sign me in', :button_html => {:class => 'login submit button', :disable_with => 'Wait...', :id => 'user_submit' }%> <% end %> <div class="forgot">Yikes: <a class="pass-reset-btn cboxElement" href="#pass-reset" data-toggle="modal" data-target="#myModal">I forgot my password!</a></div> <% end %> </div> </div> <!-- Modal --> <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button> <h4 class="modal-title" id="myModalLabel">Password reset</h4> </div> <div class="modal-body"> <p>Enter your email address and we'll shoot you a link for resetting your password.</p> <!-- Start Fromtastic Markup --> <%= semantic_form_for(resource_name, :url => password_path(resource_name), :remote => true, :format => :json, :html => { :id => 'password_reset' }) do |f| %> <%= f.inputs do %> <%= f.input :email, :label => 'Your email address', :input_html => { :placeholder => "Enter your email..."}%> <% end %> <%= f.buttons do %> <%= f.commit_button :label => 'Send me that link', :button_html => {:class => 'submit button', :disable_with => 'Wait...' }%> <% end %> <div class="clearfix"></div> <% end %> <!-- End Fromtastic Markup --> </div> </div> </div> </div> ,我认为您需要ravel来展平数组,以便创建indexcolumns

np.zeros((100, 15))

但是如果需要索引和列的默认值,只需使用DataFrame构造函数 - np.zeros ((15, 1))index = np.zeros((100, 1)).ravel() columns = np.zeros ((15, 1)).ravel() 将设置为index,因为没有索引信息且没有列标签:

columns

答案 1 :(得分:1)

TL; DR

IF any(Monday, 65, 72, 74) Day_Of_Interest = 2.

详细

您将一个元组参数传递给do repeat day=Sunday Monday Tuesday Wednesday Thursday Friday Saturday /Dnum=1 2 3 4 5 6 7. IF any(day, 65, 72, 74) Day_Of_Interest = Dnum. end repeat. exe. ,这会产生一个数组

>>> index = np.zeros(100)
>>> columns = np.zeros (15)

您收到错误,因为i)每个元素都是一个数组,而ii)np.zeros没有为数组定义。

>>> np.zeros((15,1)) array([[ 0.], [ 0.], [ 0.], [ 0.], [ 0.], [ 0.], [ 0.], [ 0.], [ 0.], [ 0.], [ 0.], [ 0.], [ 0.], [ 0.], [ 0.]]) endswith都采用类似列表(包括index)属性。你不必担心他们是否会成为一个专栏&#39;或者&#39; row&#39;在矩阵中(我认为这就是你使用元组的原因)。

你只想要一个数组......

columns