我有以下python程序test_pandas.py
。
# !/usr/bin/env python3.4
# -*- coding: utf-8 -*-
import pprint
import pandas as pd
import mypandas.mypandas
df = pd.read_csv('AllStarFull.csv')
mypandas.print_pandas_df(df,50,8)
# groupby 'player ID'
print('Grouping by Player ID')
gb = df.groupby(['playerID'])
pprint.pprint(list(gb))
# groupby 'yearID'
print('Grouping by Year ID')
gb = df.groupby(['yearID'])
pprint.pprint(list(gb))
我的文件夹结构如下。
--python
--concepts
--mypandas
--mypandas.py
--__init__.py
--test_pandas.py
当我运行test_pandas.py
时,我收到以下错误。
UserWarning)
Traceback (most recent call last):
File "C:/Cubic/playpen/python/concepts/pandas/test_pandas.py", line 11, in <module>
mypandas.print_pandas_df(df,50,8)
AttributeError: module 'mypandas' has no attribute 'print_pandas_df'
mypandas.py
具有print_pandas_df
import pandas as pd
import pprint
def print_pandas_df(df, rows, columns):
with pd.option_context('display.max_rows', rows, 'display.max_columns', columns):
pprint.pprint(df)
答案 0 :(得分:1)
您需要输入完整路径:directory.filename.methodname:
mypandas.mypandas.print_pandas_df(df,50,8)
你也可以说
from mypandas import mypandas
然后按原样编写代码。