gpt4 book ai didi

python - 将字符串追加到列表中的字符串

转载 作者:行者123 更新时间:2023-11-28 19:46:54 26 4
gpt4 key购买 nike

我正在读取一个 excel 表格:

enter image description here

import pandas as pd

df = pd.read_excel('file.xlsx', usecols = 'A,B,C')
print(df)

现在我想创建一个列表,其中表中的每一行都是字符串。此外,我想在列表中每个字符串的末尾添加一个“X”:

keylist = []
list1, list2, list3 = df['A'].tolist(), df['B'].tolist(), df['C'].tolist()

for i in zip(list1, list2, list3):
val = map(str, i)
keylist.append('/'.join(val))
keylist += 'X'

print(keylist)

除“添加 X”部分外,一切正常。这导致:

['blue/a/a1', 'X', 'blue/a/a2', 'X', ....

但我想要的是:

['blue/a/a1/X', 'blue/a/a2/X',

谢谢。

最佳答案

我认为更好的是:

d = {'A': ['blue', 'blue', 'blue', 'red', 'red', 'red', 'yellow', 
'yellow', 'green', 'green', 'green'],
'B': ['a', 'a', 'b', 'c', 'c', 'c', 'd', 'e', 'f', 'f', 'g'],
'C': ['a1', 'a2', 'b1', 'c1', 'c2', 'c3', 'd1', 'e1', 'f1', 'f2', 'g1']}
df = pd.DataFrame(d)
print (df)
A B C
0 blue a a1
1 blue a a2
2 blue b b1
3 red c c1
4 red c c2
5 red c c3
6 yellow d d1
7 yellow e e1
8 green f f1
9 green f f2
10 green g g1

keylist = df.apply(lambda x: '/'.join(x), axis=1).add('/X').values.tolist()
print (keylist)

['blue/a/a1/X', 'blue/a/a2/X', 'blue/b/b1/X', 'red/c/c1/X', 'red/c/c2/X',
'red/c/c3/X', 'yellow/d/d1/X', 'yellow/e/e1/X',
'green/f/f1/X', 'green/f/f2/X', 'green/g/g1/X']

或者如果只有几列:

keylist = (df['A'] + '/' + df['B'] + '/' + df['C'] + '/X').values.tolist()

一些时间:

#[110000 rows x 3 columns]
df = pd.concat([df] * 10000, ignore_index=True)

In [364]: %%timeit
...: (df['A'] + '/' + df['B'] + '/' + df['C'] + '/X').values.tolist()
...:
60.2 ms ± 1.04 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

In [365]: %%timeit
...: df.apply(lambda x: '/'.join(x), axis=1).add('/X').tolist()
...:
2.48 s ± 39.1 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)


In [366]: %%timeit
...: list1, list2, list3 = df['A'].tolist(), df['B'].tolist(), df['C'].tolist()
...: for i in zip(list1, list2, list3):
...: val = map(str, i)
...: keylist.append('/'.join(val))
...: keylist[-1] += '/X'
...:
192 ms ± 78.5 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

In [367]: %%timeit
...: df.iloc[:,0].str.cat([df[c] for c in df.columns[1:]],sep='/').tolist()
...:
61.1 ms ± 540 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)

In [368]: %%timeit
...: df.assign(New='X').apply('/'.join,1).tolist()
...:
2.51 s ± 76.8 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

In [369]: %%timeit
...: ['{0}/{1}/{2}/X'.format(i, j, k) for i, j, k in df.values.tolist()]
74.6 ms ± 2.27 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

关于python - 将字符串追加到列表中的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49635591/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com