Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 重复函数以提取类似信息_Python_Json_Pandas - Fatal编程技术网

Python 重复函数以提取类似信息

Python 重复函数以提取类似信息,python,json,pandas,Python,Json,Pandas,下面是我如何使用pandas打开和读取json文件。我真的很欣赏熊猫的力量:) 问题是我需要“Description”和“Default”值以及“Type”字符串。我已经编写了一个函数来提取上面提到的类型。我真的需要再写两个函数吗 def mydescription(mydict): try: if mydict["Description"]: return mydict["Description"] except: pas

下面是我如何使用pandas打开和读取json文件。我真的很欣赏熊猫的力量:)

问题是我需要“Description”和“Default”值以及“Type”字符串。我已经编写了一个函数来提取上面提到的类型。我真的需要再写两个函数吗

def mydescription(mydict):
    try:
        if mydict["Description"]:
            return mydict["Description"]
    except:
        pass


def mydefault(mydict):
    try:
        if mydict["Default"]:
            return mydict["Default"]
    except:
        pass

df["myParametersDescription"] = df.Parameters.apply(lambda x: mydescription(x))
df["myParametersDefault"] = df.Parameters.apply(lambda x: mydefault(x))
如果字典包含超过3个键,我将如何处理它

最终的表格应该是这样的

df.iloc[:,-3::].dropna(how=“all”)


您可以将新参数传递给函数:

def func(mydict, val):
    try:
        if mydict[val]:
            return mydict[val]
    except:
        pass

df["myParametersType"] = df.Parameters.apply(lambda x: func(x, 'Type'))
df["myParametersDescription"] = df.Parameters.apply(lambda x: func(x, 'Description'))
df["myParametersDefault"] = df.Parameters.apply(lambda x: func(x, 'Default'))
df = df.iloc[:, -3:].dropna(how="all")

通过使每一行成为
pd.Series
,您可以为每个字典中的每个键和值创建数据帧。
像这样:

get_df=df['Parameters'].apply(lambda x:pd.Series(x)).drop(0,axis=1)#NaN是colnam 0
get_df.columns=['P_'+get_df.columns中col的col]#您已经有了'Description'列
获取_df.head()

所以,只需粘贴它(使用
concat
或其他方法)

df[get_-df.columns]=get_-df
df.head()

是否有任何(内置)方法来反规范化或分解所有列中的所有词典?@shantanuo-不幸的是,没有,因为反规范化嵌套dict是一些自定义函数所必需的。
myParametersType    myParametersDescription myParametersDefault
pInstanceKeyName    AWS::EC2::KeyPair::KeyName  The name of the private key to use for SSH acc...   None
pTwitterTermList    String  List of terms for twitter to listen to  'your', 'search', 'terms', 'here'
pTwitterLanguages   String  List of languages to use for the twitter strea...   'en'
pTwitterAuthConsumerKey String  Consumer key for access twitter None
pTwitterAuthConsumerSecret  String  Consumer Secret for access twitter  None
pTwitterAuthToken   String  Access Token Secret for calling twitter None
pTwitterAuthTokenSecret String  Access Token Secret for calling twitter None
pApplicationName    String  Name of the application deploying for the EyeO...   EyeOfCustomer
pVpcCIDR    String  Please enter the IP range (CIDR notation) for ...   10.193.0.0/16
pPublicSubnet1CIDR  String  Please enter the IP range (CIDR notation) for ...   10.193.10.0/24
def func(mydict, val):
    try:
        if mydict[val]:
            return mydict[val]
    except:
        pass

df["myParametersType"] = df.Parameters.apply(lambda x: func(x, 'Type'))
df["myParametersDescription"] = df.Parameters.apply(lambda x: func(x, 'Description'))
df["myParametersDefault"] = df.Parameters.apply(lambda x: func(x, 'Default'))
df = df.iloc[:, -3:].dropna(how="all")