【发布时间】:2019-01-07 21:48:40
【问题描述】:
我有这个df:
import pandas as pd
df1 = pd.DataFrame({
'Type': ['red', 'blue', 'red', 'red', 'blue'],
'V1': ['No', 'No', 'No', 'Yes', 'No'],
'V2': ['Yes', 'Yes', 'No', 'Yes', 'No'],
'V3': ['Yes', 'No', 'No', 'Yes', 'No'],
'V4': ['No', 'No', 'No', 'Yes', 'Yes']
})
我想要一个如下所示的数据框:
Type V1 V2 V3 V4 V3_4
0 red No Yes Yes No Yes
1 blue No Yes No No No
2 red No No No No No
3 red Yes Yes Yes Yes Yes
4 blue No No No Yes Yes
因此,基本上来自V3 的任何“是”值都将转入一个新列V3_4,并将来自V4 的“是”值转入V3_4 列。
看起来我可以使用 ffill 或使用某些逻辑构建 python 函数来做到这一点。两种方法我都可以,我想知道最优雅的是什么。
【问题讨论】:
标签: python string pandas function dataframe