Reputation:
I am trying to import
xy coordinates
from multiple columns
into a list. I can get it going when the coordinates
are read from a single column
but struggling to get it to read from multiple columns
efficiently.
I need this for plotting
Attempt:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
d = ({
'Time' : [1,2,3,4,5,6,7,8],
'GrA1_X' : [10,12,17,16,16,14,12,8],
'GrA1_Y' : [10,12,13,7,6,7,8,8],
'GrA2_X' : [5,8,13,16,19,15,13,5],
'GrA2_Y' : [6,15,12,7,8,9,10,8],
'GrB1_X' : [15,18,25,16,6,15,17,10],
'GrB1_Y' : [7,12,5,9,10,12,18,9],
'GrB2_X' : [10,4,18,14,16,12,17,4],
'GrB2_Y' : [8,12,16,8,10,14,12,15],
})
df = pd.DataFrame(data=d)
GrA_X = df[df.columns[1::2][:2]]
GrA_Y = df[df.columns[2::2][:2]]
GrB_X = df[df.columns[5::2][:2]]
GrB_Y = df[df.columns[6::2][:2]]
fig = plt.figure(figsize=(10,6))
ax = plt.gca()
Zs = []
for l,color in zip('AB', ('red', 'blue')):
# plot all of the points from a single group
ax.plot(GrA_X.iloc[0], GrA_Y.iloc[0], '.', c='red', ms=15, label=l, alpha = 0.5)
ax.plot(GrB_X.iloc[0], GrB_Y.iloc[0], '.', c='blue', ms=15, label=l, alpha = 0.5)
Zrows = []
for _,row in df.iterrows():
x,y = row['Gr%s_X'%l], row['Gr%s_Y'%l]
I'm stuck with the Zrows = []
call. Specifically, how to append multiple
columns
within this list.
Upvotes: 1
Views: 2808
Reputation: 446
This could be an alternative solution, given I am understanding your problem correctly.
df = pd.DataFrame(data=d)
X = [df[c].tolist() for c in df.columns if c.find("_X") >= 0]
Y = [df[c].tolist() for c in df.columns if c.find("_Y") >= 0]
allX = [x for sublist in X for x in sublist]
allY = [y for sublist in Y for y in sublist]
dfXY = pd.DataFrame({"X": allX, "Y":allY})
Now you have all x's and y's in a simple dataframe. Cheers
Upvotes: 1
Reputation: 9810
I'm still not clear whether I understand the purpose of the last loop correctly, but I'll take the risk and suggest a solution. Your problem seems to be with the naming of the columns and I don't see another way except looping over the different variations of Gr{A,B}{1,2}_{X,Y}
(here the {}
indicate variations). I would go with a nested for
-loop, i.e.:
Zs = []
for l,color in zip('AB', ('red', 'blue')):
# plot all of the points from a single group
ax.plot(GrA_X.iloc[0], GrA_Y.iloc[0], '.', c='red', ms=15, label=l, alpha = 0.5)
ax.plot(GrB_X.iloc[0], GrB_Y.iloc[0], '.', c='blue', ms=15, label=l, alpha = 0.5)
Zrows = []
for _,row in df.iterrows():
for i in [1,2]:
x,y = row['Gr{}{}_X'.format(l,i)], row['Gr{}{}_Y'.format(l,i)]
Zrows.append((x,y))
print(Zrows)
which gives a list of coordinate tuples:
[(15, 7), (10, 8), (18, 12), (4, 12), (25, 5), (18, 16), (16, 9), (14, 8), (6, 10), (16, 10), (15, 12), (12, 14), (17, 18), (17, 12), (10, 9), (4, 15)]
Note that I changed the string formatting to the format()
-syntax, which in my opinion vastly increases readability. Please let me know if this is even close to what you are after.
Upvotes: 0