I have a dataframe called 'test_df', one of its columns is named COUNTRY. I am required to change the value BRAZIL to BRASIL in the 'COUNTRY' column of the test_df dataframe. I tried the mask() method for changing all BRAZIL to BRASIL values in the COUNTRY column but it seems to create a separate dataframe. I am pasting the snippet below :
test_df = cursor.fetch_pandas_all()
test_df = test_df['COUNTRY'].mask(test_df['COUNTRY'] == 'BRAZIL', 'BRASIL')
print(test_df)
I get the following output :
0 INDIA
1 AUSTRALIA
2 MALAYSIA
3 PERU
4 BRASIL
...
How do I print the entire original dataframe (test_df) with the COUNTRY column changes ? I tried removing ['COUNTRY'] before calling the .mask() method, but that does not work. It is changing the values of all Columns to BRASIL which is not what is required. The BRASIL values should show up only in the COUNTRY column and not in any other column.
CodePudding user response:
Try:
test_df['COUNTRY'] = test_df['COUNTRY'].str.replace('BRAZIL', 'BRASIL')
CodePudding user response:
import pandas as pd
data = [['John', 'USA'], ['Dawn', "CANADA"], ['Julia', "BRAZIL"]]
df = pd.DataFrame(data, columns = ['Name', 'Country'])
print(df)
df.loc[df['Country'] == "BRAZIL", 'Country'] = "BRASIL"