[英]Concat two columns values of dataframe
I have a dataframe as below:我有一个 dataframe 如下:
Name Age
0 Alex 10
1 Bob 12
2 Clarke 13
The dataframe is created as: dataframe 创建为:
data = [['Alex',10],['Bob',12],['Clarke',13]]
df = pd.DataFrame(data,columns=['Name','Age'])
I want to perform string concatenation of the two columns as:我想将两列的字符串连接为:
Name Age
0 10 Alex 10
1 12 Bob 12
2 13 Clarke 13
I tried using df["Name"] = df["Age"]+" "+df["Name"]
, which resulted in the below error:我尝试使用df["Name"] = df["Age"]+" "+df["Name"]
,这导致了以下错误:
Traceback (most recent call last): File "", line 1, in File "/anaconda3/envs/env1/lib/python3.6/site-packages/pandas/core/ops/common.py", line 65, in new_method return method(self, other) File "/anaconda3/envs/env1/lib/python3.6/site-packages/pandas/core/ops/ init .py", line 343, in wrapper result = arithmetic_op(lvalues, rvalues, op) File "/anaconda3/envs/env1/lib/python3.6/site-packages/pandas/core/ops/array_ops.py", line 189, in arithmetic_op res_values = na_arithmetic_op(lvalues, rvalues, op) File "/anaconda3/envs/env1/lib/python3.6/site-packages/pandas/core/ops/array_ops.py", line 149, in na_arithmetic_op result = masked_arith_op(left, right, op) File "/anaconda3/envs/env1/lib/python3.6/site-packages/pandas/core/ops/array_ops.py", line 111, in masked_arith_op result[mask] = op(xrav[mask], y) numpy.core._exceptions.UFuncTypeError: ufunc 'add' did not contain a loop with signature matching types (dtype('<U21'), dtype('<U21')) -> dtype('<U21')追溯(最近调用最后):文件“”,第 1 行,在文件“/anaconda3/envs/env1/lib/python3.6/site-packages/pandas/core/ops/common.py”,第 65 行,在new_method return method(self, other) 文件“/anaconda3/envs/env1/lib/python3.6/site-packages/pandas/core/ops/ init .py”,第 343 行,包装结果 = arithmetic_op(lvalues, rvalues , op) 文件“/anaconda3/envs/env1/lib/python3.6/site-packages/pandas/core/ops/array_ops.py”,第 189 行,in arithmetic_op res_values = na_arithmetic_op(lvalues, rvalues, op) File “ /anaconda3/envs/env1/lib/python3.6/site-packages/pandas/core/ops/array_ops.py”,第 149 行,在 na_arithmetic_op result = masked_arith_op(left, right, op) File “/anaconda3/envs/ env1/lib/python3.6/site-packages/pandas/core/ops/array_ops.py”,第 111 行,在 masked_arith_op result[mask] = op(xrav[mask], y) numpy.core._exceptions.UFuncTypeError: ufunc 'add' 不包含带有签名匹配类型的循环 (dtype('<U21'), dtype('<U21')) -> dtype('<U21')
Since Age
is an int
column, you need to cast it to str
using astype
由于Age
是一个int
列,您需要使用astype
将其转换为str
In [2511]: df['Name'] = df["Age"].astype(str) + " " + df["Name"]
In [2511]: df['Name']
Out[2511]:
0 10 Alex
1 12 Bob
2 13 Clarke
Series.str.cat
df['Name'] = df['Age'].astype(str).str.cat(df['Name'], sep=' ')
Name Age
0 10 Alex 10
1 12 Bob 12
2 13 Clarke 13
Here is a possible solution:这是一个可能的解决方案:
df['Name'] = df['Age'].map(str) + ' ' + df['Name']
You can use:您可以使用:
df['Name']=df['Age'].apply(lambda x:str(x)) + ' ' + df['Name']
We can use the apply function to cast the dataframe "age" column to string from int and then concatenate it to the "name" column using the "+" operator.我们可以使用 apply function 将 dataframe“age”列从 int 转换为字符串,然后使用“+”运算符将其连接到“name”列。
df["name"] = df["age"].apply(str) + " " + df["name"] df["姓名"] = df["年龄"].apply(str) + " " + df["姓名"]
df["name"] df[“名字”]
The output will look something like this output 看起来像这样
0 10 Alex 1 12 Bob 2 13 Clarke
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.