I have a data frame with rows aj and want to add a row k. when i use the append function it adds row k but then the other rows have a () around them, ie (a), (b), etc. does anyone know how to get those () out of there? code in 1:
import pandas as pd
import numpy as np
from pandas import Series, DataFrame
data = {'animal': ['cat', 'cat', 'snake', 'dog', 'dog', 'cat', 'snake','cat', 'dog', 'dog'],
'age': [2.5, 3, 0.5, np.nan, 5, 2, 4.5, np.nan, 7, 3],
'visits': [1, 3, 2, 3, 2, 3, 1, 1, 2, 1],
'priority': ['yes', 'yes', 'no', 'yes', 'no', 'no', 'no', 'yes', 'no', 'no']}
labels = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
df = pd.DataFrame(data, index=[labels])
df
code in 2:
df.loc['k'] = ['lion', 1, 3, 'yes']
df
output of 2:
animal age visits priority
(a,) cat 2.5 1 yes
(b,) cat 3.0 3 yes
(c,) snake 0.5 2 no
(d,) dog NaN 3 yes
(e,) dog 5.0 2 no
(f,) cat 2.0 3 no
(g,) snake 4.5 1 no
(h,) cat NaN 1 yes
(i,) dog 7.0 2 no
(j,) dog 3.0 1 no
k lion 1.0 3 yes
Add :
to df.loc
:
df.loc['k', :] = ['lion', 1, 3, 'yes']
print(df)
Output:
animal age visits priority
a cat 2.5 1.0 yes
b cat 3.0 3.0 yes
c snake 0.5 2.0 no
d dog NaN 3.0 yes
e dog 5.0 2.0 no
f cat 2.0 3.0 no
g snake 4.5 1.0 no
h cat NaN 1.0 yes
i dog 7.0 2.0 no
j dog 3.0 1.0 no
k lion 1.0 3.0 yes
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.