Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.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
String 将列表与另一个不带循环的列表合并_String_Python 3.x_Pandas_Join_Merge - Fatal编程技术网

String 将列表与另一个不带循环的列表合并

String 将列表与另一个不带循环的列表合并,string,python-3.x,pandas,join,merge,String,Python 3.x,Pandas,Join,Merge,我有两个熊猫系列,看起来像这样: import pandas as pd listA = [5,4,3] listB = ["a","b","c"] s = pd.Series(listA) print(s) p = pd.Series(listB) print(p) listTogether = ["a5","a4","a3","b5","b4","b3","c5","c4","c3"] t = pd.Series(listTogether) print(t) 我想得到一个由两个列表混合而

我有两个熊猫系列,看起来像这样:

import pandas as pd
listA = [5,4,3]
listB = ["a","b","c"]
s = pd.Series(listA)
print(s)
p = pd.Series(listB)
print(p)
listTogether = ["a5","a4","a3","b5","b4","b3","c5","c4","c3"]
t = pd.Series(listTogether)
print(t)
我想得到一个由两个列表混合而成的字符串列表,如下所示:

import pandas as pd
listA = [5,4,3]
listB = ["a","b","c"]
s = pd.Series(listA)
print(s)
p = pd.Series(listB)
print(p)
listTogether = ["a5","a4","a3","b5","b4","b3","c5","c4","c3"]
t = pd.Series(listTogether)
print(t)
你有什么提示吗?可以通过避免循环来实现吗


提前非常感谢您的帮助

来自
多索引的技巧

listTogether = pd.MultiIndex.from_product([p,s.astype(str)]).map(''.join).tolist()
listTogether 
Out[242]: ['a5', 'a4', 'a3', 'b5', 'b4', 'b3', 'c5', 'c4', 'c3']

来自多索引的技巧

listTogether = pd.MultiIndex.from_product([p,s.astype(str)]).map(''.join).tolist()
listTogether 
Out[242]: ['a5', 'a4', 'a3', 'b5', 'b4', 'b3', 'c5', 'c4', 'c3']

使用
zip
可能会对您有所帮助

您可以对列表执行类似的操作,但它涉及for循环:

listTogether = ['{}{}'.format(a,b) for (a,b) in zip(listA,listB)]

使用
zip
可能会对您有所帮助

您可以对列表执行类似的操作,但它涉及for循环:

listTogether = ['{}{}'.format(a,b) for (a,b) in zip(listA,listB)]

您可以使用itertools产品

from itertools import product

pd.DataFrame(list(product(p.tolist(),s.astype(str).tolist()))).apply(''.join, axis = 1).tolist()

839 µs ± 18.8 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

['a5', 'a4', 'a3', 'b5', 'b4', 'b3', 'c5', 'c4', 'c3']
如果您想要一个非常高效的解决方案,请选择纯python方式

[''.join(i) for i in list(product(p.tolist(),s.astype(str).tolist()))]
79 µs ± 924 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

您可以使用itertools产品

from itertools import product

pd.DataFrame(list(product(p.tolist(),s.astype(str).tolist()))).apply(''.join, axis = 1).tolist()

839 µs ± 18.8 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

['a5', 'a4', 'a3', 'b5', 'b4', 'b3', 'c5', 'c4', 'c3']
如果您想要一个非常高效的解决方案,请选择纯python方式

[''.join(i) for i in list(product(p.tolist(),s.astype(str).tolist()))]
79 µs ± 924 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

不管你喜欢与否,你都在循环

[f'{b}{a}' for b in listB for a in listA]

['a5', 'a4', 'a3', 'b5', 'b4', 'b3', 'c5', 'c4', 'c3']

不管你喜欢与否,你都在循环

[f'{b}{a}' for b in listB for a in listA]

['a5', 'a4', 'a3', 'b5', 'b4', 'b3', 'c5', 'c4', 'c3']

你试过什么?这可以通过
zip
和comprehension/
map
实现。您尝试了什么?这可以通过
zip
和理解地图来实现。谢谢,这正是我想要的。我一直在努力学习如何使用多索引。谢谢谢谢,这正是我想要的。我一直在努力学习如何使用多索引。谢谢