Home > Software design >  How to append value to a specified column in csv with pandas python?
How to append value to a specified column in csv with pandas python?

Time:07-18

enter image description here

To replace the NaN values in the world column:

df['world'] = df['world'].replace(np.nan, 'some country')

To replace the NaN values in the whole dataframe:

df = df.replace(np.nan, 'some value')

To replace inplace i.e., no need to reassign:

df.replace(np.nan, 'some value', inplace=True)

To append some other values in dataframe:

df2 = pd.DataFrame([[True, 'bob', 'canada', 'hi'], [False, 'marley', 'jamaica', np.nan]], 
                    columns=['status', 'user', 'world', 'cat'])
df = df.append(df2)

After all these operations, the dataframe may look like this:

enter image description here

  • Related