简体   繁体   English

如何拆分标题值并将它们转换为不同的列?

[英]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.

相关问题 如何从我的 json 列中删除分隔的 pipe 并将它们拆分为不同的列及其各自的值 - How to remove delimeted pipe from my json column and split them to different columns and their respective values 如何将 python 中的字典列表拆分为具有值名称的列? - How do I split a list of dicts in python into columns with names of the values? 如何将一行的值拆分为 pandas 中的不同列? - How to split values of a row into different columns in pandas? 如何在 Pandas 中将多列转换为单独的行/值? - How do I convert multiple columns to individual rows/values in pandas? 如何将列拆分为不同的向量 - How can I split columns into a different vector 合并值分为不同的列 - Merge values split into different columns 如何将具有不同长度值的列表转换为字典? - How do I convert a list with values of different lengths into a dictionary? 数据框列的数字很多是 str 格式,很多是浮点数,我如何将它们全部转换为浮点数 - Dataframe columns have numbers many of them in str format and many in float, how do I convert all of them to float 如何使用 pandas 根据来自不同列的多个值生成不同的列 - How do I generate different columns based on multiple values from different columns using pandas 如何拆分多个列? - How do I split multiple columns?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM