是否有办法将pandas中两个数据系列的信息连接起来?不是附加或合并两个数据帧的信息,而是将每个数据序列的内容组合成一个新的数据序列。
的例子:
ColumnA (Item type) Row 2 = 1 (float64)
ColumnB (Item number) Row 2 = 1 (float64)
ColumnC (Registration Date) Row 2 = 04/07/2018 (43285) (datetime64[ns])
在excel中,我会将A
,B
,C
列中的行连接起来,并使用公式=concat(A2, B2, C2)
将每列中的数字组合起来例如,结果将是另一个单元格D2
中的1143285
。
有没有办法让我在Pandas中做到这一点?我只能在数据帧中找到连接、组合或追加序列的方法,但不能在序列本身中找到方法。
你可以使用
df['D'] = df.apply(lambda row : str(row['A'])+
str(row['B']) + str(row['C']), axis = 1)
按照你的例子,它将是
import pandas as pd
d = {'A': [1],'B':[1],'C':[43285]}
df = pd.DataFrame(data=d)
df['D'] = df.apply(lambda row : str(row['A'])+
str(row['B']) + str(row['C']), axis = 1)
输出:
A B C D
0 1 1 43285 1143285