用 Pandas 中另一个 DataFrame 的值替换 DataFrame 的值
在本文中,我们将学习如何使用 pandas 将一个 DataFrame 的值替换为另一个 DataFrame 的值。
可以使用DataFrame.replace()方法来完成。它用于替换 DataFrame 中的正则表达式、字符串、列表、系列、数字、字典等,DataFrame 方法的值被动态替换为另一个值。这是一个非常丰富的函数,因为它有很多方法。本教程解释了如何在实践中使用这些方法的不同示例。
Syntax: DataFrame.replace( old value, new value)
Parameters:
- old value: This is old substring to be replaced.
- new value: This is new substring, which would replace old substring.
例子
在这里,我们将创建一些将在进一步示例中使用的数据。
Python3
import pandas as pd
# initialise data of lists.
colors = {'first_set': ['99', '88', '77', '66',
'55', '44', '33', '22'],
'second_set': ['1', '2', '3', '4', '5',
'6', '7', '8']
}
color = {'first_set': ['a', 'b', 'c', 'd', 'e',
'f', 'g', 'h'],
'second_set': ['VI', 'IN', 'BL', 'GR',
'YE', 'OR', 'RE', 'WI']
}
# Calling DataFrame constructor on list
df = pd.DataFrame(colors, columns=['first_set', 'second_set'])
df1 = pd.DataFrame(color, columns=['first_set', 'second_set'])
# Display the Output
display(df)
display(df1)
Python3
# selecting old value
a = df1['first_set'][4]
# selecting new value
b = df['first_set'][1]
# replace values of one DataFrame
# with the value of another DataFrame
df1 = df1.replace(a,b)
# Display the Output
display(df1)
Python3
# Display the Output
display(df)
display(df1)
# Selecting old value
a = df['first_set'][4]
# Selecting new value
b = df1['first_set'][1]
# replace values of one DataFrame with
# the value of another DataFrame
df = df.replace(a,b)
# Display the Output
display(df)
Python3
#selected value
a = df['first_set'][4]
b = df1['second_set'][1]
# replace values of one DataFrame with
# the value of another DataFrame
df = df.replace(a,'Hello')
# replace values of one DataFrame with
# the value of another DataFrame
df1 = df1.replace(b,'Geeks')
display (df)
display(df1)
Python3
# replace column of one DataFrame with
# the column of another DataFrame
df['second_set'] = df1.replace(df['first_set'],df['second_set'])
display(df)
Python3
# replaceing value of DataFrame
df1.first_set[df1.first_set == '66'] = 'DF1'
df2.first_set[df2.first_set == 'g'] = 'DF2'
# display updated table
display(df1)
display(df2)
Python3
import pandas as pd
# Creating dataframe
df1 = pd.DataFrame([["rumul", 10, 12, 10],
["rahul", 10, 11, 16],
["purvi", 18, 14, 10],
["ravi", 20, 13, 30]],
columns=["Name1", "Maths",
"Physics",
"Chemistry"])
# setting name1 as index column
df1 = df1.set_index('Name1')
display(df1)
# Creating another dataframe for
# replacement
df2 = pd.DataFrame(
[["rahul", 1, 1]],
columns=["Name2", "Maths", "Physics"])
df3 = pd.DataFrame(
[["purvi", 5, 8]],
columns=["Name3", "Maths", "Chemistry"])
# setting name2 and name3 as
# index column
df2 = df2.set_index('Name2')
df3 = df3.set_index('Name3')
# update the values at the
# passed index
df1.update(df2)
df1.update(df3)
display(df1)
Python3
import pandas as pd
# Creating dataframe
df1 = pd.DataFrame({'Name': ['rumul', 'rahul',
'ravi', 'imran'],
'Marks': [5, 20, 8, 12]})
df2 = pd.DataFrame({'Name': ['rumul', 'rahul',
'purvi', 'ravi',
'imran'],
'Marks': [18, 19, 13, 11, 15]})
display(df1)
display(df2)
# replace values of one DataFrame with
# the value of another DataFrame
df1['Marks'] = df2[df2['Name'].isin(df1['Name'])]['Marks'].values
display(df1)
输出:
示例 1:将“e”值替换为 DataFrame 的 first_set 的“88”值。
蟒蛇3
# selecting old value
a = df1['first_set'][4]
# selecting new value
b = df['first_set'][1]
# replace values of one DataFrame
# with the value of another DataFrame
df1 = df1.replace(a,b)
# Display the Output
display(df1)
输出:
示例 2:将“55”值替换为 DataFrame 的 first_set 的“b”值。
蟒蛇3
# Display the Output
display(df)
display(df1)
# Selecting old value
a = df['first_set'][4]
# Selecting new value
b = df1['first_set'][1]
# replace values of one DataFrame with
# the value of another DataFrame
df = df.replace(a,b)
# Display the Output
display(df)
输出:
示例 3:现在让我们将 DataFrame 的“first_set”列下的“55”值替换为“Hello”值,将“VI”值替换为“第二个集”列下的“Geeks”值。
蟒蛇3
#selected value
a = df['first_set'][4]
b = df1['second_set'][1]
# replace values of one DataFrame with
# the value of another DataFrame
df = df.replace(a,'Hello')
# replace values of one DataFrame with
# the value of another DataFrame
df1 = df1.replace(b,'Geeks')
display (df)
display(df1)
输出:
示例 4:现在让我们将一个 DataFrame 的整个列替换为另一个 DataFrame 的列。
蟒蛇3
# replace column of one DataFrame with
# the column of another DataFrame
df['second_set'] = df1.replace(df['first_set'],df['second_set'])
display(df)
输出:
示例 5:这里, df1.first_set[df1.first_set == '66'] = 'DF1'意味着首先我们在 df1 的 first_set 中找到 '66',然后我们将该值 ('66') 替换为 ' DF1' 表示相同的位置。
蟒蛇3
# replaceing value of DataFrame
df1.first_set[df1.first_set == '66'] = 'DF1'
df2.first_set[df2.first_set == 'g'] = 'DF2'
# display updated table
display(df1)
display(df2)
输出:
示例 6:在此示例中,我们创建了另一个数据框 'df2' 和 'df3' 来替换 'df1' 的值。这里“df2”“df3”的值将通过更新方法,然后它会找到“df1”中存在的匹配实体,找到后将用“df2”“df3”值更新值。 Pandas df.update()函数更新在给定 Series 对象中标识的传递索引值。
Syntax: df.update(other)
Parameter :
- other: series
蟒蛇3
import pandas as pd
# Creating dataframe
df1 = pd.DataFrame([["rumul", 10, 12, 10],
["rahul", 10, 11, 16],
["purvi", 18, 14, 10],
["ravi", 20, 13, 30]],
columns=["Name1", "Maths",
"Physics",
"Chemistry"])
# setting name1 as index column
df1 = df1.set_index('Name1')
display(df1)
# Creating another dataframe for
# replacement
df2 = pd.DataFrame(
[["rahul", 1, 1]],
columns=["Name2", "Maths", "Physics"])
df3 = pd.DataFrame(
[["purvi", 5, 8]],
columns=["Name3", "Maths", "Chemistry"])
# setting name2 and name3 as
# index column
df2 = df2.set_index('Name2')
df3 = df3.set_index('Name3')
# update the values at the
# passed index
df1.update(df2)
df1.update(df3)
display(df1)
输出:
示例 7:使用isin方法过滤 df 并分配所需的行值。这里我们选择了通用的'Name'来过滤掉DataFrame(df1)和DataFrame(df2)中的数据,之后我们将其替换为'df2'的值。例如,rumul'marks 替换为 5 到 18 个标记,rahul'marks 替换为 20 到 19 个标记等。 Pandas isin() 方法用于过滤数据帧。此方法有助于选择在特定列中具有特定值的行。
Syntax: DataFrame.isin(values)
Parameters:
- values: iterable, Series, List, Tuple, DataFrame or dictionary.
蟒蛇3
import pandas as pd
# Creating dataframe
df1 = pd.DataFrame({'Name': ['rumul', 'rahul',
'ravi', 'imran'],
'Marks': [5, 20, 8, 12]})
df2 = pd.DataFrame({'Name': ['rumul', 'rahul',
'purvi', 'ravi',
'imran'],
'Marks': [18, 19, 13, 11, 15]})
display(df1)
display(df2)
# replace values of one DataFrame with
# the value of another DataFrame
df1['Marks'] = df2[df2['Name'].isin(df1['Name'])]['Marks'].values
display(df1)
输出: