在 Pandas DataFrame 中替换字符中的字符
在本文中,我们将了解如何使用Python替换 pandas 数据框中字符串中的字符。
我们可以使用 str.replace() 方法替换字符基本上是用新的替换现有字符串或字符中的字符串。我们可以为整个数据框以及特定列替换字符中的字符串。
Syntax: str.replace (old_string, new_string, n=-1, case=None, regex=True)
Parameters:
- old_string: string to be replaced.
- new_string: string or callable to replace instead of pat.
- n: Number of replacement to make in a single string, default is -1 which means All.
- case: Takes boolean value to decide case sensitivity. Make false for case insensitivity.
- regex: Boolean value, if True assume that the passed pattern is a regex.
Return Type: return a copy of the object with all matching occurrences of old_string replaced by new_string.
示例 1:下面的程序是为整个数据帧替换字符串中的一个字符。
Python3
# import pandas
import pandas as pd
data = {'Student_Full_Name': ['Mukul_Jatav', 'Rahul_Shukla',
'Robin_Singh', 'Mayank_Sharma',
'Akash_Verma'],
'Father_Full_name': ['Mukesh_Jatav', 'Siddhart_Shukla',
'Rohit_Singh', 'Sunil_Sharma',
'Rajesh_Verma']
}
# create an dataframe
df = pd.DataFrame(data, columns=['Student_Full_Name',
'Father_Full_name'])
# print dataframe
print(" original dataframe \n", df)
# replace '_' with '-'
df = df.replace('_', '+', regex=True)
# print dataframe
print(" After replace character \n", df)
Python3
# import pandas
import pandas as pd
data = {'first': ['abcp', 'xyzp', 'mpok',
'qrps', 'ptuw'],
'second': ['abcp', 'xyzp', 'mpok',
'qrps', 'ptuw']
}
# create an dataframe
df = pd.DataFrame(data, columns=['first', 'second'])
# print dataframe
print("\n original dataframe \n\n", df)
# replace '_' with '='
df['first'] = df['first'].str.replace('p', '-')
# print dataframe
print("\n\n After replace character \n\n", df)
输出:
示例 2:下面的程序是为特定列替换字符串中的一个字符。
Python3
# import pandas
import pandas as pd
data = {'first': ['abcp', 'xyzp', 'mpok',
'qrps', 'ptuw'],
'second': ['abcp', 'xyzp', 'mpok',
'qrps', 'ptuw']
}
# create an dataframe
df = pd.DataFrame(data, columns=['first', 'second'])
# print dataframe
print("\n original dataframe \n\n", df)
# replace '_' with '='
df['first'] = df['first'].str.replace('p', '-')
# print dataframe
print("\n\n After replace character \n\n", df)
输出: