Debraj Das
Debraj Das

Reputation: 99

how to read a json in pandas dataframe and change one column value to uppercase and save the json file

I'm trying to read a json file into pandas dataframe. Then, I'm trying to convert one entire column values into uppercase. The code is below,

data = pd.read_json('demo1.json') 
print(data)
print(data[column].str.upper())
print(data)

The output is below,

          CITY FIRST_NAME  ID LAST_NAME          SSN
0      Waymill  Albertine   1       Jan  515-72-7353
1  Spellbridge   Maryetta   2      Hoyt  515-72-7354
2    Stoneland     Dustin   3    Divina  515-72-7355
3      Fayview      Jenna   4     Sofia  515-72-7356


0        WAYMILL
1    SPELLBRIDGE
2      STONELAND
3        FAYVIEW


Name: CITY, dtype: object
          CITY FIRST_NAME  ID LAST_NAME          SSN
0      Waymill  Albertine   1       Jan  515-72-7353
1  Spellbridge   Maryetta   2      Hoyt  515-72-7354
2    Stoneland     Dustin   3    Divina  515-72-7355
3      Fayview      Jenna   4     Sofia  515-72-7356

So, I'm able to print the values as uppercase, but, the whole dataframe isn't changing.

Need some guideline.

Upvotes: 1

Views: 1930

Answers (2)

jezrael
jezrael

Reputation: 863301

Assign back output of str.upper function to column with same name for overwrite values or to new name for append new column filled by uppercase values:

data['CITY'] = data['CITY'].str.upper()
print (data)
          CITY FIRST_NAME  ID LAST_NAME          SSN
0      WAYMILL  Albertine   1       Jan  515-72-7353
1  SPELLBRIDGE   Maryetta   2      Hoyt  515-72-7354
2    STONELAND     Dustin   3    Divina  515-72-7355
3      FAYVIEW      Jenna   4     Sofia  515-72-7356

data['NEW_CITY'] = data['CITY'].str.upper()
print (data)
          CITY FIRST_NAME  ID LAST_NAME          SSN     NEW_CITY
0      Waymill  Albertine   1       Jan  515-72-7353      WAYMILL
1  Spellbridge   Maryetta   2      Hoyt  515-72-7354  SPELLBRIDGE
2    Stoneland     Dustin   3    Divina  515-72-7355    STONELAND
3      Fayview      Jenna   4     Sofia  515-72-7356      FAYVIEW

EDIT:

If need CITY column to end of DataFrames columns use DataFrame.pop for extract and then assign to same column name:

df['CITY'] = df.pop('CITY').str.upper()
print (df)
  FIRST_NAME  ID LAST_NAME          SSN         CITY
0  Albertine   1       Jan  515-72-7353      WAYMILL
1   Maryetta   2      Hoyt  515-72-7354  SPELLBRIDGE
2     Dustin   3    Divina  515-72-7355    STONELAND
3      Jenna   4     Sofia  515-72-7356      FAYVIEW

Upvotes: 2

Karn Kumar
Karn Kumar

Reputation: 8826

In case you need to create a new column as you mentioned along with converting to uppercase then try below.

DataFrame.assign() + str.upper()

df = df.assign(NEW_CITY=data['CITY'].str.upper())

In case you want to have only the NEW_CITY and remove the old one ie CITY then you can try..

df = df.assign(NEW_CITY=data['CITY'].str.upper()).drop(columns=['CITY'])

To retain the column name back using df.rename()

df.rename(columns={'NEW_CITY':'CITY'}, inplace=True)

Upvotes: 0

Related Questions