如何在Python列表中组合两行(How to combine two rows in Python list)
假设我有一个2D列表,
a= [['a','b','c',1],
['a','b','d',2],
['a','e','d',3],
['a','e','c',4]]
我想获得一个列表,如果行中的前两个元素相同,则将第四个元素相加,删除第三个元素并将这些行组合在一起,如下所示,
b = [['a','b',3],
['a','e',7]]
最有效的方法是什么?
Suppose I have a 2D list,
a= [['a','b','c',1],
['a','b','d',2],
['a','e','d',3],
['a','e','c',4]]
I want to obtain a list such that if the first two elements in rows are identical, sum the fourth element, drop the third element and combine these rows together, like the following,
b = [['a','b',3],
['a','e',7]]
What is the most efficient way to do this?
原文:https://stackoverflow.com/questions/37888218
更新时间:2020-10-10 15:10
最满意答案
用pandas的groupby :
import pandas as pd
df = pd.DataFrame(a)
df.groupby([0, 1]).sum().reset_index().values.tolist()
输出:
df.groupby([0, 1]).sum().reset_index().values.tolist()
Out[19]: [['a', 'b', 3L], ['a', 'e', 7L]]
Using pandas's groupby:
import pandas as pd
df = pd.DataFrame(a)
df.groupby([0, 1]).sum().reset_index().values.tolist()
Output:
df.groupby([0, 1]).sum().reset_index().values.tolist()
Out[19]: [['a', 'b', 3L], ['a', 'e', 7L]]
2016-06-17
相关问答
ip_dict = dict(ip)
print([(user, ip_dict.get(user, dns)) for user, dns in dyndns])
这输出: [('user1', '1.1.1.1'), ('user2', '1.1.1.2'), ('user3', 'dyndns3'), ('user4', '1.1.1.4')]
ip_dict = dict(ip)
print([(user, ip_dict.get(user, dns)) for user, dns i
...
一个简单的解决方案是在联合中执行此操作 - 在这种情况下, selected = false行数也无关紧要: select min(uniqueid) as uniqueid,
itemid,
sum(quantity) as quantity,
false as selected
from a
where selected
group by itemid
union all
select uniqueid,
itemid,
...
这不会创建新的列表,只是修改现有的列表。 l = ['a', 'b', 'c', 'a', 'd']
for i in range(len(l)-2, -1, -1):
if l[i] == 'a':
l[i] = l[i] + l.pop(i+1)
print(l)
This does not create a new list, just modifies the existing one. l = ['a', 'b', 'c', 'a', 'd']
for i in
...
要df.iloc特定行,您可以使用df.iloc : res1 = df.iloc[[3, 4], :].sum()
# Col1 26.0
# Col2 12.0
# Col3 9.3
# Col4 1.3
# dtype: float64
res2 = df.iloc[[6, 7], :].sum()
# Col1 24.0
# Col2 15.0
# Col3 8.1
# Col4 3.1
# dtype: float64
如果
...
如何这样的事情: >>> import itertools
>>> foo = [[1, 2, 3], [4, 5, 6], [7, 8, 8]]
>>> for p in itertools.permutations(foo, 2):
... print zip(*p)
...
[(1, 4), (2, 5), (3, 6)]
[(1, 7), (2, 8), (3, 8)]
[(4, 1), (5, 2), (6, 3)]
[(4, 7), (5, 8), (6, 8)]
[(7,
...
在许多情况下,评论显示有多种不同的分组选项。 一个简单的选择是以数字顺序形成组迭代通道,如果通道不能适合现有组,则创建一个新组。 它不会产生最大距离,但它将保证生成最小数量的组: def combine_channels(channels, dist):
result = {}
replacements = {}
groups = []
group = []
key = None
# Iterate through channels in asce
...
您可以在Series使用pd.concat并调用tolist : In [144]:
s = pd.Series([pd.DataFrame(data=np.random.randn(5,3), columns=list('abc')), pd.DataFrame(data=np.random.randn(5,3), columns=list('abc')), pd.DataFrame(data=np.random.randn(5,3), columns=list('abc'))])
s
Out
...
这是最简单的解决方案。 >>> S1 = [1,2,3]
>>> S2 = [4,5,6,7]
>>> S = [S1, S2]
>>> S
[[1, 2, 3], [4, 5, 6, 7]]
要重新获取列表: >>> S1 = S[0]
>>> S2 = S[1]
>>> S1
[1, 2, 3]
>>> S2
[4, 5, 6, 7]
This is the simplest solution. >>> S1 = [1,2,3]
>>> S2 = [4,5,6,7]
>>> S = [S1
...
在python中(无关紧要2或3)你可以做类似的事情 list1=['1A','1B','1C','1D','1E']
list2=['_foo','_bar','_baz','_qux']
_foo=['30','40']
_bar=[]
_baz=['60','70']
_qux=[]
comb = {}
# NEW
for _i in list2:
comb[_i] = globals()[_i]
for _i in list1:
output=[]
for _j
...
用pandas的groupby : import pandas as pd
df = pd.DataFrame(a)
df.groupby([0, 1]).sum().reset_index().values.tolist()
输出: df.groupby([0, 1]).sum().reset_index().values.tolist()
Out[19]: [['a', 'b', 3L], ['a', 'e', 7L]]
Using pandas's groupby: import pan
...