Pandas,将两列中的唯一值合并为一列,同时保留顺序
Pandas, combine unique value from two column into one column while preserving order
我的数据分为四列,如下所示。第 1 列中存在一些值,第 1 列的某些值在第 3 列中再次重复。我想将第 1 列与第 3 列合并,同时从第 3 列中删除重复项。我还想保留顺序列。第 1 列与第 2 列相关联,第 3 列与第 4 列相关联,所以如果我可以在合并期间将第 1 列项目与第 2 列和第 3 列项目与第 4 列移动,那就太好了。任何帮助将不胜感激。
输入table:
Item
Price
Item
Price
Car
105
Truck
54822
Chair
20
Pen
1
Cup
2
Car
105
Glass
1
输出table:
Item
Price
Car
105
Chair
20
Cup
2
Truck
54822
Pen
1
Glass
1
提前致谢。
将输入 table 分成左右部分后,我们可以使用 boolean indexing:
非常简单地将左侧项目与未重复的右侧项目连接起来
import pandas as pd
# this initial section only recreates your sample input table
from io import StringIO
input = pd.read_table(StringIO("""| Item | Price | Item | Price |
|-------|-------|------|-------|
| Car | 105 | Truck| 54822 |
| Chair | 20 | Pen | 1 |
| Cup | 2 | Car | 105 |
| | | Glass| 1 |
"""), ' *\| *', engine='python', usecols=[1,2,3,4], skiprows=[1], keep_default_na=False)
input.columns = list(input.columns[:2])*2
# now separate the input table into the left and right part
left = input.iloc[:,:2].replace("", pd.NA).dropna().set_index('Item')
right = input.iloc[:,2:] .set_index('Item')
# finally construct the output table by concatenating without duplicates
output = pd.concat([left, right[~right.index.isin(left.index)]])
Price
Item
Car 105
Chair 20
Cup 2
Truck 54822
Pen 1
Glass 1
我的数据分为四列,如下所示。第 1 列中存在一些值,第 1 列的某些值在第 3 列中再次重复。我想将第 1 列与第 3 列合并,同时从第 3 列中删除重复项。我还想保留顺序列。第 1 列与第 2 列相关联,第 3 列与第 4 列相关联,所以如果我可以在合并期间将第 1 列项目与第 2 列和第 3 列项目与第 4 列移动,那就太好了。任何帮助将不胜感激。
输入table:
Item | Price | Item | Price |
---|---|---|---|
Car | 105 | Truck | 54822 |
Chair | 20 | Pen | 1 |
Cup | 2 | Car | 105 |
Glass | 1 |
输出table:
Item | Price |
---|---|
Car | 105 |
Chair | 20 |
Cup | 2 |
Truck | 54822 |
Pen | 1 |
Glass | 1 |
提前致谢。
将输入 table 分成左右部分后,我们可以使用 boolean indexing:
非常简单地将左侧项目与未重复的右侧项目连接起来import pandas as pd
# this initial section only recreates your sample input table
from io import StringIO
input = pd.read_table(StringIO("""| Item | Price | Item | Price |
|-------|-------|------|-------|
| Car | 105 | Truck| 54822 |
| Chair | 20 | Pen | 1 |
| Cup | 2 | Car | 105 |
| | | Glass| 1 |
"""), ' *\| *', engine='python', usecols=[1,2,3,4], skiprows=[1], keep_default_na=False)
input.columns = list(input.columns[:2])*2
# now separate the input table into the left and right part
left = input.iloc[:,:2].replace("", pd.NA).dropna().set_index('Item')
right = input.iloc[:,2:] .set_index('Item')
# finally construct the output table by concatenating without duplicates
output = pd.concat([left, right[~right.index.isin(left.index)]])
Price
Item
Car 105
Chair 20
Cup 2
Truck 54822
Pen 1
Glass 1