【发布时间】:2018-02-03 04:21:35
【问题描述】:
Education'] = df['Husband_Education'].astype(str) + df['Husband_Black']`我想将一列中的一些值与python中另一列的内容结合起来。该列的值为:“yes”、“No”和“Years”。我只想将所有“年份值迁移到另一列”我的数据如下所示:
我的数据:
+--------------------+---------------+
| Husband_education | Husband black |
+--------------------+---------------+
| less than 12 years |
+--------------------+---------------+
| 12 -15 years | No |
+--------------------+---------------+
| 12-15 years | yes |
+--------------------+---------------+
期望的输出:
+--------------------+---------------+
| Husband_education | Husband black |
+--------------------+---------------+
| less than 12 years | --- |
+--------------------+---------------+
| 12 -15 years | No |
+--------------------+---------------+
| 12-15 years | yes |
+--------------------+---------------+
我希望所有等于“年”的单词移动到第一列,并在第二列中保留“是”和“否”的值,我有 3,772 行
我的代码看起来像这样有什么想法吗?
for row in df['Husband_Black']:
if 'years' in row :
df['Husband_Education'] = df['Husband_Education'].astype(str) + df['Husband_Black']
【问题讨论】:
-
不清楚你想要完成什么。请提供预期结果的示例。
-
提供预期的输出和你的努力,你尝试了什么,没有成功。
-
我在代码前添加了输出,请查看我的帖子