首页 > 解决方案 > 如何在里面用熊猫系列展平Dataframe?

问题描述

我想展平我的数据框。问题是每一行都包含 pandas.core.series.Series 类型的数据,我想将其转换为 df 并从其他行中相互连接。我知道有一些方法可以通过简单的一列来实现,但是下面的代码:

big_load_df = pd.concat([load_df, load_df['LoadNodes'].apply(pd.Series)], axis = 1).drop('LoadNodes', axis = 1)

给我这样的输出

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-192-ebe7fd4b7b86> in <module>
      2 # big_load_df = data.join(df1).reset_index(drop=True)
      3 
----> 4 big_load_df = pd.concat([load_df, load_df['LoadNodes'].apply(pd.Series)], axis = 1).drop('LoadNodes', axis = 1)
      5 

/opt/conda/lib/python3.7/site-packages/pandas/core/series.py in apply(self, func, convert_dtype, args, **kwargs)
   4355         dtype: float64
   4356         """
-> 4357         return SeriesApply(self, func, convert_dtype, args, kwargs).apply()
   4358 
   4359     def _reduce(

/opt/conda/lib/python3.7/site-packages/pandas/core/apply.py in apply(self)
   1041             return self.apply_str()
   1042 
-> 1043         return self.apply_standard()
   1044 
   1045     def agg(self):

/opt/conda/lib/python3.7/site-packages/pandas/core/apply.py in apply_standard(self)
   1100                     values,
   1101                     f,  # type: ignore[arg-type]
-> 1102                     convert=self.convert_dtype,
   1103                 )
   1104 

/opt/conda/lib/python3.7/site-packages/pandas/_libs/lib.pyx in pandas._libs.lib.map_infer()

/opt/conda/lib/python3.7/site-packages/pandas/core/series.py in __init__(self, data, index, dtype, name, copy, fastpath)
    353             name = ibase.maybe_extract_name(name, data, type(self))
    354 
--> 355             if is_empty_data(data) and dtype is None:
    356                 # gh-17261
    357                 warnings.warn(

/opt/conda/lib/python3.7/site-packages/pandas/core/construction.py in is_empty_data(data)
    795     is_none = data is None
    796     is_list_like_without_dtype = is_list_like(data) and not hasattr(data, "dtype")
--> 797     is_simple_empty = is_list_like_without_dtype and not data
    798     return is_none or is_simple_empty
    799 

/opt/conda/lib/python3.7/site-packages/pandas/core/generic.py in __nonzero__(self)
   1536     def __nonzero__(self):
   1537         raise ValueError(
-> 1538             f"The truth value of a {type(self).__name__} is ambiguous. "
   1539             "Use a.empty, a.bool(), a.item(), a.any() or a.all()."
   1540         )

ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

我知道问题出在我的数据框行上,但我能做什么?

load_df
LoadNodes
29  Id Description Name No LoadCaseId LtbSup...
30  Id Description Name No LoadCaseId LtbSup...
31  Id Description Name No LoadCaseId LtbSup...
32  Id Description Name No LoadCaseId LtbSup...
33  Id Description Name No LoadCaseId LtbSup...
... ...
902 Id Description Name No LoadCaseId LtbSup...
903 Id Description Name No LoadCaseId LtbSup...
904 Id Description Name No LoadCaseId LtbSup...
905 Id Description Name No LoadCaseId LtbSup...
914 Id Description Name No LoadCaseId LtbSup...
5073 rows × 1 columns
load_df['LoadNodes'][29]
29       Id Description  Name  No  LoadCaseId LtbSup...
29       Id Description  Name  No  LoadCaseId LtbSup...
29       Id Description  Name  No  LoadCaseId LtbSup...
29       Id Description  Name  No  LoadCaseId LtbSup...
29       Id Description  Name  No  LoadCaseId LtbSup...
29       Id Description  Name  No  LoadCaseId LtbSup...
Name: LoadNodes, dtype: object

标签: pythonpandasdataframeseries

解决方案


推荐阅读