Essentially, I would like to add values to certain columns in an empty DataFrame with defined columns, but when I run the code, I get.
Empty DataFrame
Columns: [AP, AV]
Index: []
Code:
df = pd.DataFrame(columns=['AP', 'AV'])
df['AP'] = propName
df['AV'] = propVal
I think this could be a simple fix, but I've tried some different solutions to no avail. I've tried adding the values to an existing dataframe I have, and it works when I do that, but would like to have these values in a new, separate structure.
Thank you,
CodePudding user response:
df = pd.DataFrame({'AP': [propName]})
df['AV'] = propVal
print(df)
or
df = pd.DataFrame({'AP': [123, 233], 'AV': [345, 777]})
It's probably better to initialize with empty values:
df = pd.DataFrame({'AP' : [np.nan], 'AV': [np.nan]})
df['AP'] = 12345
df['AV'] = 12345
print(df)
CodePudding user response:
Assign propName and propValue to dictionary:
dict = {}
dict[propName] = propValue
Then, push to empty DataFrame, df:
df = pd.DataFrame()
df['AP'] = dict.keys()
df['AV'] = dict.values()
Probably not the most elegant solution, but works great for me.