Reputation: 616
If I do the following, I get a dataframe with the dictionary keys as headers and the values in a row:
d = {'a':['a'], 'b':['b'], 'c':['c']}
pd.DataFrame(d)
a b c
0 a b c
If I do the same but add column names, the dataframe is empty...
d = {'a':['a'], 'b':['b'], 'c':['c']}
pd.DataFrame(d, columns = [1,2,3])
1 2 3
Why?
Upvotes: 2
Views: 779
Reputation: 140
Because you didn't rename the column you set new ones up, try:
d = {'a':['a'], 'b':['b'], 'c':['c']}
df=pd.DataFrame(d)
df.rename(columns = {'a': 1, 'b': 2, 'c':3})
Upvotes: 0
Reputation: 18377
This happens because, in your data d
there are no values/column names with names 1
,2
, and 3
.
From pandas doc, the parameter columns:
columns : Index or array-like Column labels to use for resulting frame. Will default to RangeIndex (0, 1, 2, …, n) if no column labels are provided.
Here you can see all three cases:
df = pd.DataFrame(d) #Uses default method for columns
print(df)
Output:
a b c
0 a b c
Passing RangeIndex values to columns
:
d = {'a':['a'], 'b':['b'], 'c':['c']}
pd.DataFrame(d, columns = [1,2,3]) #These values are not the RangeIndex values, but label values
Output:
Empty dataframe #Because there's no columns names 1, 2, or 3
Passing the Index values to columns
:
df = pd.DataFrame(d,columns = ['a','b','c'])
print(df)
Output:
a b c
0 a b c
Upvotes: 2
Reputation: 30940
this happens because you are reindexing the columns of your dataframe to [0,1,2] and the values really exist in 'a', 'b', 'c', then you need:
d = {'a':['a'], 'b':['b'], 'c':['c']}
df=pd.DataFrame(d)
print(df)
a b c
0 a b c
df.rename(columns={'a':0,'b':1,'c':2},inplace=True)
print(df)
0 1 2
0 a b c
Also you can use:
df.columns=[0,1,2]
d = {'a':['a'], 'b':['b'], 'c':['c']}
df=pd.DataFrame(d)
print(df)
a b c
0 a b c
df.columns=[0,1,2]
print(df)
0 1 2
0 a b c
Upvotes: 2