pandas 将单元格是列表的DataFrame拆成多列

方法:

pd.DataFrame(dfcol.values.tolist()) 将单元格元素是列表的列拆成多列

如果要与原来的其他列合并

pd.concat(pd.DataFrame(df\[col.values.tolist()), df其他列], axis=1)

示例:

points数组如下:

生成DataFrame如下

处理结果为:

解释:

1、dfcol.values.tolist() 解释,本例中 col=0。生成一个 dfcol.values numpy矩阵,让后用tolist()转化为列表。

2、pd.DataFrame(列表)创造一个.DataFrame。列表中每个元素是DataFrame的按行排列的元素,即第n个元素放在第n行的0列。由于元素是数组,数组在生成DataFrame时会自动分列。

3、如果使用 df_i.replace("NaN", "",inplace=True)替换NaN为空字符串,则又会变成列表。

相关推荐
2601_967979122 天前
一文详解:7个 Pandas stack() 和 unstack() 使用技巧
pandas·数据预处理·dataframe·stack()·unstack()
2601_967979124 天前
pandas重塑层次化索引(stack()和unstack()函数解析)
pandas·重塑·层次化索引·stack()·unstack()
郝学胜-神的一滴5 天前
Effective Python 条款35:用类封装替代生成器异常注入
开发语言·人工智能·python·金融·pandas·scikit-learn·matplotlib
弈栈录5 天前
时间序列分析:按日、周、月进行趋势统计
数据分析·pandas
linweidong8 天前
RTX 4090 Ubuntu + ComfyUI + comfy-kitchen 0.2.41 完整部署文档
机器学习·云原生·conda·pandas·scikit-learn
benchmark_cc9 天前
个人开发者接入五档盘口:用 QuantDash 读取单只股票的验收清单与易错点
python·数据分析·pandas·量化交易·股票数据·quantdash
benchmark_cc12 天前
用 QuantDash 读取单只股票五档盘口:抽样、缺失值检查与复现记录
python·数据分析·pandas·量化交易·股票数据·quantdash
benchmark_cc16 天前
单只股票五档盘口接入个人研究仓库:QuantDash 读取、校验与易错点
python·数据分析·pandas·量化交易·股票数据·quantdash
benchmark_cc17 天前
用 QuantDash 读取美股行情:AAPL.US 的运行前检查、排错顺序与数据校验清单
python·数据分析·pandas·量化交易·股票数据·quantdash
databook19 天前
经常听到的透视表是干什么的?
python·数据分析·pandas