How to rise "{ValueError}Shape of passed values is ..., indices imply ..." during pandas read_csv() function?

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!

Upvotes: 0

Views: 83

Answers (1)

Rob Raymond
Rob Raymond

Reputation: 31146

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)

Upvotes: 1

Related Questions