Reputation: 85
When I try to add three new columns they all initialize with NaN values instead of the desired values. However if I perform some logic after the empty dataframe is initialized then the values do not get initialized as NaN values.
This does not work
auction = pandas.DataFrame()
//These values initialize to NaN
auction['Date'] = dataFrame['report_date'].loc[0]
auction['Market Location'] = dataFrame['market_location_name'].loc[0]
auction['State'] = dataFrame['market_location_state'].loc[0]
This works
auction = pandas.DataFrame()
for i in range(4):
//logic to initialize a few columns in auction
//These values initialize to the desired values
auction['Date'] = dataFrame['report_date'].loc[0]
auction['Market Location'] = dataFrame['market_location_name'].loc[0]
auction['State'] = dataFrame['market_location_state'].loc[0]
Its not a huge deal becuase I can always use insert to get the columns in the desired order but I am confused on why it isnt working with the first block of code.
Upvotes: 1
Views: 1338
Reputation: 29635
I guess it is mostly that in the case that you called "does not work", the problem is that the dataframe has no index and initializing a column with a single value just create the column name. While in the "case that works", you already have index in your dataframe before doing auction['Date'] = dataFrame[...
and in this case, the value is assign to each element in the column. with a simple example:
# example
dataFrame = pd.DataFrame({'a':list('mn'),
'b':range(2)})
print (dataFrame)
a b
0 m 0
1 n 1
Now case that "works"
auction = pd.DataFrame()
#initialize first with an iterable: like a list or a column from another dataframe
auction['c'] = range(2) #or auction['c'] = dataFrame['b']
auction['d'] = dataFrame['a'].loc[0]
print (auction)
c d
0 0 m
1 1 m
but the case that "does not work":
auction = pd.DataFrame()
auction['d'] = dataFrame['a'].loc[0]
print (auction) #the result is a empty dataframe with column d
# Empty DataFrame
# Columns: [d]
# Index: []
#now add a column from an iterable
auction['c'] = range(2) #or auction['c'] = dataFrame['b']
print (auction) #the column d did not have any value so filled with nan
# d c
# 0 NaN 0
# 1 NaN 1
Upvotes: 1