假设我有以下返回元组的函数:
df = pd.DataFrame({'col1': [1,2,3]})
df['test'] = df['col1'].apply(return_tuple)
>>
col1 test
0 1 (1, one)
1 2 (2, two)
2 one (1, one)
如果我使用apply方法,它将返回:
df['test_1'] = df['col1'].apply(return_tuple)??? # get 0-index in tuple
df['test_2'] = df['col1'].apply(return_tuple)??? # get 1 index in tuple
>>
col1 test_1 test_2
0 1 1 one
1 2 2 two
2 one 1 one
但是我想要这样的东西:
/etc/wgetrc
谢谢。
答案 0 :(得分:2)
使用unique_ptr
和元组拆包,在亚历山大和拉兹迪的答案之间的某个地方:
<div class="section active" id="section0">
<gwd-genericad id="gwd-ad">
<gwd-pagedeck class="gwd-page-container" id="pagedeck">
<gwd-page id="page1" class="gwd-page-wrapper gwd-page-size gwd-lightbox" data-gwd-width="100%" data-gwd-height="100%">
<div class="gwd-page-content gwd-page-size">
<gwd-image id="BG-01" source="assets/BG-01.png" scaling="stretch" class="gwd-image-1fyz" data-gwd-tl-locked=""></gwd-image>
<gwd-image id="BG-01-01" source="assets/BG-01-01.png" scaling="stretch" class="gwd-image-q9o2" data-gwd-tl-locked=""></gwd-image>
<gwd-image id="BG-01-01-01" source="assets/BG-01-01-01.png" scaling="stretch" class="gwd-image-qajr gwd-gen-phfngwdanimation"></gwd-image>
<gwd-image id="BG-01-01-01-01" source="assets/BG-01-01-01-01.png" scaling="stretch" class="gwd-image-1d50 gwd-gen-19lhgwdanimation"></gwd-image><span class="gwd-span-1rx3 gwd-gen-148jgwdanimation">SAVE THE DATE</span><span class="gwd-span-wmst gwd-gen-qsz5gwdanimation">WILLIAM & TIEN LI</span>
<gwd-image id="arrow-down-sign-to-navigate-01" source="assets/arrow-down-sign-to-navigate-01.png" scaling="stretch" class="gwd-image-jolg gwd-gen-1kijgwdanimation"></gwd-image>
</div>
</gwd-page>
</gwd-pagedeck>
</gwd-genericad>
</div>
<div class="section" id="section1">
<gwd-page id="page1_1" class="gwd-page-wrapper gwd-lightbox gwd-page-1mqd" data-gwd-width="100%" data-gwd-height="100%">
<div class="gwd-page-content gwd-div-a9om">
<gwd-image id="quaver" source="assets/quaver.png" scaling="stretch" class="gwd-image-ei8t"></gwd-image>
<gwd-audio id="gwd-audio_1" sources="http://k007.kiwi6.com/hotlink/auu01qfyna/beautiful-in-white.mp3" class="gwd-audio-1q64" autoplay="" loop=""></gwd-audio>
</div>
</gwd-page>
</div>
答案 1 :(得分:1)
您也可以一步一步完成>
import pandas as pd
df = pd.DataFrame({'col1': [1,2,3]})
def return_tuple(x):
if x['col1'] in [1,'1','one']:
return pd.Series([1, 'one'])
else:
return pd.Series([2, 'two'])
df[['test_1', 'test_2']] = df.apply(return_tuple, axis=1)
答案 2 :(得分:0)
import pandas as pd
def return_tuple(x):
if x in [1, '1', 'one']:
return 1, 'one'
else:
return 2, 'two'
df_1 = pd.DataFrame({'col1': [1, 2, 3]})
df_1['test_1'] = df_1['col1'].apply(lambda item: return_tuple(item)[0])
df_1['test_2'] = df_1['col1'].apply(lambda item: return_tuple(item)[1])
print(df_1)
就这么简单!
有关lambda函数的更多信息,请参见https://realpython.com/python-lambda/。有关SO的一些相关问题,例如this one。
答案 3 :(得分:0)
您还可以在不更改现有功能的情况下完成此一线工作:
df[['test_1','test_2']] = pd.DataFrame(df['col1'].apply(return_tuple).tolist(),index=df.index)
答案 4 :(得分:0)
经过一些修改,结果如下:
import pandas as pd
def return_tuple(x):
if x in [1, '1', 'one']:
return 1, 'one'
else:
return 2, 'two'
df_1 = pd.DataFrame({'col1': [1, 2, 3]})
df_1['test_1'], df_1['test_2'] = zip(*df_1['col1'].apply(return_tuple))
使用df连接
答案 5 :(得分:0)
您可以将其扩展以执行所需的操作:
def return_tuple(x):
if x in [1,'1','one']:
return (1, 'one')
else:
return (2, 'two')
df = pd.DataFrame({'col1': [1,2,3]})
df['test'] = df['col1'].apply(return_tuple)
df[['test','test2']] = pd.DataFrame(df['test'].to_list(), index=df.index)
Out[32]:
col1 test test2
0 1 1 one
1 2 2 two
2 3 2 two