How do I add a new row to my dataframe, with values that are based on the column names?
For example
Dog = 'happy'
Cat = 'sad'
df = pd.DataFrame(columns=['Dog', 'Cat'])
I want to add a new line to the dataframe where is pulls in the variable of the column heading
Dog Cat
0 happy sad
You can try append
:
df.append({'Dog':Dog,'Cat':Cat}, ignore_index=True)
Output:
Dog Cat
0 happy sad
You can use append in a different way(other than mentioned above one)
df = pd.DataFrame([[1, 2], [3, 4]], columns=list('AB')) df output: A B 0 1 2 1 3 4 df2 = pd.DataFrame([[5, 6], [7, 8]], columns=list('AB')) df.append(df2) output: A B 0 1 2 1 3 4 0 5 6 1 7
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.