pandas.DataFrame.update#
- DataFrame.update(other, join='left', overwrite=True, filter_func=None, errors='ignore')[源代码]#
使用来自另一个DataFrame的非NA值就地修改。
在索引上对齐。没有返回值。
- 参数
- otherDataFrame或强制成为DataFrame的对象
应至少有一个与原始DataFrame匹配的索引/列标签。如果传递了Series,则必须设置其名称属性,并将其用作列名以与原始DataFrame对齐。
- join{‘Left’},默认为‘Left’
只实现了LEFT JOIN,保留了原始对象的索引和列。
- overwrite布尔值,默认为True
如何处理重叠关键点的非NA值:
True:使用中的值覆盖原始DataFrame的值 other 。
FALSE:仅更新原始DataFrame中为NA的值。
- filter_func可调用(1D数组)->布尔1D数组,可选
可以选择替换NA以外的值。对于应更新的值,返回True。
- errors{‘RAISE’,‘IGNORE’},默认‘IGNORE’
如果“”Rise“”,则在DataFrame和 other 两者都在同一位置包含非NA数据。
- 退货
- None方法直接更改调用对象
- 加薪
- ValueError
什么时候 errors='raise' 而且有重叠的非NA数据。
什么时候 errors 也不是 'ignore' 或 'raise'
- NotImplementedError
如果 join != 'left'
参见
dict.update
词典也有类似的方法。
DataFrame.merge
用于列上列操作。
示例
>>> df = pd.DataFrame({'A': [1, 2, 3], ... 'B': [400, 500, 600]}) >>> new_df = pd.DataFrame({'B': [4, 5, 6], ... 'C': [7, 8, 9]}) >>> df.update(new_df) >>> df A B 0 1 4 1 2 5 2 3 6
DataFrame的长度不会因更新而增加,只有匹配索引/列标签的值才会更新。
>>> df = pd.DataFrame({'A': ['a', 'b', 'c'], ... 'B': ['x', 'y', 'z']}) >>> new_df = pd.DataFrame({'B': ['d', 'e', 'f', 'g', 'h', 'i']}) >>> df.update(new_df) >>> df A B 0 a d 1 b e 2 c f
对于系列,必须设置其名称属性。
>>> df = pd.DataFrame({'A': ['a', 'b', 'c'], ... 'B': ['x', 'y', 'z']}) >>> new_column = pd.Series(['d', 'e'], name='B', index=[0, 2]) >>> df.update(new_column) >>> df A B 0 a d 1 b y 2 c e >>> df = pd.DataFrame({'A': ['a', 'b', 'c'], ... 'B': ['x', 'y', 'z']}) >>> new_df = pd.DataFrame({'B': ['d', 'e']}, index=[1, 2]) >>> df.update(new_df) >>> df A B 0 a x 1 b d 2 c e
如果 other 包含NAN,则不会在原始数据帧中更新相应的值。
>>> df = pd.DataFrame({'A': [1, 2, 3], ... 'B': [400, 500, 600]}) >>> new_df = pd.DataFrame({'B': [4, np.nan, 6]}) >>> df.update(new_df) >>> df A B 0 1 4.0 1 2 500.0 2 3 6.0