Reputation: 3577
I have a csv file which looks like this:
-0.08150654489363679, 0.3262445628643036, -0.1983973830938339, 0.04597456371557881
and I am reading the file in like this:
import pandas as pd
df=pd.read_csv(r'F:\Sheyenne\Statistics\IDL_stats\Basic_Stats\NDII\NDII_1984137_A_Annex.csv')
print df
which returns this:
Empty DataFrame
Columns: [-0.08150654489363679, 0.3262445628643036, -0.1983973830938339, 0.04597456371557881]
Index: []
I want to add column names to the columns like this:
df=pd.read_csv(r'F:\Sheyenne\Statistics\IDL_stats\Basic_Stats\NDII\NDII_1984137_A_Annex.csv')
df.columns=['Mean', 'Max', 'Min', 'Stdev']
print df
but when I do this I get this:
Empty DataFrame
Columns: [Mean, Max, Min, Stdev]
Index: []
My desired output is this:
Mean Max Min Stdev
-0.08150654489363679 0.3262445628643036 -0.1983973830938339 0.04597456371557881
something funny is going on when the dataframe is being read but Im not sure what.
Upvotes: 4
Views: 10944
Reputation: 393933
Pass the columns names as an arg to read_csv
:
df=pd.read_csv(r'F:\Sheyenne\Statistics\IDL_stats\Basic_Stats\NDII\NDII_1984137_A_Annex.csv', names=['Mean', 'Max', 'Min', 'Stdev'])
by default it treats the first header row as the column names so when you overwrite the columns you end up with an empty df as you only had a single row in your csv in the first place.
Also it looks like your file has initial white space, you can set to skip these too:
df=pd.read_csv(r'F:\Sheyenne\Statistics\IDL_stats\Basic_Stats\NDII\NDII_1984137_A_Annex.csv', names=['Mean', 'Max', 'Min', 'Stdev'], skipinitialspace=True)
Upvotes: 2