不好意思打擾各位大大,我是近幾個月剛學習python的新手
目前我有一筆csv檔,匯入後想將計算後的csv檔轉成DataFrame的格式方便後續使用plotly去可視化,但我將他轉DataFrame的時候,我想因為是在迴圈的狀態下,導致匯出的不是同一個DataFreme(如圖一),我想請問怎麼修改將它變成同一個DataFrame且如圖二的樣子?
謝謝
===============以下程式碼================
import pandas as pd
import numpy as np
filename = 'C:/Users/smpss/OneDrive/桌面/many data.csv'
df = pd.read_csv(filename, header=[0,1,2,3], index_col=[0])
df.columns=df.columns.droplevel(0)
df.columns=df.columns.droplevel(1)
df
import math
import plotly.express as px
import plotly.figure_factory as ff
df.columns=df.columns.droplevel(0)
#print("C","C","C","C","C")
for n in range(19400,19415):
a = df["x"]
df = df.fillna(0)
b = np.array(a)
c = df["y"]
df = df.fillna(0)
d = np.array(c)
e = df["x"]
df = df.fillna(0)
f = np.array(e)
g = df["y"]
df = df.fillna(0)
h = np.array(g)
p=0
q=0
AB = [float(b[n:n+1,p:p+1]),float(d[n:n+1,p:p+1]),float(b[n+1:n+2,p:p+1]),float(d[n+1:n+2,p:p+1])]
AC = [float(b[n:n+1,p:p+1]),float(d[n:n+1,p:p+1]),float(f[n:n+1,q+7:q+8]),float(h[n:n+1,q+7:q+8])]
dx1 = float(b[n+1:n+2,p+3:p+4]) - float(b[n:n+1,p+3:p+4])
dy1 = float(d[n+1:n+2,p+3:p+4]) - float(d[n:n+1,p+3:p+4])
dx2 = float(f[n:n+1,q+10:q+11]) - float(b[n:n+1,p+3:p+4])
dy2 = float(h[n:n+1,q+10:q+11]) - float(d[n:n+1,p+3:p+4])
dist = np.sqrt((dx1)**2+(dy1)**2)
fps = 30 #(pxl)
Velocity = dist*fps
#box = 40cm,camera = 640pxl.
#so 40/640 = 1cm/16pxl and we need 1.2 cm
#so we can get 1.2*16=19.2
#if Ans >= 19.2 ~> 20
def angle(AB, AC):
dx1 = float(b[n+1:n+2,p:p+1]) - float(b[n:n+1,p:p+1])
dy1 = float(d[n+1:n+2,p:p+1]) - float(d[n:n+1,p:p+1])
dx2 = float(f[n:n+1,q+7:q+8]) - float(b[n:n+1,p:p+1])
dy2 = float(h[n:n+1,q+7:q+8]) - float(d[n:n+1,p:p+1])
"""
print(dx1)
print(dy1)
print(dx2)
print(dy2)
"""
angle1 = math.atan2((float(b[n+1:n+2,p:p+1]) - float(b[n:n+1,p:p+1])),(float(d[n+1:n+2,p:p+1]) - float(d[n:n+1,p:p+1])))
angle1 = float(angle1 * 180/math.pi)
angle2 = math.atan2((float(f[n:n+1,q+7:q+8]) - float(b[n:n+1,p:p+1])),(float(h[n:n+1,q+7:q+8]) - float(d[n:n+1,p:p+1])))
angle2 = float(angle2 * 180/math.pi)
if angle1*angle2 >= 0:
included_angle = abs(angle1-angle2)
else:
included_angle = abs(angle1) + abs(angle2)
if included_angle > 180:
included_angle = 360 - included_angle
return included_angle
n==n+1
ang1 = angle(AB, AC)
if float(ang1) <= 45 and Velocity>=20:
AAA = "A"
BBB = "B"
CCC = round(((n-1)*0.0001),4)
DDD = round((n*0.0001),4)
EEE = "C"
KKK = [AAA,BBB,CCC,DDD,EEE]
df2=pd.DataFrame({'':[KKK]},index=list('1'))
print(df2)
else:
continue