[英]How do I split header values and convert them in different columns?
I have a data which looks like this我有一个看起来像这样的数据
However I need to separate the header values and convert them into three different column and it should look like the following.但是,我需要将标题值分开并将它们转换为三个不同的列,它应该如下所示。
What is the best method to achieve this?实现这一目标的最佳方法是什么?
import matplotlib.pyplot as plt
import pandas as pd
import math
import os
url = 'https://raw.githubusercontent.com/callmedemi/AMPSE/main/machinepara1_gm_m1.csv'
dataset = pd.read_csv(url, encoding='latin')
print(dataset)
dataset1 = dataset.columns
print(dataset1)
The answer looks like this:答案如下:
wn ... leafValue( OP("/I0/M1" "gm") "Vbias" 2 "lp" 4e-05 "wp" 0.0002 ) (S)
0 0.000005 ... 6.773000e-21
1 0.000008 ... 6.774000e-21
2 0.000010 ... 6.775000e-21
3 0.000013 ... 6.776000e-21
4 0.000015 ... 6.776000e-21
[5 rows x 251 columns]
Index(['wn ',
'leafValue( OP("/I0/M1" "gm") "Vbias" 0 "lp" 1.5e-05 "wp" 0.00012 ) (S)',
'leafValue( OP("/I0/M1" "gm") "Vbias" 0 "lp" 1.5e-05 "wp" 0.00014 ) (S)',
'leafValue( OP("/I0/M1" "gm") "Vbias" 0 "lp" 1.5e-05 "wp" 0.00016 ) (S)',
'leafValue( OP("/I0/M1" "gm") "Vbias" 0 "lp" 1.5e-05 "wp" 0.00018 ) (S)',
'leafValue( OP("/I0/M1" "gm") "Vbias" 0 "lp" 1.5e-05 "wp" 0.0002 ) (S)',
'leafValue( OP("/I0/M1" "gm") "Vbias" 0 "lp" 2.125e-05 "wp" 0.00012 ) (S)',
'leafValue( OP("/I0/M1" "gm") "Vbias" 0 "lp" 2.125e-05 "wp" 0.00014 ) (S)',
'leafValue( OP("/I0/M1" "gm") "Vbias" 0 "lp" 2.125e-05 "wp" 0.00016 ) (S)',
'leafValue( OP("/I0/M1" "gm") "Vbias" 0 "lp" 2.125e-05 "wp" 0.00018 ) (S)',
...
'leafValue( OP("/I0/M1" "gm") "Vbias" 2 "lp" 3.375e-05 "wp" 0.00012 ) (S)',
'leafValue( OP("/I0/M1" "gm") "Vbias" 2 "lp" 3.375e-05 "wp" 0.00014 ) (S)',
'leafValue( OP("/I0/M1" "gm") "Vbias" 2 "lp" 3.375e-05 "wp" 0.00016 ) (S)',
'leafValue( OP("/I0/M1" "gm") "Vbias" 2 "lp" 3.375e-05 "wp" 0.00018 ) (S)',
'leafValue( OP("/I0/M1" "gm") "Vbias" 2 "lp" 3.375e-05 "wp" 0.0002 ) (S)',
'leafValue( OP("/I0/M1" "gm") "Vbias" 2 "lp" 4e-05 "wp" 0.00012 ) (S)',
'leafValue( OP("/I0/M1" "gm") "Vbias" 2 "lp" 4e-05 "wp" 0.00014 ) (S)',
'leafValue( OP("/I0/M1" "gm") "Vbias" 2 "lp" 4e-05 "wp" 0.00016 ) (S)',
'leafValue( OP("/I0/M1" "gm") "Vbias" 2 "lp" 4e-05 "wp" 0.00018 ) (S)',
'leafValue( OP("/I0/M1" "gm") "Vbias" 2 "lp" 4e-05 "wp" 0.0002 ) (S)'],
dtype='object', length=251)
If you see the for each "lp", "wp" gives five different values and for each "Vbias"(range:0-2) there are five different lp values.如果您看到每个“lp”,“wp”给出五个不同的值,每个“Vbias”(范围:0-2)有五个不同的 lp 值。 The header is the main issue here as I can't extract the header.
标题是这里的主要问题,因为我无法提取标题。
What I want to do is, extract "Vbias", "lp" and "wp" from the header for each of "gm" values and place them in corresponding columns.我想要做的是,从每个“gm”值的标题中提取“Vbias”、“lp”和“wp”,并将它们放在相应的列中。 The picture files are just for a better understanding, the real CSV file is in the code.
图片文件只是为了更好的理解,真正的CSV文件在代码中。
When trying to read the CSV file from the provided URL, I got and encoding error, so I first needed to an argument to specify a valid encoding to be used.尝试从提供的 URL 读取 CSV 文件时,我遇到了编码错误,因此我首先需要一个参数来指定要使用的有效编码。
Then, since the column headers seem to be unusable and we know the exact names we want to put there, we may simply replace the data frame columns names with our own list:然后,由于列标题似乎不可用,而且我们知道要放在那里的确切名称,我们可以简单地用我们自己的列表替换数据框列名称:
import pandas as pd
url = 'https://raw.githubusercontent.com/callmedemi/AMPSE/main/trial.csv'
dataset = pd.read_csv(url, encoding='latin')
# remove imported column headers
df = dataset[1:]
# and define new ones, based on our knowledge of the data
df.columns = ['wm', 'gm', 'Vbias', 'lp', 'wp' ]
print(df)
Result:结果:
wm gm Vbias lp wp
1 0.000007 4.250000e-08 4.350000e-08 4.440000e-08 4.540000e-08
2 0.000010 5.480000e-08 5.580000e-08 5.670000e-08 5.770000e-08
3 0.000013 6.710000e-08 6.810000e-08 6.900000e-08 7.000000e-08
4 0.000015 7.940000e-08 8.040000e-08 8.130000e-08 8.230000e-08
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.