Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/314.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_Python 2.7_Pandas_Dictionary - Fatal编程技术网

Python 对数据帧列中具有相同键的字典求和/乘法

Python 对数据帧列中具有相同键的字典求和/乘法,python,python-2.7,pandas,dictionary,Python,Python 2.7,Pandas,Dictionary,给定一个包含多列字典的数据帧,如何将数据帧中的键相加和/或相乘以得到一列 A B {"ab":1, "b":2, "c":3} {"ab":1, "b":3, "c":5} 所以加在一起你会得到另一个专栏 C {"ab":2, "b":5, "c":8} 或者把你得到的 C {"ab":1, "b":6, "c"

给定一个包含多列字典的数据帧,如何将数据帧中的键相加和/或相乘以得到一列

                       A                        B
   {"ab":1, "b":2, "c":3}   {"ab":1, "b":3, "c":5}
所以加在一起你会得到另一个专栏

                    C
{"ab":2, "b":5, "c":8}
或者把你得到的

                    C
{"ab":1, "b":6, "c":15}

我知道如果它们只是一列数字,我可以使用sum/etc,但是如果它们是字典的列,那么最好的方法是什么呢?lambda函数?

您可以首先将dict扩展到多列DFs
df.A.apply(pd.Series)
,进行算术运算,最后将结果转换回dict:
(result)。到dict('r')

说明:

In [91]: df.A.apply(pd.Series)
Out[91]:
   ab   b   c
0   1   2   3
1  11  12  13

您可以首先将dicts扩展到多列DFs
df.A.apply(pd.Series)
,进行算术运算,最后将结果转换回dict:
(result)。到dict('r')

说明:

In [91]: df.A.apply(pd.Series)
Out[91]:
   ab   b   c
0   1   2   3
1  11  12  13
天真的方法:

使用它可以很好地处理dict或dict列表作为数据输入:

from pandas.io.json import json_normalize 
processed_df = json_normalize(df.T.to_dict('list'), 0)

要查找总和

processed_df.sum()    # Append .to_dict() if you want to render it as a dictionary
ab    2
b     5
c     8
dtype: int64
processed_df.prod()    # Append .to_dict() if you want to render it as a dictionary 
ab     1
b      6
c     15
dtype: int64
df.stack().apply(pd.Series).sum(level=0)
df.stack().apply(pd.Series).prod(level=0)
要查找产品

processed_df.sum()    # Append .to_dict() if you want to render it as a dictionary
ab    2
b     5
c     8
dtype: int64
processed_df.prod()    # Append .to_dict() if you want to render it as a dictionary 
ab     1
b      6
c     15
dtype: int64
df.stack().apply(pd.Series).sum(level=0)
df.stack().apply(pd.Series).prod(level=0)
如果dataframe有太多的列/行,这将是一种过分的杀伤力,尽管可以通过将其合并为具有单个列标题的长格式来克服。但同样,重新调整它以对齐匹配的行并进行计算将是一件非常困难的事情


更普遍的方法:

示例
DF

df = pd.DataFrame({'A': [{"ab":1, "b":2, "c":3}, {'b':4, 'c':5, 'ab':6}], 
                   'B': [{"ab":7, "b":8, "c":9}, {'b':10, 'c':11, 'ab':12}]})
df

计算总和:

processed_df.sum()    # Append .to_dict() if you want to render it as a dictionary
ab    2
b     5
c     8
dtype: int64
processed_df.prod()    # Append .to_dict() if you want to render it as a dictionary 
ab     1
b      6
c     15
dtype: int64
df.stack().apply(pd.Series).sum(level=0)
df.stack().apply(pd.Series).prod(level=0)

计算产品

processed_df.sum()    # Append .to_dict() if you want to render it as a dictionary
ab    2
b     5
c     8
dtype: int64
processed_df.prod()    # Append .to_dict() if you want to render it as a dictionary 
ab     1
b      6
c     15
dtype: int64
df.stack().apply(pd.Series).sum(level=0)
df.stack().apply(pd.Series).prod(level=0)

将其分配回新列:

df['C'] = df.stack().apply(pd.Series).sum(level=0).to_dict('records')
df
天真的方法:

使用它可以很好地处理dict或dict列表作为数据输入:

from pandas.io.json import json_normalize 
processed_df = json_normalize(df.T.to_dict('list'), 0)

要查找总和

processed_df.sum()    # Append .to_dict() if you want to render it as a dictionary
ab    2
b     5
c     8
dtype: int64
processed_df.prod()    # Append .to_dict() if you want to render it as a dictionary 
ab     1
b      6
c     15
dtype: int64
df.stack().apply(pd.Series).sum(level=0)
df.stack().apply(pd.Series).prod(level=0)
要查找产品

processed_df.sum()    # Append .to_dict() if you want to render it as a dictionary
ab    2
b     5
c     8
dtype: int64
processed_df.prod()    # Append .to_dict() if you want to render it as a dictionary 
ab     1
b      6
c     15
dtype: int64
df.stack().apply(pd.Series).sum(level=0)
df.stack().apply(pd.Series).prod(level=0)
如果dataframe有太多的列/行,这将是一种过分的杀伤力,尽管可以通过将其合并为具有单个列标题的长格式来克服。但同样,重新调整它以对齐匹配的行并进行计算将是一件非常困难的事情


更普遍的方法:

示例
DF

df = pd.DataFrame({'A': [{"ab":1, "b":2, "c":3}, {'b':4, 'c':5, 'ab':6}], 
                   'B': [{"ab":7, "b":8, "c":9}, {'b':10, 'c':11, 'ab':12}]})
df

计算总和

processed_df.sum()    # Append .to_dict() if you want to render it as a dictionary
ab    2
b     5
c     8
dtype: int64
processed_df.prod()    # Append .to_dict() if you want to render it as a dictionary 
ab     1
b      6
c     15
dtype: int64
df.stack().apply(pd.Series).sum(level=0)
df.stack().apply(pd.Series).prod(level=0)

计算产品

processed_df.sum()    # Append .to_dict() if you want to render it as a dictionary
ab    2
b     5
c     8
dtype: int64
processed_df.prod()    # Append .to_dict() if you want to render it as a dictionary 
ab     1
b      6
c     15
dtype: int64
df.stack().apply(pd.Series).sum(level=0)
df.stack().apply(pd.Series).prod(level=0)

将其分配回新列:

df['C'] = df.stack().apply(pd.Series).sum(level=0).to_dict('records')
df