Skip to content
Advertisement

How to add row to dataframe that keeps tuple as a tuple rather than splitting out into two elements?

I have a dataframe that I’d like to iteratively add rows to. The columns are an integer for ‘time’, an (x,y) coordinates and a dichotomous status. For the sake of this example, rather than the full iteration, I will just demonstrate the issue with adding one row to the dataframe, rather than lots. The issue appears to be that when I concatenate the new row to the existing dataframe, it breaks apart my tuple (5,0) in to two integer values, and so there are two rows attempting to be added rather than just the one, and so I get the error: “ValueError: arrays must all be same length”. How can I fix this?

import pandas as pd
import numpy as np
# set up coordinates for a 10x10 grid
m=10
n=10
coordinates = []
for x in range(m):
    for y in range(n):
        coordinates.append((x,y))

# set the state of each point to "ON" or "OFF"
states = np.random.choice(["ON","OFF"],size=len(coordinates), replace=True, p=[0.2, 0.8])

# collate the above into a dataframe
df = pd.DataFrame({"time": np.zeros(len(coordinates)), "individual": coordinates, "state": states})

# pick a random point
point = coordinates[50]

# add row for time+1 to the data frame. 
df = pd.concat([df, pd.DataFrame({"time": 1, "individual": point, "state": np.random.choice(["ON","OFF"],1, replace=True, p=[0.2, 0.8])})])

Advertisement

Answer

You just need to add brackets around coordinates[50] to avoid splitting your tuple:

point = [coordinates[50]]

(End of) output:

95   0.0     (9, 5)    ON
96   0.0     (9, 6)   OFF
97   0.0     (9, 7)    ON
98   0.0     (9, 8)   OFF
99   0.0     (9, 9)    ON
0    1.0     (5, 0)   OFF
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement