Reputation: 3617
I have a Panda DataFrame which has 1646 X 26
shape. But when I am trying to write the fame in a csv file, the first row is getting skipped. I am getting 1645 X 26
shape in the csv file. So I looked up in the internet and I saw some solutions like using header = False, header = None, header = 0
. So I was following the solutions. But it got worse. Now I after using those solutions, the shape of the csv file is 1644 X 26
.
I believe to_csv()
is considering the first row as header. So I implicitly tried changing the header name like this:
I took all the column name of my dataframe by writing this:
cols = list(_df.columns.values)
Then in the to_csv()
I wrote like this:
_df.to_csv(path_or_buf = 'fantasy_data.csv', sep = ',', index = False, header = cols)
But it is still the same!!
Note
If I run _df.head()
, it returns the first row!
How to change this so that I can get all the data and to_csv()
won't skip any row?
Upvotes: 1
Views: 6459
Reputation: 1
I had the same problem and solved it by the following steps. First, read the csv file using pandas.read_csv and assign attribute "header=None". Second, when applying to_csv assign the header and index attributes as following "header=["col1","col2","col3"], index=False". Using this way the first row is not skipped.
Upvotes: -1
Reputation: 863166
Sample:
_df = pd.DataFrame({'A':list('abcdef'),
'B':[4,5,4,5,5,4],
'C':[7,8,9,4,2,3],
'D':[1,3,5,7,1,0]})
print (_df)
A B C D
0 a 4 7 1
1 b 5 8 3
2 c 4 9 5
3 d 5 4 7
4 e 5 2 1
5 f 4 3 0
print (_df.shape)
(6, 4)
#if need omit header and index in csv
print(_df.to_csv(sep = ',', index = False, header = None))
a,4,7,1
b,5,8,3
c,4,9,5
d,5,4,7
e,5,2,1
f,4,3,0
if want omit only index
print(_df.to_csv(sep = ',', index = False))
A,B,C,D
a,4,7,1
b,5,8,3
c,4,9,5
d,5,4,7
e,5,2,1
f,4,3,0
Upvotes: 4