[英]pd.read_csv with some missing columns
I have a txt file as below and I want to read in this file as df, but got an error: Too many columns specified , because row 3 and row 4 only have 3 columns, is it possible to keep all my 5 columns and just let the missing columns in row 3 and 4 empty?我有一个如下的 txt 文件,我想在这个文件中读取为 df,但出现错误:指定的列太多,因为第 3 行和第 4 行只有 3 列,是否可以保留我所有的 5 列让第 3 行和第 4 行中缺失的列为空?
df = pd.read_csv(data, sep =";", dtype = str, headers = None)
1;2;3;4;5
1;2;3;4;5
1;2;3
1;2;3
ideal df:理想的df:
Col1 Col2 Col3 Col4 Col5
1 2 3 4 5
1 2 3 4 5
1 2 3
1 2 3
Works just fine for me.对我来说效果很好。 My pandas version == 1.2.1我的 pandas 版本 == 1.2.1
This is the command I used:这是我使用的命令:
pd.read_csv(data, sep =";", header=None)
the output: output:
0 1 2 3 4
0 1 2 3 4.0 5.0
1 1 2 3 4.0 5.0
2 1 2 3 NaN NaN
3 1 2 3 NaN NaN
You change the names of columns with names
parameter in your read_csv
function您在read_csv
function 中使用names
参数更改列的名称
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.