python - 如何创建仅包含另一个数据框中已更改行的新数据框?

标签 python python-3.x pandas dataframe numpy

我想创建一个新的 csv 文件,其中只有已更改的行。

enter image description here

条件是:

import pandas as pd
df = pd.read_csv("sample.csv", delimiter='')

df['Part Number'] = df['Part Number'].astype(str).str.replace('+','-PLUS')
df['Part Number'] = df['Part Number'].astype(str).str.replace('/','-SLASH-')
df['Part Number'] = df['Part Number'].astype(str).str.replace('\\','-SLASH-')
df['Part Number'] = df['Part Number'].astype(str).str.replace(' ','-')
df['Part Number'] = df['Part Number'].astype(str).str.replace('_','-')
df['Part Number'] = df['Part Number'].astype(str).str.replace('.','-')
df['Part Number'] = df['Part Number'].astype(str).str.replace('"','')
df['Part Number'] = df['Part Number'].astype(str).str.replace('(','')
df['Part Number'] = df['Part Number'].astype(str).str.replace(')','')
df['Part Number'] = df['Part Number'].astype(str).str.replace('%','-')
# It can be more these are examples.

现在我想创建新的数据框: 这应该只列出部件号已被替换的行。

enter image description here

最佳答案

尝试存储原始值并稍后比较它们:

original = df['Part Number'].copy()

#...Changes happen here...

new_df = df[df['Part Number'] != original].join(original, rsuffix = " Changed")

关于python - 如何创建仅包含另一个数据框中已更改行的新数据框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65714912/

相关文章:

python - python中的异步编程

javascript - 转换 Javascript RegEx.exec,将组匹配返回到 Python

python - Ms Word 及其花哨的引号中断字符串比较

python - 在迭代 pandas 数据帧时删除一行

python - matlab数据文件到pandas DataFrame

Python 调度程序 vs 循环 + sleep

python - 类型错误 : unsupported callable using Dataset with estimator input_fn

python - 解析 dbus 监视器输出消息

python-3.x - 使用 Keras 和 TensorFlow 后端可重现结果

python - 将百分位数传递给 pandas agg 函数