如何快速高效地将excel信息存储到python中

2024-06-28 05:20:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个excel文件,它有5000多行32列。数据代表16项测试。它是这样存储的:第一列是属于存储在第二列中的y值的x值。第三列是属于存储在第四列中的y值的x值,依此类推。。。由于这些值可以细分为4个测试(8列中的4个测试是32列),因此我希望将它们存储如下:

  • 第1列=xtest11
  • 第2列=Y测试11
  • 第3列=xtest12
  • 第4列=Y测试12
  • 第5列=xtest13
  • 第6列=Y测试13
  • 第7列=xtest14
  • 第8列=Y测试14
  • 第9列=xtest21
  • 第10列=Y测试22
  • 第11列=xtest23
  • 第12列=Y测试24
  • 第31列=xtest44
  • 第32列=ytest44

现在我已经用很多行代码把这些都写出来了。我想知道是否可以使用for循环或其他方法使代码看起来更好。我是python(spyder)的新手,我想学习这些东西,以便在学习过程中更轻松。多谢各位

F0=-33.5
df = pd.read_excel (r'D:\GEO_ENGINEERING\Thesis\Resultaten instrong\datazuiver.xlsx') #place "r" before the path string to address special character, such as '\'. Don't forget to put the file name at the end of the path + '.xlsx'
    
    

#%% Test 1

T11=df["Time_1_1"]
F11=df["F_1_1"]-F0
T12=df["Time_1_2"]
F12=df["F_1_2"]-F0
T13=df["Time_1_3"]
F13=df["F_1_3"]-F0
T14=df["Time_1_4"]
F14=df["F_1_4"]-F0
T11=T11[1:5000]
T12=T12[1:5000]
T13=T13[1:5000]
T14=T14[1:5000]
F11=F11[1:5000]
F12=F12[1:5000]
F13=F13[1:5000]
F14=F14[1:5000]
plt.figure()
plt.plot(T11,F11, label='Proef 1 test 1')
plt.plot(T12,F12, label='Proef 1 test 2')
plt.plot(T13,F13, label='Proef 1 test 3')
plt.plot(T14,F14, label='Proef 1 test 4')
plt.xlabel('Time [s]')
plt.ylabel('Force [N]')
plt.title('Test 1')
plt.show()

#%%
df = pd.read_excel (r'D:\GEO_ENGINEERING\Thesis\Resultaten instrong\datazuiver.xlsx') #place "r" before the path string to address special character, such as '\'. Don't forget to put the file name at the end of the path + '.xlsx'
T21=df["Time_2_1"]
F21=df["F_2_1"]-F0
T22=df["Time_2_2"]
F22=df["F_2_2"]-F0
T23=df["Time_2_3"]
F23=df["F_2_3"]-F0
T24=df["Time_2_4"]
F24=df["F_2_4"]-F0
T21=T21[1:2366]
T22=T22[1:2366]
T23=T23[1:2366]
T24=T24[1:2366]
F21=F21[1:2366]
F22=F22[1:2366]
F23=F23[1:2366]
F24=F24[1:2366]
plt.figure()
plt.plot(T21,F21, label='Proef 2 test 1')
plt.plot(T22,F22, label='Proef 2 test 2')
plt.plot(T23,F23, label='Proef 2 test 3')
plt.plot(T24,F24, label='Proef 2 test 4')
plt.xlabel('Time [s]')
plt.ylabel('Force [N]')
plt.title('Test 2')
plt.show()
#%% Proef 3


T31=df["Time_3_1"]
F31=df["F_3_1"]-F0
T32=df["Time_3_2"]
F32=df["F_3_2"]-F0
T33=df["Time_3_3"]
F33=df["F_3_3"]-F0
T34=df["Time_3_4"]
F34=df["F_3_4"]-F0
T31=T31[1:2366]
T32=T32[1:2344]
T33=T33[1:2366]
T34=T34[1:2366]
F31=F31[1:2366]
F32=F32[1:2344]
F33=F33[1:2366]
F34=F34[1:2366]
plt.figure()
plt.plot(T31,F31, label='Proef 3 test 1')
plt.plot(T32,F32, label='Proef 3 test 2')
plt.plot(T33,F33, label='Proef 3 test 3')
plt.plot(T34,F34, label='Proef 3 test 4')
plt.xlabel('Time [s]')
plt.ylabel('Force [N]')
plt.title('Test 3 (no drillgrout)')
plt.legend()
plt.show()

#%% Proef 4

T41=df["Time_4_1"]
F41=df["F_4_1"]-F0
T42=df["Time_4_2"]
F42=df["F_4_2"]-F0
T43=df["Time_4_3"]
F43=df["F_4_3"]-F0
T44=df["Time_4_4"]
F44=df["F_4_4"]-F0
T41=T41[1:2366]
T42=T42[1:2366]
T43=T43[1:2366]
T44=T44[1:2366]
F41=F41[1:2366]
F42=F42[1:2366]
F43=F43[1:2366]
F44=F44[1:2366]
plt.figure()
plt.plot(T41,F41, label='Proef 4 test 1')
plt.plot(T42,F42, label='Proef 4 test 2')
plt.plot(T43,F43, label='Proef 4 test 3')
plt.plot(T44,F44, label='Proef 4 test 4')
plt.xlabel('Time [s]')
plt.ylabel('Force [N]')
plt.title('Test 4')
plt.legend()
plt.show()
#%% Plot all the tests
plt.figure()
plt.plot(T11,F11, label='Proef 1 test 1')
plt.plot(T12,F12, label='Proef 1 test 2')
plt.plot(T13,F13, label='Proef 1 test 3')
plt.plot(T14,F14, label='Proef 1 test 4')
plt.plot(T21,F21, label='Proef 2 test 1')
plt.plot(T22,F22, label='Proef 2 test 2')
plt.plot(T23,F23, label='Proef 2 test 3')
plt.plot(T24,F24, label='Proef 2 test 4')
plt.plot(T41,F41, label='Proef 4 test 1')
plt.plot(T42,F42, label='Proef 4 test 2')
plt.plot(T43,F43, label='Proef 4 test 3')
plt.plot(T44,F44, label='Proef 4 test 4')
plt.xlabel('Time [s]')
plt.ylabel('Force [N]')
plt.title('Test 1, 2, 3 and 4')
plt.legend()
plt.show()
#%% Plot of test 2 and 4mat
plt.figure()
plt.plot(T21,F21, label='Proef 2 test 1')
plt.plot(T22,F22, label='Proef 2 test 2')
plt.plot(T23,F23, label='Proef 2 test 3')
plt.plot(T24,F24, label='Proef 2 test 4')
plt.plot(T41,F41, label='Proef 4 test 1')
plt.plot(T42,F42, label='Proef 4 test 2')
plt.plot(T43,F43, label='Proef 4 test 3')
plt.plot(T44,F44, label='Proef 4 test 4')
plt.xlabel('Time [s]')
plt.ylabel('Force [N]')
plt.title('Test 2 and 4')
plt.legend()
plt.show()

Tags: thetestdftimeplottitlepltlabel
1条回答
网友
1楼 · 发布于 2024-06-28 05:20:08

这是假设您只有一个数据帧。看起来像两次导入同一个:一次启动,一次在中间。

如果你想减少行数,你可以编写一个简单的函数,它接受任何“Proef”数字、你想在绘图中显示的标题和数据框,然后进行绘图。比如说,

import pandas as pd
import matplotlib.pyplot as plt

F0=-33.5
DATA_END_VALUES = [[5000]*4] + [[2366]*4] + [[2366, 2344]*2] + [[2366]*4]
# First index of DATA_END_VALUES is the proef number while the second is 
# the test number

def plot_results(df, proef_numbers, title):
    for proef  in proef_numbers:
        for test in range(1,5):
            time_data = df[f"Time_{proef}_{test}"][1:DATA_END_VALUES[proef-1][test-1]] 
            f_data = df[f"F_{proef}_{test}"][1:DATA_END_VALUES[proef-1][test-1]] - F0 
            plt.plot(time_data, f_data, label=f"Proef {proef} test {test}" )
    plt.xlabel('Time [s]')
    plt.ylabel('Force [N]')
    plt.title(title)
    plt.legend()
    plt.show()

df = pd.read_excel ("data.xlsx")
proef_numbers = [1,2]
title = "Tests 1 and 2"
plot_results(df, proef_numbers ,title)

我制作了一些虚拟数据,它的行为似乎和预期的一样。你可以检查你的真实数据,看看它是否适用。请记住,对于包含更多数据的绘图,图例可能占据绘图的大部分。稍后您需要对其进行调整

您还提到要将列名重命名为itestjk,其中i是x变量还是y变量,j是“proef”号,k是“test”号。如果进行这些更改,只需将time_data行和f_data更改为以下内容:


time_data = df[f"xtest{proef}{test}"][1:DATA_END_VALUES[proef-1][test-1]]
f_data = df[f"ytest{proef}{test}"][1:DATA_END_VALUES[proef-1][test-1]] - F0

相关问题 更多 >