我有一个dataframe
tdf。它有几个列,其中三个是X,Y,Z
我想收集每一行,把X,Y,Z的值作为一个元组传递给一个函数。你知道吗
一开始我试过:
def format_tuple(x):
print(x)
return x
tmdf = tdf[['X', 'Y', 'Z']].applymap(format_tuple)
但是,这段代码将每个列“X”、“Y”、“Z”单独处理,如print(x)
中所示,单独打印每个列的值,而不是将三列转换为一行tuple
。你知道吗
然后我想,把这些值变成这样的tuple
,但它不起作用:
tmdf = tdf[['X', 'Y', 'Z']].apply(tuple, axis=1).applymap(format_tuple)
applymap
用于元素转换。根据您的需求,沿第一个轴使用apply
:注意the first group is duplicated for performance reasons。你知道吗
相关问题 更多 >
编程相关推荐