我在熊猫“DataFrame”的错误中遇到了麻烦

时间:2021-04-29 02:54:22

标签: python pandas

我是一个正在学习熊猫的大人物。 我尝试了以下两件事,但没有解决。 我使用 Jupyter 笔记本。 你能帮我吗?

#1

import sys
sys.getdefaultencoding()

#2

from importlib import reload
import sys
reload(sys)

#我想成功。 #我在另一个文件中成功了,但在当前文件中无法运行。

from pandas import Series,DataFrame
import pandas as pd

data={"ID":["001","002","003"],
      "city":["hyougo","tiba","gihu"],
      "people":["100","230","249"]}
data_frame=DataFrame(data)
print(data_frame)

#错误 -------------------------------------------------- -------------------------

    AttributeError                            Traceback (most recent call last)
    <ipython-input-133-640865466ed4> in <module>
          3       "people":["100","230","249"]}
          4 data_frame=DataFrame(data)
    ----> 5 print(data_frame)
    
    ~\anaconda3\lib\site-packages\pandas\core\frame.py in __repr__(self)
        678         else:
        679             width = None
    --> 680         self.to_string(
        681             buf=buf,
        682             max_rows=max_rows,
    
    ~\anaconda3\lib\site-packages\pandas\core\frame.py in to_string(self, buf, columns, col_space, header, index, na_rep, formatters, float_format, sparsify, index_names, justify, max_rows, min_rows, max_cols, show_dimensions, decimal, line_width, max_colwidth, encoding)
        818                 line_width=line_width,
        819             )
    --> 820             return formatter.to_string(buf=buf, encoding=encoding)
        821 
        822     # ----------------------------------------------------------------------
    
    ~\anaconda3\lib\site-packages\pandas\io\formats\format.py in to_string(self, buf, encoding)
        912         encoding: Optional[str] = None,
        913     ) -> Optional[str]:
    --> 914         return self.get_result(buf=buf, encoding=encoding)
        915 
        916     def to_latex(
    
    ~\anaconda3\lib\site-packages\pandas\io\formats\format.py in get_result(self, buf, encoding)
        519         """
        520         with self.get_buffer(buf, encoding=encoding) as f:
    --> 521             self.write_result(buf=f)
        522             if buf is None:
        523                 return f.getvalue()
    
    ~\anaconda3\lib\site-packages\pandas\io\formats\format.py in write_result(self, buf)
        821         else:
        822 
    --> 823             strcols = self._to_str_columns()
        824             if self.line_width is None:  # no need to wrap around just print
        825                 # the whole frame
    
    ~\anaconda3\lib\site-packages\pandas\io\formats\format.py in _to_str_columns(self)
        717         # may include levels names also
        718 
    --> 719         str_index = self._get_formatted_index(frame)
        720 
        721         if not is_list_like(self.header) and not self.header:
    
    ~\anaconda3\lib\site-packages\pandas\io\formats\format.py in _get_formatted_index(self, frame)
       1057             )
       1058         else:
    -> 1059             fmt_index = [index.format(name=self.show_row_idx_names, formatter=fmt)]
       1060 
       1061         fmt_index = [
    
    AttributeError: 'list' object has no attribute 'format'

1 个答案:

答案 0 :(得分:0)

需要先导入pandas,然后在pd前添加别名DataFrame

import pandas as pd

data={"ID":["001","002","003"],
      "city":["hyougo","tiba","gihu"],
      "people":["100","230","249"]}
data_frame=pd.DataFrame(data)
print(data_frame)

打印:

    ID    city people
0  001  hyougo    100
1  002    tiba    230
2  003    gihu    249

如果你没有安装pandas,你需要先安装

pip install pandas