Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
178 views
in Technique[技术] by (71.8m points)

python - 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])})])

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...