> For the complete documentation index, see [llms.txt](https://igb.mit.edu/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://igb.mit.edu/mini-courses/python/data-processing-with-python/pandas/editing-dataframes.md).

# Editing DataFrames

You can trivially add new columns or change the values in existing column.

### Add a new column

1. Be sure that the column you are adding has the same indices as the old dataframe.
2. This is most easily accomplished by manipulating an old column and saving the value
3. Example: `dataframe['new_column_name'] = ms['old_column']*value`
4. Example: `dataframe['new_column_name'] = ms['old_column1']*ms['old_column2']`

### Alter a column

1. You select a set of cells using the tools from above change their values
2. Note that dataframes are MUTABLE!
3. `dataframe.loc[selection,selection] = 7`

### Dealing with missing data

1. The `dataframe.dropna()` and `.fillna()` funtions are super helpful in removing/replacing missing values
2. Example: `only_complete_rows = dataframe.dropna(how='any')`
3. Example: `replace_with_0 = dataframe.fillna(value=0.0)`

```
# what is this line doing?
ms['light +1 charge mass']=ms['light Precursor Mz']*ms['Precursor Charge'] - ((ms['Precursor Charge']-1)*1.0078)
```

```
ms[['Peptide Modified Sequence', 'light Precursor Mz', 'Precursor Charge', 'light +1 charge mass']]
```

<figure><img src="/files/fCc1Qf7fShEwMhhB89sE" alt=""><figcaption></figcaption></figure>

```
#think through what this line is doing
ms.loc[ms['light +1 charge mass']>2000,['Peptide Modified Sequence', 'light Precursor Mz', 'Precursor Charge', 'light +1 charge mass']]
```

<figure><img src="/files/ySf9pthkDeAX2RqFsGwA" alt=""><figcaption></figcaption></figure>

```
ms.loc[ms['light +1 charge mass']>2000,'light +1 charge mass'] = 'way too big!'
```

<figure><img src="/files/MPDx67gcVL9zJtjSFZk6" alt=""><figcaption></figcaption></figure>

```
#you can quickly save your work at a .csv using the command .to_csv(path_to_file)
ms.to_csv("C:\\Users\duan\Desktop\PythonDataProcessingVisualization\mass_spec_new.csv")
#look in your directory for a new .csv file!
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://igb.mit.edu/mini-courses/python/data-processing-with-python/pandas/editing-dataframes.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
