【问题标题】:only convert certain rows with Pandas只用 Pandas 转换某些行
【发布时间】:2018-04-03 03:33:37
【问题描述】:

我有一只熊猫DataFrame。我正在尝试操作一列以显示月数。如果记录是 01m,则将其设为 1。否则,如果是 01y,则乘以 1 x 12 即可得到 12。但有时我确实有一个名为 _variable_value 的字段,我想保持原样。 (忽略)

当前的dataframe 如下所示:

      institution_short_name            product_name             Term  term
0                        One                Standard       _01y_value  4.85
1                        One                Standard       _02y_value  5.15
2                        One                Standard       _03y_value  5.49
3                        One                Standard       _04y_value  5.89
4                        One                Standard       _05y_value  6.09
5                        One                Standard       _06m_value  4.99
6                        One                Standard       _18m_value  5.15
7                        One                Standard  _variable_value  5.79

我目前收到一个错误,因为它正在尝试将 'va' 转换为 int,这是不可能的。

df['Time'] = np.where(df['Time'].str.contains("y"), df['Time'].map(lambda x: str(x)[1:3]).astype(int).apply(lambda x: x*12), df['Time'].map(lambda x: str(x)[1:3]).astype(int))

这是我的预期输出:

      institution_short_name            product_name             Term  term
0                        One                Standard               12  4.85
1                        One                Standard               24  5.15
2                        One                Standard               36  5.49
3                        One                Standard               48  5.89
4                        One                Standard               60  6.09
5                        One                Standard                6  4.99
6                        One                Standard               18  5.15
7                        One                Standard  _variable_value  5.79

【问题讨论】:

  • 请提供熊猫代码pd.DataFrame() 格式的示例df。我们不需要重新创建数据。
  • 啊,不知道你可以复制粘贴进去!
  • 现在你已经删除了这些行,我现在可以复制粘贴了。

标签: python python-3.x python-2.7 pandas dataframe


【解决方案1】:

使用str.replace 和正则表达式的一种方法:

df['Time'] = df.Time.str.replace(
    r"_(\d{2})([ym]).*", 
    lambda m: str(int(m.group(1)) * (12 if m.group(2) == "y" else 1))
)

df

#  institution_short_name product_name             Time  term
#0                    One     Standard               12  4.85
#1                    One     Standard               24  5.15
#2                    One     Standard               36  5.49
#3                    One     Standard               48  5.89
#4                    One     Standard               60  6.09
#5                    One     Standard                6  4.99
#6                    One     Standard               18  5.15
#7                    One     Standard  _variable_value  5.79

_(\d{2})([ym]).* 匹配以_ + two digits + y or m 开头的字符串,并将数字和单位捕获到两个不同的组中;根据单位,可以根据需要通过引用lambda函数中的组来修改匹配的数值; _variable_value 等与模式不匹配的情况将被忽略。

【讨论】:

    猜你喜欢
    • 2017-01-13
    • 1970-01-01
    • 1970-01-01
    • 2019-09-26
    • 2021-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多