python - 使用 iterrows(),从数据帧的单行中插入列的子集作为另一个数据帧中的新行

标签 python pandas for-loop dataframe row

我需要从 6 列数据帧的单行中选择 3 列,并将其附加到另一个只有 3 列的数据帧。

import pandas
import numpy

df1 = pd.DataFrame({'Name':['Sanjay','Robin','Hugo'],
                    'Email':['s@email.com','r@email.com','h@email.com'],
                    'OrderNo':[23,234,66],
                    'Address':['234 West Ave','45 Oak Street','Rt. 3443 FM290'],
                    'Phone':['555-1212','555-3322','555-6655'],
                    'Age':[34,23,65]})

df2 = pd.DataFrame(columns = ['Name', 'OrderNo', 'Phone'])

for index, row in df1.iterrows():
    if index == 0:
        df2 = df2.append(row[0,2,4])
        #df2 = df2.append(row['Name','OrderNo','Phone'])  <------also threw key error

print(df2)

这是我希望看到的:

     Name  OrderNo     Phone
0  Sanjay       23  555-1212

以下是本练习的输出:

Traceback (most recent call last):
  File "/Users/Joe/anaconda3/lib/python3.6/site-packages/pandas/core/indexes/base.py", line 3109, in get_value
    return libindex.get_value_box(s, key)
  File "pandas/_libs/index.pyx", line 55, in pandas._libs.index.get_value_box
  File "pandas/_libs/index.pyx", line 63, in pandas._libs.index.get_value_box
TypeError: 'tuple' object cannot be interpreted as an integer

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 15, in <module>
  File "/Users/Joe/anaconda3/lib/python3.6/site-packages/pandas/core/series.py", line 766, in __getitem__
    result = self.index.get_value(self, key)
  File "/Users/Joe/anaconda3/lib/python3.6/site-packages/pandas/core/indexes/base.py", line 3117, in get_value
    raise e1
  File "/Users/Joe/anaconda3/lib/python3.6/site-packages/pandas/core/indexes/base.py", line 3103, in get_value
    tz=getattr(series.dtype, 'tz', None))
  File "pandas/_libs/index.pyx", line 106, in pandas._libs.index.IndexEngine.get_value
  File "pandas/_libs/index.pyx", line 114, in pandas._libs.index.IndexEngine.get_value
  File "pandas/_libs/index.pyx", line 162, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/hashtable_class_helper.pxi", line 1492, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas/_libs/hashtable_class_helper.pxi", line 1500, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: (0, 2, 4)

最佳答案

您还可以使用 DataFrame.append() 函数:

df2=df2.append(df1[['Name', 'OrderNo', 'Phone']].iloc[0])
df2

    Name    OrderNo Phone
0   Sanjay  23  555-1212

关于python - 使用 iterrows(),从数据帧的单行中插入列的子集作为另一个数据帧中的新行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51797491/

相关文章:

java - 在数组中添加元素

for-loop - 批处理命令查找并运行文件

python - Matplotlib Pandas : display columns name inside a stacked barchart

python - 如何从一列中获取与另一列匹配的最大值?

javascript - 从数组中的对象获取 bool 值,然后为其分配 DOM 属性

python - Seaborn 直方图使列变白

python - 为什么 OrderedDict 命名为驼峰大小写,而 defaultdict 命名为小写?

python - PyQt部署 "Unable to copy file"

python - 如何从 python 中的文件一次读取一个字符?

python - 如何在考虑行子集的同时遍历 Pandas DataFrame