0

I have a csv file:

a,b,c 
1,2,3
4,5,6
7

as you can see the last row doesn't match with count of headers. So I want to raise an {ValueError}Shape of passed values is (3, 1), indices imply (3, 3) error when I read the file with pd.read_csv() function

But if I do it in this way: pd.DataFrame(np.array([[1, 2, 3], [4, 5, 6], [7]]), columns=['a', 'b', 'c']) I get this error.

Any thoughts? Thanks in advance!

1 Answers1

1

Using remove NaN from array you can try to create a new dataframe with NaNs filtered from values of dataframe from read_csv()

df = pd.read_csv(io.StringIO("""a,b,c 
1,2,3
4,5,6
7"""))#.astype("Int64")

try:
    pd.DataFrame(np.array([r[~np.isnan(r)] for r in df.values]), columns=df.columns)
except Exception as e:
    print(str(e))

output

Shape of passed values is (3, 1), indices imply (3, 3)
Rob Raymond
  • 29,118
  • 3
  • 14
  • 30