简体   繁体   English

如何用来自另一个数据帧的索引替换一个数据帧中的空白索引

[英]How to Replace Blank Indexes in One Dataframe with Indexes From Another Dataframe

I have two dataframes, df1 and df2 .我有两个数据框, df1df2

df1 is scraped data: df1是抓取的数据:

  Name         ID   Symbol
0  AAA   23135106         
1  Bbb  G06242104  String2
2  Ccc  30303M102  String3
3  DDD   2079K305         
4        2079K107  

And df2 is reference data:df2是参考数据:

  Name         ID   Symbol
0  Aaa   23135106  String1
1  Bbb  G06242104  String2
2  Ccc  98980L101  String3
3  Ddd   2079K305  String4
4  Eee   2079K107  String5
5  Fff    287Y109  String6
6  Ggg     380105  String7
7  Hhh  G00349103  String8

By using ID as the key, I want to:通过使用ID作为密钥,我想:

  1. populate the empty Symbols and Names in df1 with those in df2 , anddf2 SymbolsNames填充df1的空SymbolsNames ,以及
  2. replace the malformatted (eg, AAA vs Aaa) Names in df1 with those in df2 ,df2 Names替换df1中的df2 (例如,AAA vs Aaa) Names

so that the end result looks like:以便最终结果如下所示:

  Name         ID   Symbol
0  Aaa   23135106  String1       
1  Bbb  G06242104  String2
2  Ccc  30303M102  String3
3  Ddd   2079K305  String4       
4  Eee   2079K107  String5

fillna and map is what you need: fillnamap是您所需要的:

df1['Symbol'] = df1.Symbol.fillna(df1.ID.map(df2.set_index('ID').Symbol)) 

Output:输出:

  Name         ID   Symbol
0  AAA   23135106  String1
1  Bbb  G06242104  String2
2  Ccc  30303M102  String3
3  DDD   2079K305  String4
4  EEE   2079K107  String5

I think you only need DataFrame.merge + DataFrame.fillna :我认为你只需要DataFrame.merge + DataFrame.fillna

df1[['Name','ID']].merge(df2[['ID','Symbol']],on='ID',how = 'left').fillna(df1)

  Name         ID   Symbol
0  AAA   23135106  String1
1  Bbb  G06242104  String2
2  Ccc  30303M102  String3
3  DDD   2079K305  String4
4  EEE   2079K107  String5

or或者

( df1[['ID']].merge(df2[['Name','ID','Symbol']],on='ID',how = 'left')
             .fillna(df1)
             .reindex(columns = df1.columns) )

  Name         ID   Symbol
0  Aaa   23135106  String1       
1  Bbb  G06242104  String2
2  Ccc  30303M102  String3
3  Ddd   2079K305  String4       
4  Eee   2079K107  String5

If you need update both Name and Symbol , you need update and slicing assignment如果您需要同时更新NameSymbol ,则需要update和切片分配

df1_1 = df1.set_index('ID')
df1_1.update(df2.set_index('ID'))
df1.loc[df1.Symbol == '', ['Name', 'Symbol']] = df1_1.reset_index()

Out[1238]:
  Name         ID   Symbol
0  Aaa   23135106  String1
1  Bbb  G06242104  String2
2  Ccc  30303M102  String3
3  Ddd   2079K305  String4
4  Eee   2079K107  String5

暂无
暂无

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

相关问题 如果索引值相同,如何将一个DataFrame列复制到另一个Dataframe中 - How to copy one DataFrame column in to another Dataframe if their indexes values are the same 根据另一个数据框的索引保留一个数据框的索引 - Retain indexes from a dataframe based on indexes of another dataframe 用特定索引替换另一个列表中的 dataframe 值 - replace dataframe values from another list with specific indexes 一种快速方法,可从一个数据框的另一个数据框中查找元素并返回其索引 - Fast approach to find elements from one Dataframe in another and return their indexes 根据另一个数据帧的索引和列,用 NaN 替换数据帧单元格 - Replace dataframe cells with NaN based on indexes and columns of another dataframe 如何在索引不匹配时将 dataframe 中的 2 列添加到另一列 - how to Add 2 columns from a dataframe to another while indexes do Not match 如何使用另一个 DataFrame 的值作为索引和列引用(并替换其他)在 DataFrame 中保留一个值? - How keep a value in a DataFrame using the values of another DataFrame as indexes and columns reference (and replace the others)? 使用Pandas从另一个数据框中包含的值中检测数据框中的索引 - Detect indexes of a dataframe from values contained in another dataframe with Pandas 如何从Pandas DataFrame检索所有索引 - How to retrieve all indexes from pandas DataFrame Python:如何从 pandas 到 select 索引 dataframe? - Python: how to select indexes from pandas dataframe?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM