Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 数据库到dataframes并获取有关列填充的信息_Python_Mysql_Pandas_Metadata - Fatal编程技术网

Python 数据库到dataframes并获取有关列填充的信息

Python 数据库到dataframes并获取有关列填充的信息,python,mysql,pandas,metadata,Python,Mysql,Pandas,Metadata,我试图从pandas dataframe中获取一些元数据:我想知道在数据库的所有表中有多少行包含数据。下面的代码告诉我: PandasError: DataFrame constructor not properly called! 但我不知道为什么。这似乎是一个完全没有数据的表格,但我不明白为什么会有问题 engine = sqlalchemy.create_engine("mysql+mysqldb://root:123@127.0.0.1/%s" % db) meta = sqlalc

我试图从pandas dataframe中获取一些元数据:我想知道在数据库的所有表中有多少行包含数据。下面的代码告诉我:

PandasError: DataFrame constructor not properly called! 
但我不知道为什么。这似乎是一个完全没有数据的表格,但我不明白为什么会有问题

engine = sqlalchemy.create_engine("mysql+mysqldb://root:123@127.0.0.1/%s" % db)
meta = sqlalchemy.MetaData()
meta.reflect(engine)
tables = meta.tables.keys() # Fetches all table names
cnx = engine.raw_connection() # Raw connection is needed.

df = pd.read_sql('SELECT * FROM offending_table', cnx )
df = df.applymap(lambda x: np.nan if x == "" else x) # maak van alle "" een NaN

count = df.count()

table = pd.DataFrame(count, columns=['CellsWithData'])
table
完整的错误消息是:

offending_table
---------------------------------------------------------------------------
PandasError                               Traceback (most recent call last)
<ipython-input-367-f33bb79a6773> in <module>()
     14     count = df.count()
     15 
---> 16     table = pd.DataFrame(count, columns=['CellsWithData'])
     17     if len(all_tables) == 0:
     18         all_tables = table

/Library/Python/2.7/site-packages/pandas/core/frame.pyc in __init__(self, data, index, columns, dtype, copy)
    271                                          copy=False)
    272             else:
--> 273                 raise PandasError('DataFrame constructor not properly called!')
    274 
    275         NDFrame.__init__(self, mgr, fastpath=True)

PandasError: DataFrame constructor not properly called!
当我这样做的时候:

df.count()
我得到:

0

这是预期的行为吗?

看来
applymap
是罪魁祸首:-)

当您有一个空的
read\u sql
查询结果集时,您将得到一个空的数据帧。例如:

In [2]: df = pd.DataFrame(columns=list('ABC'))

In [3]: df
Out[3]:
Empty DataFrame
Columns: [A, B, C]
Index: []
使用此空数据帧,然后在调用此数据帧上的applymap时,它显然会转换为一个系列,然后计数只给出一个数字:

In [10]: df2 = df.applymap(lambda x: np.nan if x == "" else x)

In [11]: df2
Out[11]:
A   NaN
B   NaN
C   NaN
dtype: float64

In [12]: df2.count()
Out[12]: 0
直接在空数据帧上进行计数时,会得到所需的输出:

In [13]: df.count()
Out[13]:
A    0
B    0
C    0
dtype: int64
我不知道applymap为什么会这样做(或者它是否是一个bug),但现在一个简单的解决方案是在applymap之前快速执行if:

if not len(df):
   df = df.applymap(lambda x: np.nan if x == "" else x)

上述问题的原因是,
DataFrame
构造函数不接受标量作为输入数据。

尝试将示例缩减到实际失败的代码中。能否在for循环中添加一个
print t t
,以便您可以看到它给出错误的表。然后,您能否显示该特定表格的
df
count
的输出?您确定是该表格吗?因为那真的很奇怪。顺便说一句,如果你给一个数据帧加了一个标量,你会得到这样一个错误,比如
pd.DataFrame(5,columns=['CellsWithData']
。作为进一步的调试步骤,可能在数据帧调用之前做一个
print count
。你是对的,它是一个不同的表。当循环碰到一个空表(例如,没有行)时,问题似乎会出现.那张表的
count
是什么样子的?
if not len(df):
   df = df.applymap(lambda x: np.nan if x == "" else x)