pandas - get subelements
Hi team,
My need is to extract particular sub-elements (ContactId and
AssociatedEntity) from below json using pandas.
Json:
{
"odata.metadata": "https://example1.com/odata/$metadata#Contacts";,
"value": [
{
"Addresses": [
{
"ContactId": "35aa05d1-21c7-493d-96e3-3c966732",
"AssociatedEntity": "ac7a9ec8-b71b-486b-8b3b-41b6bc11f936"
}
],
"ContactId2": "35aa05d1-21c7-493d-96e3-3c966732"
},
{
"Addresses": [
{
"ContactId": "ca717463-734d-4f2f-a01e-6ff0c806",
"AssociatedEntity": "bda08493-7ae0-47cf-8d3a-f1a486498836"
}
],
"ContactId2": "ca717463-734d-4f2f-a01e-6ff0c806"
}
]
}
My code so far:
import json
import pandas as pd
txt1 =
'{"odata.metadata":"https://example1.com/odata/$metadata#Contacts","value":[
{ "Addresses":[{
"ContactId":"35aa05d1-21c7-493d-96e3-3c966732","AssociatedEntity":"ac7a9ec8-b71b-486b-8b3b-41b6bc11f936"
} ],"ContactId2":"35aa05d1-21c7-493d-96e3-3c966732"
},{ "Addresses":[{
"ContactId":"ca717463-734d-4f2f-a01e-6ff0c806","AssociatedEntity":"bda08493-7ae0-47cf-8d3a-f1a486498836"
} ],"ContactId2":"ca717463-734d-4f2f-a01e-6ff0c806"
}]}'
json2 = json.loads(txt1)
print("json:\n")
print(json2)
df = pd.json_normalize(json2['value'])
print("\npandas df (1):\n")
df1a = df['Addresses'].copy()
print(df1a)
pandas df (1):
0[{'ContactId': '35aa05d1-21c7-493d-96e3-3c...
1[{'ContactId': 'ca717463-734d-4f2f-a01e-6f...
Name: Addresses, dtype: object
Now how do I get ContactId and AssociatedEntity?
Please advise.
Best,
Marchello
--
https://mail.python.org/mailman/listinfo/python-list
Re: pandas - get subelements
Hi team,
It worked for me this way:
df3 = pd.json_normalize(json2["value"], "Addresses")
print(df3)
ContactId AssociatedEntity
0 35aa05d1-21c7-493d-96e3-3c966732
ac7a9ec8-b71b-486b-8b3b-41b6bc11f936
1 ca717463-734d-4f2f-a01e-6ff0c806
bda08493-7ae0-47cf-8d3a-f1a486498836
Solved.
On 2021-07-12 12:07, Marchello wrote:
Hi team,
My need is to extract particular sub-elements (ContactId and
AssociatedEntity) from below json using pandas.
Json:
{
"odata.metadata": "https://example1.com/odata/$metadata#Contacts";,
"value": [
{
"Addresses": [
{
"ContactId": "35aa05d1-21c7-493d-96e3-3c966732",
"AssociatedEntity": "ac7a9ec8-b71b-486b-8b3b-41b6bc11f936"
}
],
"ContactId2": "35aa05d1-21c7-493d-96e3-3c966732"
},
{
"Addresses": [
{
"ContactId": "ca717463-734d-4f2f-a01e-6ff0c806",
"AssociatedEntity": "bda08493-7ae0-47cf-8d3a-f1a486498836"
}
],
"ContactId2": "ca717463-734d-4f2f-a01e-6ff0c806"
}
]
}
My code so far:
import json
import pandas as pd
txt1 =
'{"odata.metadata":"https://example1.com/odata/$metadata#Contacts","value":[
{ "Addresses":[{
"ContactId":"35aa05d1-21c7-493d-96e3-3c966732","AssociatedEntity":"ac7a9ec8-b71b-486b-8b3b-41b6bc11f936"
} ],"ContactId2":"35aa05d1-21c7-493d-96e3-3c966732"
},{ "Addresses":[{
"ContactId":"ca717463-734d-4f2f-a01e-6ff0c806","AssociatedEntity":"bda08493-7ae0-47cf-8d3a-f1a486498836"
} ],"ContactId2":"ca717463-734d-4f2f-a01e-6ff0c806"
}]}'
json2 = json.loads(txt1)
print("json:\n")
print(json2)
df = pd.json_normalize(json2['value'])
print("\npandas df (1):\n")
df1a = df['Addresses'].copy()
print(df1a)
pandas df (1):
0[{'ContactId': '35aa05d1-21c7-493d-96e3-3c...
1[{'ContactId': 'ca717463-734d-4f2f-a01e-6f...
Name: Addresses, dtype: object
Now how do I get ContactId and AssociatedEntity?
Please advise.
Best,
Marchello
--
https://mail.python.org/mailman/listinfo/python-list
argparse support of/by argparse
Hello everyone,
Let us consider this patch of code:
import argparse
def parse_cli() -> argparse.Namespace:
parser = argparse.ArgumentParser()
parser.add_argument('n', type=int)
return parser.parse_args()
args = parse_cli()
print(args.n + ' ') # type error
Running CPython on it will raise a TypeError, and running Mypy on it
will indicate that no issues were found.
I was wondering if there is any way for me to have mypy detecting the
args.n type, based on the type keyword of the parser.add_argument function ?
It appears that some type annotations were added to tierce party
modules, provided by mypy itself. Is there a technical issue preventing
such work to be made for argparse (or other CLI ; i didn't find anything
for others either)
Thank you for reading,
--lucas
--
https://mail.python.org/mailman/listinfo/python-list
Re: argparse support of/by argparse
On Tue, Jul 13, 2021 at 5:22 AM lucas wrote:
>
> Hello everyone,
>
> Let us consider this patch of code:
>
> import argparse
>
> def parse_cli() -> argparse.Namespace:
> parser = argparse.ArgumentParser()
> parser.add_argument('n', type=int)
> return parser.parse_args()
>
> args = parse_cli()
> print(args.n + ' ') # type error
>
> Running CPython on it will raise a TypeError, and running Mypy on it
> will indicate that no issues were found.
>
> I was wondering if there is any way for me to have mypy detecting the
> args.n type, based on the type keyword of the parser.add_argument function ?
>
> It appears that some type annotations were added to tierce party
> modules, provided by mypy itself. Is there a technical issue preventing
> such work to be made for argparse (or other CLI ; i didn't find anything
> for others either)
>
Seems complicated, since it depends on a lot of run-time information.
What if you flip the problem on its head? Instead of creating the
argparser and letting that govern the types, maybe create a dataclass,
and then programmatically build the parser.
from dataclasses import dataclass
import argparse
@dataclass
class Args:
n: int
def parse_cli() -> Args:
parser = argparse.ArgumentParser()
for field, typ in Args.__dataclass_fields__.items():
if hasattr(typ, "type"): typ = typ.type # Python 3.10 changed
things a bit
parser.add_argument(field, type=typ)
return Args(**vars(parser.parse_args()))
args = parse_cli()
print(args.n + ' ')
Only barely tested it and didn't try MyPy, but that's the basic idea.
You'd have to figure out the tidiest way to define all the other
attributes of your arguments (help text, etc), but ideally, all the
info should be able to be coded in the dataclass.
Incidentally, you could choose to make parse_cli into a classmethod of
Args. Might be cleaner.
ChrisA
--
https://mail.python.org/mailman/listinfo/python-list
