Reputation:
Real simple question that I cant seem to get. For the following single column DF:
Cost
1
What syntax would I use to print "Cost = 1". i know print df['Cost'] would be 1. But i want the column name to be in the output.
Upvotes: 0
Views: 394
Reputation: 8273
If you have more than one column
df = pd.DataFrame({'Product':['x','y'],'Cost': [1,2]})
col_names=df.columns
for row in range(0,len(df)):
for col_name,col in zip(col_names,df.iloc[row]):
print("{}={}".format(col_name,col))
Upvotes: 0
Reputation: 164673
This is one way without having to reference your column name(s) explicitly.
df = pd.DataFrame({'Cost': [1]})
for k in df:
print('{0} = {1}'.format(k, df[k].iloc[0]))
# Cost = 1
Upvotes: 2