python - 返回更新系列的 pandas.Series 方法

标签 python pandas

是否有一种 Series 方法的作用类似于更新但返回更新后的系列而不是就地更新?

换句话说,有没有更好的方法来做到这一点:

# Original series
ser1 = Series([10, 9, 8, 7], index=[1,2,3,4])

# I want to change ser1 to be [10, 1, 2, 7]
adj_ser = Series([1, 2], index=[2,3])

adjusted = my_method(ser1, adj_ser)

# Is there a builtin that does this already?
def my_method(current_series, adjustments):
    x = current_series.copy()
    x.update(adjustments)
    return(x)

最佳答案

一个可能的解决方案应该是 combine_first ,但它通过 ser1 更新 adj_ser,还将整数转换为 float:

adjusted = adj_ser.combine_first(ser1)
print (adjusted)
1    10.0
2     1.0
3     2.0
4     7.0
dtype: float64

关于python - 返回更新系列的 pandas.Series 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53672196/

相关文章:

Pandas 数据透视表内存错误,大量可用内存

python - 按天计算计数

python - Python是否具有用于一阶递归关系的迭代递归生成器函数?

python oracle 将结果放入字典并匹配给定的列表

python - 如何按定义的时间间隔对 Pandas 数据框进行分组?

python - 从 df 创建字典的正确方法或计算 jaccard 相似度的方法

python Pandas : Add column to grouped DataFrame with method chaining

python - 多级别的 Groupby 和 Sum

python - 使pytest对数据库使用固定日期

python - 创建新列,其中两个不同数据帧的两列相同