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?
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:
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
This is the command I used:
pd.read_csv(data, sep =";", header=None)
the 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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.