如何使用" loc"在pandas中添加行? " for"?

时间:2017-12-12 08:57:06

标签: python pandas for-loop

我想通过" loc"将数据帧的数据添加到新数据帧。我使用" loc"但是发生了错误。我可以添加数据吗?

>>> import pandas as pd    
>>> df = pd.DataFrame({'A': [1.0, 1.2, 3.4, 4.1, 8.2]})
>>> import pandas as pd
>>> df_new = pd.DataFrame(columns=['A'])
>>> for i in df:
...     df_new.loc[i] = df.loc[i]
... 
Traceback (most recent call last):
  File "/Users/Hajime/anaconda3/lib/python3.6/site-packages/pandas/core/indexing.py", line 1434, in _has_valid_type
    error()
  File "/Users/Hajime/anaconda3/lib/python3.6/site-packages/pandas/core/indexing.py", line 1429, in error
    (key, self.obj._get_axis_name(axis)))
KeyError: 'the label [A] is not in the [index]'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
  File "/Users/Hajime/anaconda3/lib/python3.6/site-packages/pandas/core/indexing.py", line 1328, in __getitem__
    return self._getitem_axis(key, axis=0)
  File "/Users/Hajime/anaconda3/lib/python3.6/site-packages/pandas/core/indexing.py", line 1551, in _getitem_axis
    self._has_valid_type(key, axis)
  File "/Users/Hajime/anaconda3/lib/python3.6/site-packages/pandas/core/indexing.py", line 1442, in _has_valid_type
    error()
  File "/Users/Hajime/anaconda3/lib/python3.6/site-packages/pandas/core/indexing.py", line 1429, in error
    (key, self.obj._get_axis_name(axis)))
KeyError: 'the label [A] is not in the [index]'

但是下面的代码成功了。

>>> df_new.loc[1] = df.loc[1]
>>> df_new
     A
1  1.2

3 个答案:

答案 0 :(得分:1)

为什么不看看for在这里迭代的内容?

In [353]: for i in df:
     ...:     print(i)
     ...:     
A

结论 - 对df的迭代会导致迭代列名。你正在寻找的东西是df.iterrows,或者是df.index的迭代。

例如,

for i, r in df.iterrows():
     df_new.loc[i, :] = r

df_new

     A
0  1.0
1  1.2
2  3.4
3  4.1
4  8.2

答案 1 :(得分:0)

错误在于此部分:

for i in df:
    df_new.loc[i] = df.loc[i]

对于 loc ,第一个参数是 index 。但列名

如果您只想将df添加到df_new。使用concat。

df_new = pd.concat([df_new, df])

答案 2 :(得分:0)

import pandas as pd    
df = pd.DataFrame({'A': [1.0, 1.2, 3.4, 4.1, 8.2]})
import pandas as pd
df_new = pd.DataFrame(columns=['A'])
for i in df:

只需添加:,然后我会在第一时间做你想做的事

df.loc[index of row, column name] 

现在你做错了什么?您将列名称作为不存在的行索引传递

df_new.loc[:,i] = df.loc[:,i]

无论如何你可以在1 go中传递所有列:

df_new[col_names]=df[col_names]

col_names是一个列表