【问题标题】:Is there a way to make separate a column depending on the values of another column in python 3.x?有没有办法根据 python 3.x 中另一列的值来分隔一列?
【发布时间】:2020-03-30 10:27:43
【问题描述】:

我正在用 python 编写代码来分析我的论文数据。从实验中,我得到了大量数据。在脚本的第一部分,它生成如下输出:

GateVolt      Field      HallVolt 
0             1500          76       
0             1490          75
0             1485          74
.               .            .
.               .            .
0.1           1485          72
0.1           1476          70
.               .            .
.               .            .
0.2           1470          67
0.2           1465          62
.               .            .
.               .            .
.               .            .

根据此输出进行进一步分析。 我写的代码:

    #Read the CSV that contains all the data

    field = np.array([])
    hallVolt = np.array([])
    gate_voltage = np.array([])
    channel_voltage = np.array([])
    voltage_Counter = 0

    for lineIndex in range(len(fileLines)):
        currentLine = fileLines[lineIndex]
        dataSegment = currentLine.split()
        field = np.append(Field, float(dataSegment[9])) #This is milli Tesla
        hallVolt = np.append(hallVolt, float(dataSegment[5])) #This is milli Volt
        channel_voltage = np.append(channel_voltage, float(dataSegment[2])) #This is Volt
        VG = float(dataSegment[6]) - float(dataSegment[2])/2
        gate_voltage = np.append(gate_voltage, np.round(VG, 2)) #This is Volt
        if lineIndex > 1 and VG != gate_voltage[lineIndex-1]:
            voltage_Counter = voltage_Counter + 1

    HallVoltage = hallVolt-((max(hallVolt))+min(hallVolt))/2
    HallVoltage_Norm = HV/((max(HV)-min(HV))/2)

所以我希望我的代码为每个“栅极电压”创建单独的“霍尔电压”和“磁场”列,如下所示:

GateVolt Field  HallVoltage  GateVolt  Field  HallVolt   GateVolt  Field  HallVolt
0        1500          76     0.1       1485     72       0.2      1470        67
0        1490          75     0.1       1476     70       0.2      1465        62
0        1485          74      .         .        .        .         .        .
.          .            .      .         .        .        .         .        .
.          .            .      .         .        .        .         .        .
.          .            .      .         .        .        .         .        .

最后,我想绘制“霍尔电压”与“场”的关系图,并针对不同的栅极电压进行一些分析和拟合以及更多的绘图。

我几乎是编码和 python 的新手,我不知道接下来要做什么才能获得这样的输出。有什么办法可以实现我想要做的吗? 先谢谢了!!! :)

【问题讨论】:

  • List append 比np.append 快​​得多(也更容易使用)。您可以在循环后从列表中创建一个数组。对于具有列标题的显示,pandas 优于 numpy

标签: python-3.x numpy matplotlib data-analysis


【解决方案1】:

您可以使用groupby 分隔这些表,如下所示:

x = df.groupby(['GateVolt'])
for state, frame in x:
    print(f"{state!r}")
    print("------------------------")
    print(frame, end="\n\n")

【讨论】:

  • 非常感谢!但是如果我可以将它们分成列会更好,因为最后我必须用 matplotlib 绘制它们。
猜你喜欢
  • 2019-09-23
  • 1970-01-01
  • 2020-01-02
  • 2023-02-10
  • 2023-02-22
  • 1970-01-01
  • 2023-04-05
  • 2020-09-26
  • 1970-01-01
相关资源
最近更新 更多