2013-05-29 74 views
22

當將行附加到數據框時,是否有任何方法可以指定我想要的新行的索引?熊貓:將一行添加到數據框並指定其索引標籤

原始文檔提供the following example

In [1301]: df = DataFrame(np.random.randn(8, 4), columns=['A','B','C','D']) 

In [1302]: df 
Out[1302]: 
      A   B   C   D 
0 -1.137707 -0.891060 -0.693921 1.613616 
1 0.464000 0.227371 -0.496922 0.306389 
2 -2.290613 -1.134623 -1.561819 -0.260838 
3 0.281957 1.523962 -0.902937 0.068159 
4 -0.057873 -0.368204 -1.144073 0.861209 
5 0.800193 0.782098 -1.069094 -1.099248 
6 0.255269 0.009750 0.661084 0.379319 
7 -0.008434 1.952541 -1.056652 0.533946 

In [1303]: s = df.xs(3) 

In [1304]: df.append(s, ignore_index=True) 
Out[1304]: 
      A   B   C   D 
0 -1.137707 -0.891060 -0.693921 1.613616 
1 0.464000 0.227371 -0.496922 0.306389 
2 -2.290613 -1.134623 -1.561819 -0.260838 
3 0.281957 1.523962 -0.902937 0.068159 
4 -0.057873 -0.368204 -1.144073 0.861209 
5 0.800193 0.782098 -1.069094 -1.099248 
6 0.255269 0.009750 0.661084 0.379319 
7 -0.008434 1.952541 -1.056652 0.533946 
8 0.281957 1.523962 -0.902937 0.068159 

其中新行會自動獲取索引標籤。有沒有辦法控制新標籤?

回答

26

系列的name成爲數據幀行的index

In [99]: df = pd.DataFrame(np.random.randn(8, 4), columns=['A','B','C','D']) 

In [100]: s = df.xs(3) 

In [101]: s.name = 10 

In [102]: df.append(s) 
Out[102]: 
      A   B   C   D 
0 -2.083321 -0.153749 0.174436 1.081056 
1 -1.026692 1.495850 -0.025245 -0.171046 
2 0.072272 1.218376 1.433281 0.747815 
3 -0.940552 0.853073 -0.134842 -0.277135 
4 0.478302 -0.599752 -0.080577 0.468618 
5 2.609004 -1.679299 -1.593016 1.172298 
6 -0.201605 0.406925 1.983177 0.012030 
7 1.158530 -2.240124 0.851323 -0.240378 
10 -0.940552 0.853073 -0.134842 -0.277135 
+0

感謝@unutbu。你知道append是否返回原始數據框的複製/查看/引用? –

+5

它返回一份副本。你可以通過'df2 = df.append(s)'來判斷,然後是'df2.ix [0,'A'] = 1000',然後檢查'df'是否已經改變。 (它不。) – unutbu

+0

指令的藝術是使用最簡單的例子,將理解(在stackoverflow上的許多消息,而不是僅僅選擇一個)。順便說一句,什麼是xs? – gseattle

10

df.loc將做的工作:

>>> df = pd.DataFrame(np.random.randn(3, 2), columns=['A','B']) 
>>> df 
      A   B 
0 -0.269036 0.534991 
1 0.069915 -1.173594 
2 -1.177792 0.018381 
>>> df.loc[13] = df.loc[1] 
>>> df 
      A   B 
0 -0.269036 0.534991 
1 0.069915 -1.173594 
2 -1.177792 0.018381 
13 0.069915 -1.173594 
相關問題