Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.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
Python 3.x 简单应用于concat列_Python 3.x_Pandas_Apply - Fatal编程技术网

Python 3.x 简单应用于concat列

Python 3.x 简单应用于concat列,python-3.x,pandas,apply,Python 3.x,Pandas,Apply,我怎样才能使这个程序工作?结果应为串联整数: import pandas as pd d = pd.DataFrame({'A': range(5), 'B': range(5, 10)}) f = lambda x, y: str(x) + str(y) d[['A', 'B']].apply(f, axis = 1) Thx&p>与数据帧和轴=1一起应用,每个循环生成系列: f = lambda x: str(x.A) + str(x.B) a = d[['A', 'B']].apply(

我怎样才能使这个程序工作?结果应为串联整数:

import pandas as pd
d = pd.DataFrame({'A': range(5), 'B': range(5, 10)})
f = lambda x, y: str(x) + str(y)
d[['A', 'B']].apply(f, axis = 1)

Thx&p>与
数据帧
轴=1
一起应用,每个循环生成
系列

f = lambda x: str(x.A) + str(x.B)
a = d[['A', 'B']].apply(f, axis = 1)
非常相似的是,可以先将两列转换为
string
s:

f = lambda x: x.A + x.B
a = d[['A', 'B']].astype(str).apply(f, axis = 1)
您可以通过自定义函数检查处理:

def f(x):
    print (x)
    print (x.A)
    print (x.B)
    return str(x.A) + str(x.B)

a = d[['A', 'B']].apply(f, axis = 1)

A    0
B    5
Name: 0, dtype: int64
0
5
A    1
B    6
Name: 1, dtype: int64
1
6
A    2
B    7
Name: 2, dtype: int64
2
7
A    3
B    8
Name: 3, dtype: int64
3
8
A    4
B    9
Name: 4, dtype: int64
4
9
但更好/更快的方法是使用
+
连接,但首先将数字列强制转换为
字符串
s:

a = d['A'].astype(str) + d['B'].astype(str)


为什么不使用更简单的方法,例如:

d['A'].apply(str) + d['B'].apply(str)

我没有得到的是,我要从d['A','B']]向lambda函数“f”传递什么?Columns=熊猫系列/列表或d.A和d.B的单个元素?不确定是否理解,因此添加了自定义函数以更好地解释。非常感谢,使用您的自定义函数甚至可以让我明白。
d['A'].apply(str) + d['B'].apply(str)