Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/302.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 了解我的数据是系列还是数据帧_Python_Pandas - Fatal编程技术网

Python 了解我的数据是系列还是数据帧

Python 了解我的数据是系列还是数据帧,python,pandas,Python,Pandas,我有以下数据: 看到结果后,我会假设它是一个具有层次索引的序列。 但我还是不确定。 这就是我执行以下操作的原因: 通过看到上面的警告,我得到了一个确认,这确实是一个系列。 我仍然在问自己,我能不能在不引起任何错误的情况下找到答案。 我的意思是,是否有任何“pd.attribute”来确定它是序列还是数据帧? 提前感谢您提供的任何指针。您可以使用iInstance即 isinstance(ldata, pd.Series) 或类型(这是一个函数,而不是ldata方法),即 我倾向于发现前者更

我有以下数据:

看到结果后,我会假设它是一个具有层次索引的序列。 但我还是不确定。 这就是我执行以下操作的原因:

通过看到上面的警告,我得到了一个确认,这确实是一个系列。 我仍然在问自己,我能不能在不引起任何错误的情况下找到答案。 我的意思是,是否有任何“pd.attribute”来确定它是序列还是数据帧?
提前感谢您提供的任何指针。

您可以使用
iInstance

isinstance(ldata, pd.Series)
类型
(这是一个函数,而不是ldata方法),即


我倾向于发现前者更有用,因为您通常不仅要确定类型是什么,还要确定它是否属于特定类型,以便您可以采取一些行动。

放入if..elif..else块:

if isinstance(data, pd.DataFrame):
  # Dataframe
elif isinstance(data, pd.Series):
  # Series
else:
  # other type
if isinstance(data, pd.DataFrame):
  # Dataframe
elif isinstance(data, pd.Series):
  # Series
else:
  # other type