Reputation: 2443
Based on this post,
import pandas as pd
inp = [{'c1':10,'cols':{'c2':20,'c3':'str1'}, 'c4':'41'}, {'c1':11,'cols':{'c2':20,'c3':'str2'},'c4':'42'}, {'c1':12,'cols':{'c2':20,'c3':'str3'},'c4':'43'}]
df = pd.DataFrame(inp)
pd.io.json.json_normalize(df.to_dict('records'))
Above script works well.
Make little change to inp
:
inp=[{'c1':10,'cols':{'c2':5,'c3':NaT}, 'c4':'41'}, {'c1':11,'cols':{'c2':Timestamp('2014-06-03 21:19:26'),'c3':'str2'},'c4':'42'}, {'c1':12,'cols':{'c2':20,'c3':'str3'},'c4':'43'}]
df = pd.DataFrame(inp)
pd.io.json.json_normalize(df.to_dict('records'))
I just change str1
to NaT
and 20
to Timestamp('2014-06-03 21:19:26')
,script works not well, got error as below:
NameError: name 'NaT' is not defined
NameError: name 'Timestamp' is not defined
As NaT
is common in real data,what's the problem cause error?
Upvotes: 2
Views: 1641
Reputation: 2755
You should be referring to pd.NaT and pd.Timestamp:
inp=[{'c1':10,'cols':{'c2':5,'c3':pd.NaT}, 'c4':'41'}, {'c1':11,'cols':{'c2':pd.Timestamp('2014-06-03 21:19:26'),'c3':'str2'},'c4':'42'}, {'c1':12,'cols':{'c2':20,'c3':'str3'},'c4':'43'}]
Upvotes: 4