将多个列合并为一个列在 pandas 中

huangapple go评论77阅读模式
英文:

combine multiple column into one in pandas

问题

我有以下类似的表格

  1. 1 2 3 ...
  2. 0 a 1 2
  3. 1 b 1 3
  4. 2 c 2 1

我想将它转换成以下形式

  1. 1 2
  2. 0 a 1
  3. 1 a 2
  4. 2 b 1
  5. 3 b 3
  6. 4 c 2
  7. 5 c 1
  8. ...

我想要将列2(以及其他列)中的每个值与列1中的值配对。我不知道如何在pandas中做这个操作,甚至不知道从哪里开始。

英文:

I have table like below

  1. Column 1 Column 2 Column 3 ...
  2. 0 a 1 2
  3. 1 b 1 3
  4. 2 c 2 1

and I want to convert it to be like below

  1. Column 1 Column 2
  2. 0 a 1
  3. 1 a 2
  4. 2 b 1
  5. 3 b 3
  6. 4 c 2
  7. 5 c 1
  8. ...

I want to take each value from Column 2 (and so on) and pair it to value in column 1. I have no idea how to do it in pandas or even where to start.

答案1

得分: 3

你可以使用 pd.melt 来执行此操作:

  1. >> df = pd.DataFrame({'A': {0: 'a', 1: 'b', 2: 'c'},
  2. ... 'B': {0: 1, 1: 3, 2: 5},
  3. ... 'C': {0: 2, 1: 4, 2: 6}})
  4. >> df
  5. A B C
  6. 0 a 1 2
  7. 1 b 3 4
  8. 2 c 5 6
  9. >> pd.melt(df, id_vars=['A'], value_vars=['B', 'C'])
  10. A variable value
  11. 0 a B 1
  12. 1 b B 3
  13. 2 c B 5
  14. 3 a C 2
  15. 4 b C 4
  16. 5 c C 6
英文:

You can use pd.melt to do this:

  1. >>> df = pd.DataFrame({'A': {0: 'a', 1: 'b', 2: 'c'},
  2. ... 'B': {0: 1, 1: 3, 2: 5},
  3. ... 'C': {0: 2, 1: 4, 2: 6}})
  4. >>> df
  5. A B C
  6. 0 a 1 2
  7. 1 b 3 4
  8. 2 c 5 6
  9. >>> pd.melt(df, id_vars=['A'], value_vars=['B', 'C'])
  10. A variable value
  11. 0 a B 1
  12. 1 b B 3
  13. 2 c B 5
  14. 3 a C 2
  15. 4 b C 4
  16. 5 c C 6

答案2

得分: 0

以下是您要翻译的代码部分:

  1. import pandas as pd
  2. df = pd.DataFrame({'col1': ['a', 'b', 'c'], 'col2': [1, 1, 2], 'col3': [2, 3, 1]})
  3. new_df = pd.DataFrame(columns=['col1', 'col2'])
  4. for index, row in df.iterrows():
  5. for element in row.values[1:]:
  6. new_df.loc[len(new_df)] = [row[0], element]
  7. print(new_df)

输出:

  1. col1 col2
  2. 0 a 1
  3. 1 a 2
  4. 2 b 1
  5. 3 b 3
  6. 4 c 2
  7. 5 c 1
英文:

Here's my approach, hope it helps:

  1. import pandas as pd
  2. df=pd.DataFrame({'col1':['a','b','c'],'col2':[1,1,2],'col3':[2,3,1]})
  3. new_df=pd.DataFrame(columns=['col1','col2'])
  4. for index,row in df.iterrows():
  5. for element in row.values[1:]:
  6. new_df.loc[len(new_df)]=[row[0],element]
  7. print(new_df)
  8. Output:
  9. col1 col2
  10. 0 a 1
  11. 1 a 2
  12. 2 b 1
  13. 3 b 3
  14. 4 c 2
  15. 5 c 1

huangapple
  • 本文由 发表于 2023年2月8日 16:20:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/75382960.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定