python - 如何在里面用熊猫系列展平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
解决方案
推荐阅读
- javascript - 表示具有有序子节点的树
- c# - 根据适合相同内容的内容拆分内容
- php - 为什么 composer.json 需要 PHP 7.4 但服务器正在运行 PHP 7.3 不会中断部署?
- node.js - 无法让 'res.status(200).redirect('')' 工作
- java - 在 Spring Batch 中读取 CSV 数据(创建自定义 LineMapper)
- php - Codeigniter 致命错误:未找到接口“App\Model\ModeInterface”
- javascript - 如何在每一行中添加一个类名而不影响其余行?
- python - pylint/jedi 的 Cloud9 python 版本
- r - 我可以使用 KableExtra 添加可以直观地描绘列组的线条吗?
- python - 闰年python脚本