I want to append data from column and a default phrase at the same time into a Pandas DataFrame, db has many columns, not only id_sin & extra...
I tried this:
import pandas as pd
db = pd.DataFrame({'id_sin':['s123','s124','s125','s126'],
'extra':['abc','def','ghi','jkl'],
...
})
df = pd.DataFrame(columns=['id_sin','phrase'])
df = df.append([db['id_sin'],['default phrase']*len(db)])
This is the expected result:
>>> df
id_sin phrase
0 s123 default phrase
1 s124 default phrase
2 s125 default phrase
3 s126 default phrase
But I am getting an error, please help... thanks in advance!
db['Phrase'] = 'default phrase'????db) with many columns and what you would like is to create another DataFrame with a subset of the original columns (in this case just'id_sin') and another column called phrase which takes on a static value for all rows? If so, you want:df = db[['id_sin']].assign(phrase='default phrase'). Otherwise please try to explain the problem more clearly.