[英]add a row to data frame and name it
I have a data frame with rows aj and want to add a row k.我有一个包含行 aj 的数据框,并且想要添加行 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?
当我使用 append function 时,它会添加第 k 行,但其他行周围有一个 (),即 (a)、(b) 等。有谁知道如何将这些 () 取出来? code in 1:
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: 2中的代码:
df.loc['k'] = ['lion', 1, 3, 'yes']
df
output of 2: output 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
:
df.loc['k', :] = ['lion', 1, 3, 'yes']
print(df)
Output: 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.