我有两个具有相同模式的Dataframe。我在两个Dataframe上都使用了outerjoin方法,并使用select和coalesce方法来选择和转换所有列。我想迭代select方法中的列列表,而不显式地定义coalesce方法中的每一列。如果知道有没有不使用自定义项的解决方案,那就太好了。正在联接的两个表是 songs
以及 staging_songs
在下面的代码片段中。
而不是像这样明确定义每一列:
updated_songs = songs.join(staging_songs, songs.song_id == staging_songs.song_id, how='full').select(
f.coalesce(staging_songs.song_id, songs.song_id),
f.coalesce(staging_songs.artist_name, songs.artist_name),
f.coalesce(staging_songs.song_name, songs.song_name)
)
按照以下思路做某事:
# column names to iterate over in select method
songs_columns = songs.columns
updated_songs = songs.join(staging_songs, songs.song_id == staging_songs.song_id, how='full').select(
#using for loop like this raises a syntax error
for col in songs_columns:
f.coalesce(staging_songs.col, songs.col))
1条答案
按热度按时间hk8txs481#
试试这个: