组合列使单词成为列值v列值

时间:2017-12-04 08:46:36

标签: python python-3.x pandas

我想让df1合并AA, AB and vdf3["C"] = "v"通过 AA AB AC AD AE 0 fry pan calendar cakery chock 1 a pan calendar cakery chock 2 fry pan calendar cakery chock 3 a pan calendar cakery chock 添加。

DF1

    AA   AB        AC      AD     AE   AF
0  fry  pan  calendar  cakery  chock   fry v pan
1    a  pan  calendar  cakery  chock   a v pan
2  fry  pav  calendar  cakery  chock   fry v pav
3    a  pan  calendar  cakery  chock   a v pab

想要df3:

('C', 'occurred at index 0')

KeyError:

df3["C"] = np.nan
df3["C"] = "v"
AAAAAH = df1['combined']=df1.apply(lambda x:'%s_%s_%s' % (x['AA'],x['C'],x['AB']),axis=1)
df3['AB']=AAAAAH

在:

woocommerce_product_query

1 个答案:

答案 0 :(得分:0)

您可以合并不含apply的列:

df['new'] = df['AA'] + ' v ' + df['AB']
print (df)

    AA   AB        AC      AD     AE        new
0  fry  pan  calendar  cakery  chock  fry v pan
1    a  pan  calendar  cakery  chock    a v pan
2  fry  pan  calendar  cakery  chock  fry v pan
3    a  pan  calendar  cakery  chock    a v pan

或者如果需要分配到AB列:

df['AB'] = df['AA'] + ' v ' + df['AB']
print (df)
    AA         AB        AC      AD     AE
0  fry  fry v pan  calendar  cakery  chock
1    a    a v pan  calendar  cakery  chock
2  fry  fry v pan  calendar  cakery  chock
3    a    a v pan  calendar  cakery  chock