python - 想将列表转换为以下示例的字符串
问题描述
我从一列得到一个结果集df['Test'].head()
:
0 [fed, official, say, weak, data, caused, weather, slow, taper]
1 [fed, 's, charles, plosser, see, high, bar, change, pace, tapering]
我想将其转换如下并存储在与以下内容相同的列中:
fed official say weak data caused weather slow taper
fed 's Charles plosser see high bar change pace tapering
解决方案
利用.apply(" ".join)
前任:
import pandas as pd
df = pd.DataFrame({'Test': [['fed', 'official', 'say', 'weak', 'data', 'caused', 'weather', 'slow', 'taper'],
['fed', "'s", 'charles', 'plosser', 'see', 'high', 'bar', 'change', 'pace', 'tapering']
]
})
print(df["Test"].apply(" ".join))
输出:
0 fed official say weak data caused weather slow...
1 fed 's charles plosser see high bar change pac...
Name: Test, dtype: object
推荐阅读
- php - 如何以这种并排模式排列数字?
- google-bigquery - 如何将多个 csv 文件(不同的模式)加载到 bigquery 中
- reactjs - material-ui中的高程和z-index之间的区别?
- dart - 我什么时候应该在 Flutter 中使用以下属性
- javascript - 电子全局变量或 DB
- c++ - mov + mfence 在 NUMA 上安全吗?
- azure - 将 Web 应用部署到 Azure 应用服务 - 使用资源管理器连接
- tensorflow - 如何结合两个 RNN 模型
- excel - 如何在一个语句中打开和设置工作簿?
- c# - c#绘图库计算范围和中点来绘制和圆