简体   繁体   English

在 Pandas 中合并列并排序

[英]Combine columns and sort in Pandas

For example lets say I have a table with columns 1, 2, and 3. Column 1 contains dog, cat, dog.例如,假设我有一个包含第 1、2 和 3 列的表。第 1 列包含狗、猫、狗。 I want to combine columns 2 and 3 as such我想这样组合第 2 列和第 3 列

chars = ['column2', 'column3']
csv['combined'] = csv[chars].apply(lambda row: ','.join(row.values.astype(str)), axis=1)

Now lets say column2 contains a in row 1, b in row 2, and c in row 3. Let's say column3 contains b in row 1, c in row 2, and a in row 3. I want to sort and have row 1 show up (a, b) followed by row 3 which should be (a, c) as well.现在假设 column2 在第 1 行包含 a,在第 2 行包含 b,在第 3 行包含 c。假设 column3 在第 1 行包含 b,在第 2 行包含 c,在第 3 行包含 a。我想排序并让第 1 行显示up (a, b) 后跟第 3 行,也应该是 (a, c)。 I can't get the a in row 3 to take precedence even when I try sort_values as such:即使我尝试这样的 sort_values,我也无法让第 3 行中的 a 优先:

csv['combined'] = csv['combined'].sort_values()

Ultimately I want to group by column 1 and then aggregate.最终我想按第 1 列分组,然后聚合。 In the end I should see (dog, a,c), (dog, a,c), and (cat, b, c)最后我应该看到(dog, a,c), (dog, a,c), and (cat, b, c)

You want to sort the rows in increasing order.您想按升序对行进行排序。 In that case, you can try np.sort :在这种情况下,您可以尝试np.sort

cols = ['column2','column3']

df[cols] = np.sort(df[cols], axis=1)

Output: Output:

  column1 column2 column3
0     dog       a       b
1     cat       b       c
2     dog       a       c

Hope this helps, Happy Coding:)希望这会有所帮助,快乐编码:)

df = pd.DataFrame(data=[['dog','a','b'],['cat','b','c']['dog','c','a']],columns=(['animal','column1','column2']))
print("Actual DataFrame \n")
print(df)

for i in range(len(df)):
  if df.loc[i]['column1']>df.loc[i]['column2']:
      tmp=df.loc[i]['column1'] 
      df.loc[i]['column1'] =df.loc[i]['column2']
      df.loc[i]['column2'] = tmp

print("\n Transformed DataFrame \n")
print(df.sort_values(by='animal',ascending=False))

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM