如何解决不匹配的 columnspecs,因此使用 pd.read_fwf 和使用 colspecs 读取值错误
我正在使用 pd.read_fwf
读取文本文件,如下所示:
import pandas as pd
specs_test =[(19,20),(20,21),(21,23),(23,26)]
names_test = ["Record_Type","Resident_Status","State_Occurrence_FIPS","County_Occurrence_FIPS"]
test_l = pd.read_fwf('test.txt',header=None,names = names_test,colspecs= specs_test)
和test.txt如下:
11SC059
11SC051
11SC019
11SC033
11SC007
11SC041
22SC079
11SC043
11SC045
22SC079
读取文件test_l后如下:
Record_Type Resident_Status State_Occurrence_FIPS County_Occurrence_FIPS
0 1 S C0 59
1 1 S C0 51
2 1 S C0 19
3 1 S C0 33
4 1 S C0 7
5 1 S C0 41
6 2 S C0 79
7 1 S C0 43
8 1 S C0 45
9 2 S C0 79
但是,根据我的 colspec,它应该具有以下内容(我刚刚按预期添加了第一行):
1 1 SC 059
我在这里错过了什么?非常感谢您的帮助!
解决方法
我在将您的数据粘贴到测试文件并修复元组时得到了这个。
specs_test =[(18,19),(19,20),(20,22),(22,25)]
names_test = ["Record_Type","Resident_Status","State_Occurrence_FIPS","County_Occurrence_FIPS"]
pd.read_fwf('test.txt',header=None,names = names_test,colspecs= specs_test )
它删除了第 4 列的前导零,因此您可能需要使用 kwargs 来发送数据类型或在导入后修复该列
Record_Type Resident_Status State_Occurrence_FIPS County_Occurrence_FIPS
0 1 1 SC 59
1 1 1 SC 51
2 1 1 SC 19
3 1 1 SC 33
4 1 1 SC 7
5 1 1 SC 41
6 2 2 SC 79
7 1 1 SC 43
8 1 1 SC 45
9 2 2 SC 79
,
首先,您被索引关闭。试试:
specs_test =[(18,25)]
此外,对于数值,前导零将被忽略。要保留它们,您可以通过添加以下内容转换为字符串:
converters = {h:str for h in names_test}
最终代码可以是:
import pandas as pd
specs_test =[(18,25)] ## Here you where off by an index.
names_test = ["Record_Type","County_Occurrence_FIPS"]
test_l = pd.read_fwf('test.txt',colspecs= specs_test,converters = {h:str for h in names_test}) ## If you want to keep the leading
## zeros you can convert to string.
结果:
Record_Type Resident_Status State_Occurrence_FIPS County_Occurrence_FIPS
0 1 1 SC 059
1 1 1 SC 051
2 1 1 SC 019
3 1 1 SC 033
4 1 1 SC 007
5 1 1 SC 041
6 2 2 SC 079
7 1 1 SC 043
8 1 1 SC 045
9 2 2 SC 079
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。