如何交换包含列表且大小不同的两个Pandas数据帧中的行?

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

How to swap rows in 2 pandas dataframes which contain lists and have different size?

问题

You can achieve this in Python using the Pandas library. Here's the code to swap the second element of each row of df1 with the second element of each row of df2, considering the differing number of rows:

import pandas as pd

# Your df1 and df2 dataframes

# Create a dictionary to store the new rows for df1
new_rows_df1 = {}

# Iterate through the rows of df2 and update df1 accordingly
for index, row in df2.iterrows():
    if index in df1.index:
        new_row = df1.loc[index].copy()  # Copy the row from df1
        new_row['Path'][1] = row['Path'][1]  # Swap the second element
        new_rows_df1[index] = new_row

# Concatenate the updated rows with the original df1
updated_df1 = pd.concat([df1, pd.DataFrame.from_dict(new_rows_df1, orient='index')])

# Sort the index to maintain the original order
updated_df1.sort_index(inplace=True)

# Your updated df1 and df2 are now in 'updated_df1' and 'df2'
print(updated_df1)
print(df2)

This code will give you the desired output with swapped second elements in df1 while maintaining the rows that don't have corresponding rows in df2.

英文:

I have two Pandas dataframes, df1 and df2. Each dataframe has one column named 'Path'. Each row has a list. They are like this:

df1

Path
[OAK, ORD, FLL, PBG]
[OAK, SEA, FLL, PBG]
[OAK, AUS, FLL, PBG]
[OAK, LAS, FLL, PBG]
[OAK, LAX, FLL, PBG]
[OAK, DAL, FLL, PBG]
[OAK, MDW, FLL, PBG]
[OAK, BWI, FLL, PBG]

The df1 constructor is:

{'Path': {0: ['OAK', 'ORD', 'FLL', 'PBG'],   2: ['OAK', 'SEA', 'FLL', 'PBG'],   4: ['OAK', 'AUS', 'FLL', 'PBG'],   6: ['OAK', 'LAS', 'FLL', 'PBG'],   8: ['OAK', 'LAX', 'FLL', 'PBG'],   10: ['OAK', 'DAL', 'FLL', 'PBG'],   12: ['OAK', 'MDW', 'FLL', 'PBG'],   14: ['OAK', 'BWI', 'FLL', 'PBG']}}

df2

Path
[OAK, DFW, FLL, PBG]
[OAK, JFK, FLL, PBG]
[OAK, MCI, FLL, PBG]
[OAK, PHX, FLL, PBG]
[OAK, DEN, FLL, PBG]
[OAK, HOU, FLL, PBG]
[OAK, ATL, FLL, PBG]

The df2 constructor is:

{'Path': {1: ['OAK', 'DFW', 'FLL', 'PBG'], 3: ['OAK', 'JFK', 'FLL', 'PBG'], 5: ['OAK', 'MCI', 'FLL', 'PBG'], 7: ['OAK', 'PHX', 'FLL', 'PBG'], 9: ['OAK', 'DEN', 'FLL', 'PBG'], 11: ['OAK', 'HOU', 'FLL', 'PBG'], 13: ['OAK', 'ATL', 'FLL', 'PBG']}}

One problem is that I have a different number of rows in my dataframes. I would like to swap the second element of each row of df1 with the second element of each row of df2. If there is no corresponding row, the row should not be modified or dropped. The desired output is:

df1

Path
[OAK, DFW, FLL, PBG]
[OAK, JFK, FLL, PBG]
[OAK, MCI, FLL, PBG]
[OAK, PHX, FLL, PBG]
[OAK, DEN, FLL, PBG]
[OAK, HOU, FLL, PBG]
[OAK, ATL, FLL, PBG]
[OAK, BWI, FLL, PBG]

df2

Path
[OAK, ORD, FLL, PBG]
[OAK, SEA, FLL, PBG]
[OAK, AUS, FLL, PBG]
[OAK, LAS, FLL, PBG]
[OAK, LAX, FLL, PBG]
[OAK, DAL, FLL, PBG]
[OAK, MDW, FLL, PBG]

How can I do it in Python?

答案1

得分: 2

你可以在将列表系列转换为数据帧后,在此处使用combine_first()

n = pd.DataFrame(df2['Path'].tolist())
m = pd.DataFrame(df1['Path'].tolist())
# ----------------------------------------------------
df1_final = n[[1]].combine_first(m).dropna().agg(list, 1)
df2_final = m[[1]].combine_first(n).dropna().agg(list, 1)
print(df1_final)
print('\n')
print(df2_final)

结果如下:

0    [OAK, DFW, FLL, PBG]
1    [OAK, JFK, FLL, PBG]
2    [OAK, MCI, FLL, PBG]
3    [OAK, PHX, FLL, PBG]
4    [OAK, DEN, FLL, PBG]
5    [OAK, HOU, FLL, PBG]
6    [OAK, ATL, FLL, PBG]
7    [OAK, BWI, FLL, PBG]
dtype: object

0    [OAK, ORD, FLL, PBG]
1    [OAK, SEA, FLL, PBG]
2    [OAK, AUS, FLL, PBG]
3    [OAK, LAS, FLL, PBG]
4    [OAK, LAX, FLL, PBG]
5    [OAK, DAL, FLL, PBG]
6    [OAK, MDW, FLL, PBG]
dtype: object
英文:

You can use combine_first() here after converting the series of list into a dataframe:

n=pd.DataFrame(df2['Path'].tolist())
m=pd.DataFrame(df1['Path'].tolist())
#----------------------------------------------------
df1_final=n[[1]].combine_first(m).dropna().agg(list,1)
df2_final=m[[1]].combine_first(n).dropna().agg(list,1)

print(df1_final)
print('\n')
print(df2_final)

0    [OAK, DFW, FLL, PBG]
1    [OAK, JFK, FLL, PBG]
2    [OAK, MCI, FLL, PBG]
3    [OAK, PHX, FLL, PBG]
4    [OAK, DEN, FLL, PBG]
5    [OAK, HOU, FLL, PBG]
6    [OAK, ATL, FLL, PBG]
7    [OAK, BWI, FLL, PBG]
dtype: object


0    [OAK, ORD, FLL, PBG]
1    [OAK, SEA, FLL, PBG]
2    [OAK, AUS, FLL, PBG]
3    [OAK, LAS, FLL, PBG]
4    [OAK, LAX, FLL, PBG]
5    [OAK, DAL, FLL, PBG]
6    [OAK, MDW, FLL, PBG]
dtype: object

huangapple
  • 本文由 发表于 2020年1月3日 22:56:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/59580730.html
匿名

发表评论

匿名网友

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

确定