Skip to content

Instantly share code, notes, and snippets.

@lukasjoc
Last active April 21, 2020 09:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukasjoc/58e576536efe351a1085a6a954a9f4dc to your computer and use it in GitHub Desktop.
Save lukasjoc/58e576536efe351a1085a6a954a9f4dc to your computer and use it in GitHub Desktop.
get deltas from other frame and keep incoming frame
import pandas as pd
db_data = [
{"id": 1, "name": "Luads", "number": "000000000000", "number_active": 1},
{"id": 2, "name": "Luads", "number": "000000000000", "number_active": 1},
{"id": 3, "name": "Luads", "number": "000000000000", "number_active": 1},
{"id": 4, "name": "Lukas", "number": "000000000000", "number_active": 1},
{"id": 5, "name": "Luisd", "number": "000000000000", "number_active": 1},
{"id": 6, "name": "Jocham", "number":"000000000000", "number_active": 1},
]
input_data = [
{"id": 1, "name": "L", "number": "0000", "number_active": 1},
{"id": 2, "name": "Lukas", "number": "23", "number_active": 1},
]
df_db = pd.json_normalize(db_data)
df_input = pd.json_normalize(input_data)
print(df_db)
print(df_input)
# this is not the best solution. but i need to figure out a way to use loc and decide between views and replications
common = df_db.merge(df_input, on=["id"])
dropped_things = df_db[(~df_db.id.isin(common.id))]
dropped_things["number_active"] = 0
want = pd.concat([df_input, dropped_things])
print(want)
db_data = [
{"id": 1, "name": "Luads", "number": "000000000000", "number_active": 1},
{"id": 2, "name": "Luads", "number": "000000000000", "number_active": 0},
{"id": 3, "name": "Luads", "number": "000000000000", "number_active": 1},
]
input_data = [
{"id": 1, "name": "L", "number": "0000", "number_active": 1},
{"id": 3, "name": "L", "number": "0000", "number_active": 1},
{"id": 4, "name": "Lukas", "number": "23", "number_active": 1},
{"id": 5, "name": "Lukas", "number": "23", "number_active": 1},
]
# this is not the best solution. but i need to figure out a way to use loc and decide between views and replications
common = df_db.merge(df_input, on=["id"])
dropped_things = df_db[(~df_db.id.isin(common.id))]
dropped_things["number_active"] = 0
want = pd.concat([df_input, dropped_things])
print(want)
df_db = pd.json_normalize(db_data)
df_input = pd.json_normalize(input_data)
print(df_db)
print(df_input)
@lukasjoc
Copy link
Author

lukasjoc commented Apr 21, 2020

i dont really know how to resolve this warning. so if you know comment below

/usr/local/lib/python3.6/dist-packages/ipykernel/__main__.py:4: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment