简体   繁体   English

使用 pandas 将.dat 文件转换为 csv 格式时出现问题,需要将 1 列拆分为多列

[英]Problem in converting .dat file into csv format using pandas, Need to split 1 column into multiple columns

I am facing an issue in converting a.dat extension file into csv file using pandas.我在使用 pandas 将 a.dat 扩展文件转换为 csv 文件时遇到问题。 I have written the following basic code into google colab:我已将以下基本代码写入 google colab:

from google.colab import files
uploaded = files.upload()
KY1801.32m.dat(n/a) - 335449 bytes, last modified: 4/27/2021 - 100% done
Saving KY1801.32m.dat to KY1801.32m (2).dat
dat_file = "KY1801.32m.dat"
with open(dat_file,'r') as file:
  text = file.read()
  print(text)
import pandas as pd
import numpy as np
with open(dat_file,'r') as file:
  df = pd.DataFrame(file)
  print(df.head())
df["sno","year","month","day","hours","minutes","sec","x","y","z","w"]= df[0].str.split(" " , expand 
= True)
df

My issue remains that after using the last line of my code, I am getting an error something like the following:我的问题仍然是在使用我的代码的最后一行后,我收到如下错误:

--------------------------------------------------------------------------- KeyError Traceback (most recent call last) /usr/local/lib/python3.7/dist-packages/pandas/core/generic.py in _set_item(self, key, value) 3575 try: -> 3576 -------------------------------------------------- ------------------------- KeyError Traceback(最近一次调用最后)/usr/local/lib/python3.7/dist-packages/pandas/ _set_item(self, key, value) 3575 中的 core/generic.py 尝试:-> 3576
loc = self._info_axis.get_loc(key) 3577 except KeyError: loc = self._info_axis.get_loc(key) 3577 除了 KeyError:

8 frames KeyError: ('sno', 'year', 'month', 'day', 'hours', 'minutes', 'sec', 'x', 'y', 'z', 'w') 8 帧 KeyError: ('sno', 'year', 'month', 'day', 'hours', 'minutes', 'sec', 'x', 'y', 'z', 'w')

During handling of the above exception, another exception occurred:在处理上述异常的过程中,又出现了一个异常:

ValueError Traceback (most recent call last) ValueError Traceback(最近一次调用最后一次)
/usr/local/lib/python3.7/dist-packages/pandas/core/internals/blocks.py in init (self, values, placement, ndim) 129 if self._validate_ndim and self.ndim and len(self.mgr_locs).= len(self:values). /usr/local/lib/python3.7/dist-packages/pandas/core/internals/blocks.py in init (self, values, place, ndim) 129 if self._validate_ndim and self.ndim and len(self.mgr_locs ).= len(self:values)。 130 raise ValueError( --> 131 f"Wrong number of items passed {len(self,values)}. " 132 f"placement implies {len(self.mgr_locs)}" 133 ) 130 raise ValueError(--> 131 f"传递的项目数错误 {len(self,values)}。" 132 f"位置暗示 {len(self.mgr_locs)}" 133 )

ValueError: Wrong number of items passed 12, placement implies 1 ValueError:错误的项目数通过 12,位置意味着 1

What I want to do is to split my single column 0 in dat file into 12 constituent columns namely: sno, year, month, day, hours, minutes, seconds,x,y,z,w columns based on " " separation.我想要做的是将 dat 文件中的单列 0 拆分为 12 个组成列,即:基于“”分隔的 sno、年、月、日、小时、分钟、秒、x、y、z、w 列。 How should I do that?我该怎么做? What are the changes needed in my code?我的代码需要进行哪些更改?

My.dat file can be found here: https://drive.google.com/file/d/1SXEB0Dj2PDUYU31RlLYBPG4858Re8M-J/view?usp=sharing My.dat 文件可以在这里找到: https://drive.google.com/file/d/1SXEB0Dj2PDUYU31RlLYBPG4858Re8M-J/view?usp=sharing

.dat is creating confusion here, in this case, it's a file with space delimited. .dat 在这里造成混乱,在这种情况下,它是一个以空格分隔的文件。 so just put the separator = " "所以只要把分隔符=“”

import pandas as pd
df=pd.read_csv("KY1801.32m.dat",sep= " ")

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将.dat 文件转换为.csv 文件时出现问题 - problem with converting .dat file to .csv file 将python文件中的数据分组时,将CSV文件中的一列拆分为多列(无熊猫) - Split one column in CSV file into multiple columns while grouping the data in Python (without Pandas) Python pandas.read_csv使用逗号将列拆分为多个新列 - Python pandas.read_csv split column into multiple new columns using comma to separate 如何在熊猫中将具有常规格式的一个字符串列拆分为多个列 - How to split one string column with regular format to multiple columns in Pandas 需要将pandas dataframe列中的可变长度数据拆分为多个列 - Need to split variable length data in a pandas dataframe column into multiple columns 读取csv文件并拆分保留列名称的列。 熊猫 - Read csv file and split in columns keeping column names. Pandas 如何使用数据帧在新列中拆分两个 CSV 文件列,显示 pandas 中的匹配项? - How to split up two CSV file columns in a new column, showing matches in pandas, using dataframes? 我需要使用 python 将 csv 文件中的一列拆分为两列 - I need to split one column in csv file into two columns using python 使用python和pandas将时间戳列拆分为CSV中的两个新列 - Split timestamp column into two new columns in CSV using python and pandas 在Pandas中将一列拆分为多列 - Split a column into multiple columns in Pandas
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM