Python 尝试将带有空格的数据拆分为两个不同的列
我正在尝试将数字由1个或多个空格分隔的数据拆分为两个不同的列。它的名字叫DUT0,我想把它分成两列,分别是“温度”和“电压” 这是我的代码:Python 尝试将带有空格的数据拆分为两个不同的列,python,pandas,Python,Pandas,我正在尝试将数字由1个或多个空格分隔的数据拆分为两个不同的列。它的名字叫DUT0,我想把它分成两列,分别是“温度”和“电压” 这是我的代码: import pandas as pd import re as r from matplotlib import pyplot as plt file = pd.read_table('sample.txt') print(file) file[['vtg', 'temp']] = file['DUT0'].str.split('\s+-\s+', ex
import pandas as pd
import re as r
from matplotlib import pyplot as plt
file = pd.read_table('sample.txt')
print(file)
file[['vtg', 'temp']] = file['DUT0'].str.split('\s+-\s+', expand=True)
print(file)
我经常遇到这样的错误:“列的长度必须与键的长度相同”
数据中的数字在两个数字之间有1/多个空格。我只想把这两个数字分成两列,列出第二列
文本如下所示:
DUT0
5600 2.3
5650 12.9
5700 23891
这两个数字之间有不同数量的空格
预期产出:
温度Vtg
5600 2.3
5650 12.9
5700 23891
使用
read\u table
将sep
指定为正则表达式:
file = pd.read_table('sample.txt',
sep='\s+',
engine='python',
skiprows=1,
header=None,
names=['vtg', 'temp'])
file.head()
# vtg temp
# 0 5600 2.3
# 1 5650 12.9
# 2 5700 23891.0
你能复制粘贴问题中的部分文本吗?只需
.split()
“123 4432”.split()
试着打印等号的右边,看看数据是否与预期相符。你应该包括文件['DUT0']
的前几行,以及预期的输出。文件[['vtg','temp']=file['DUT0'].str.split(expand=True)
我试过了,它有三列,DUT0和两个数据,温度和电压