样本数据
import pandas as pd
# test data
df_1 = pd.DataFrame({'x': ['a', 'b', 'c', 'd'], 'y': ['e', 'f', 'g', 'h']})
','.join([f'{v}' for v in (df_1.x + df_1.y).map(tuple).values]) + ';'
','.join([f'{v}' for v in (df_1.sum(axis=1)).map(tuple).values]) + ';'
- 我最初的假设是这两个选项会最快,因为它们不使用循环或列表理解,但显然
.to_string()
相对较慢。
- 使用整个数据框,或使用
.loc
指定列,使用.sum(axis=1)
,将总和映射到 a tuple
,并输出到 a str
with .to_string(index=False)
。
- 这导致
'(a, e)\n(b, f)\n(c, g)\n(d, h)'
so\n
被替换为,
.
# use .loc to specify specific columns
df_1.loc[:, ['x', 'y']].sum(axis=1).map(tuple).to_string(index=False).replace('\n', ',') + ';'
# use this option to sum all columns
df_1.sum(axis=1).map(tuple).to_string(index=False).replace('\n', ',') + ';'
# resulting output of each
'(a, e),(b, f),(c, g),(d, h);'
%%timeit
# sample data with 400k rows
df_1 = pd.DataFrame({'x': ['a', 'b', 'c', 'd'], 'y': ['e', 'f', 'g', 'h']})
df = pd.concat([df_1] * 100000).reset_index(drop=True)
# Cameron
%%timeit -r1 -n1 -q -o
cameron(df)
[out]:
<TimeitResult : 337 ms ± 0 ns per loop (mean ± std. dev. of 1 run, 1 loop each)>
# Trenton
%%timeit -r1 -n1 -q -o
','.join([f'{v}' for v in (df.sum(axis=1)).map(tuple).values]) + ';'
[out]:
<TimeitResult : 391 ms ± 0 ns per loop (mean ± std. dev. of 1 run, 1 loop each)>
# xxdil
%%timeit -r1 -n1 -q -o
xxdil(df)
[out]:
<TimeitResult : 5.36 s ± 0 ns per loop (mean ± std. dev. of 1 run, 1 loop each)>
# ifly6
%%timeit -r1 -n1 -q -o
re.sub(r'[\[\] ]', '', ''.join(str([tuple(t) for _, t in df.iterrows()])) + ';')
[out]:
<TimeitResult : 34.8 s ± 0 ns per loop (mean ± std. dev. of 1 run, 1 loop each)>
# Trenton
%%timeit -r1 -n1 -q -o
df.sum(axis=1).map(tuple).to_string(index=False).replace('\n', ',') + ';'
[out]:
<TimeitResult : 49.6 s ± 0 ns per loop (mean ± std. dev. of 1 run, 1 loop each)>
功能
def cameron(df_1):
all_pairs = []
for pair in zip(df_1["x"], df_1["y"]):
pair_str = "({})".format(",".join(pair))
all_pairs.append(pair_str)
return ",".join(all_pairs) + ";"
def xxdil(df_1):
ans = ""
for i in range(df_1.shape[0]):
ans += '(' + df_1['x'][i] + ',' + df_1['y'][i] + '),'
return ans[:-1] + ';'