Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 根据另一个数据帧替换数据帧中的值_Python 3.x_Pandas - Fatal编程技术网

Python 3.x 根据另一个数据帧替换数据帧中的值

Python 3.x 根据另一个数据帧替换数据帧中的值,python-3.x,pandas,Python 3.x,Pandas,我有两个数据帧: >>> tab1 Value Sp_name 0 None ROOT 1 0.066 Genus_1_sp1 2 0.1044 Genus_2_sp1 3 0.0708 EDGE 4 0.0586 Genus_3_sp1 5 0.0083 Genus_4_sp1 其思想是解

我有两个数据帧:

   >>> tab1
        Value       Sp_name    
    0   None        ROOT  
    1   0.066       Genus_1_sp1
    2   0.1044      Genus_2_sp1
    3   0.0708      EDGE  
    4   0.0586      Genus_3_sp1
    5   0.0083      Genus_4_sp1
其思想是解析另一个数据帧,例如:

>>> tab2
Old_name             New_name
Genus_1_sp1_A     Genus_1_sp1
Genus_2_sp1_A     Genus_2_sp1
Genus_3_sp1_A     Genus_3_sp1
Genus_4_sp1_A     Genus_4_sp1
并在选项卡1中替换与
tab2
中的
Sp\u名称匹配的
Sp\u名称
,然后将
Sp\u名称
替换为相应的
旧名称
在示例中,我应该得到:

>>> tab1
    Value       Sp_name    
0   None        ROOT  
1   0.066       Genus_1_sp1_A
2   0.1044      Genus_2_sp1_A
3   0.0708      EDGE  
4   0.0586      Genus_3_sp1_A
5   0.0083      Genus_4_sp1_A
到目前为止,我试过:

for i in tab1['Sp_name']:
    found= tab2[tab2['New_name'].str.contains(i)]
    if len(found) > 0:
        tab1.at[i,'Sp_name'] = str(row['Old_name'])

tab2
创建名称词典,然后使用
.replace
将其替换回
tab1

name_dict = dict(zip(tab2.New_name, tab2.Old_name))
tab1['Sp_name'] = tab1['Sp_name'].replace(name_dict)

tab1

    Value        Sp_name
0    None           ROOT
1   0.066  Genus_1_sp1_A
2  0.1044  Genus_2_sp1_A
3  0.0708           EDGE
4  0.0586  Genus_3_sp1_A
5  0.0083  Genus_4_sp1_A

tab2
创建名称词典,然后使用
.replace
将其替换回
tab1

name_dict = dict(zip(tab2.New_name, tab2.Old_name))
tab1['Sp_name'] = tab1['Sp_name'].replace(name_dict)

tab1

    Value        Sp_name
0    None           ROOT
1   0.066  Genus_1_sp1_A
2  0.1044  Genus_2_sp1_A
3  0.0708           EDGE
4  0.0586  Genus_3_sp1_A
5  0.0083  Genus_4_sp1_A
使用

输出

    Value      Sp_name
0    None         ROOT
1   0.066  Genus_1_sp1
2  0.1044  Genus_2_sp1
3  0.0708         EDGE
4  0.0586  Genus_3_sp1
5  0.0083  Genus_4_sp1
使用

输出

    Value      Sp_name
0    None         ROOT
1   0.066  Genus_1_sp1
2  0.1044  Genus_2_sp1
3  0.0708         EDGE
4  0.0586  Genus_3_sp1
5  0.0083  Genus_4_sp1
试试看。它是为这种要求而设计的。使用其他数据帧中的非NA值就地修改

以下是我的示例代码供您参考:

from io import StringIO
import pandas as pd
from pprint import pprint

tab1="""
Value,Sp_name
None,ROOT
0.066,Genus_1_sp1
0.1044,Genus_2_sp1
0.0708,EDGE
0.0586,Genus_3_sp1
0.0083,Genus_4_sp1
"""
tab2="""
Old_name,New_name
Genus_1_sp1_A,Genus_1_sp1
Genus_2_sp1_A,Genus_2_sp1
Genus_3_sp1_A,Genus_3_sp1
Genus_4_sp1_A,Genus_4_sp1A
"""
df1 = pd.read_csv(StringIO(tab1)).set_index("Sp_name",drop=False)

df2=  pd.read_csv(StringIO(tab2)).rename(columns={"Old_name":"Sp_name"}).set_index("New_name")
df1.index.name ='New_name'

new_df = df1.copy()
new_df.update(df2)

print("\nthis is table 1 ")
pprint(df1,)

print("\nthis is table 2 ")

pprint(df2,)

print("\nthis is updated table")

pprint(new_df.reset_index(drop=True),)
这是输出

this is table 1 
              Value      Sp_name
New_name                        
ROOT           None         ROOT
Genus_1_sp1   0.066  Genus_1_sp1
Genus_2_sp1  0.1044  Genus_2_sp1
EDGE         0.0708         EDGE
Genus_3_sp1  0.0586  Genus_3_sp1
Genus_4_sp1  0.0083  Genus_4_sp1

this is table 2 
                    Sp_name
New_name                   
Genus_1_sp1   Genus_1_sp1_A
Genus_2_sp1   Genus_2_sp1_A
Genus_3_sp1   Genus_3_sp1_A
Genus_4_sp1A  Genus_4_sp1_A

this is updated table
    Value        Sp_name
0    None           ROOT
1   0.066  Genus_1_sp1_A
2  0.1044  Genus_2_sp1_A
3  0.0708           EDGE
4  0.0586  Genus_3_sp1_A
5  0.0083    Genus_4_sp1
试试看。它是为这种要求而设计的。使用其他数据帧中的非NA值就地修改

以下是我的示例代码供您参考:

from io import StringIO
import pandas as pd
from pprint import pprint

tab1="""
Value,Sp_name
None,ROOT
0.066,Genus_1_sp1
0.1044,Genus_2_sp1
0.0708,EDGE
0.0586,Genus_3_sp1
0.0083,Genus_4_sp1
"""
tab2="""
Old_name,New_name
Genus_1_sp1_A,Genus_1_sp1
Genus_2_sp1_A,Genus_2_sp1
Genus_3_sp1_A,Genus_3_sp1
Genus_4_sp1_A,Genus_4_sp1A
"""
df1 = pd.read_csv(StringIO(tab1)).set_index("Sp_name",drop=False)

df2=  pd.read_csv(StringIO(tab2)).rename(columns={"Old_name":"Sp_name"}).set_index("New_name")
df1.index.name ='New_name'

new_df = df1.copy()
new_df.update(df2)

print("\nthis is table 1 ")
pprint(df1,)

print("\nthis is table 2 ")

pprint(df2,)

print("\nthis is updated table")

pprint(new_df.reset_index(drop=True),)
这是输出

this is table 1 
              Value      Sp_name
New_name                        
ROOT           None         ROOT
Genus_1_sp1   0.066  Genus_1_sp1
Genus_2_sp1  0.1044  Genus_2_sp1
EDGE         0.0708         EDGE
Genus_3_sp1  0.0586  Genus_3_sp1
Genus_4_sp1  0.0083  Genus_4_sp1

this is table 2 
                    Sp_name
New_name                   
Genus_1_sp1   Genus_1_sp1_A
Genus_2_sp1   Genus_2_sp1_A
Genus_3_sp1   Genus_3_sp1_A
Genus_4_sp1A  Genus_4_sp1_A

this is updated table
    Value        Sp_name
0    None           ROOT
1   0.066  Genus_1_sp1_A
2  0.1044  Genus_2_sp1_A
3  0.0708           EDGE
4  0.0586  Genus_3_sp1_A
5  0.0083    Genus_4_sp1
您可以使用来进行映射。它也是最矢量化的(IMO):


您可以使用来进行映射。它也是最矢量化的(IMO):



谢谢你的解释!欢迎。祝你今天愉快。谢谢你的解释!欢迎。祝您有个美好的一天。