add variables to cell pandas in python

To add variables to a cell in Pandas DataFrame, you can use the .at[] or .loc[] accessor methods.

Here's an example code block:

main.py
import pandas as pd

# create a sample dataframe
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]})

# assign a variable to a cell using .at[]
df.at[1, 'B'] = 10

# assign a variable to a cell using .loc[]
df.loc[2, 'C'] = 11

# print the updated dataframe
print(df)
284 chars
14 lines

In the code block above, we first create a sample dataframe df. We then assign a value of 10 to the cell in row 1 and column B using the .at[] method. Similarly, we assign a value of 11 to the cell in row 2 and column C using the .loc[] method.

Finally, we print the updated dataframe to verify that the variables were successfully added to the respective cells.

gistlibby LogSnag