1

I need to select 3 columns from a single row of a 6-column dataframe and append it to another dataframe that has only 3 columns.

import pandas
import numpy

df1 = pd.DataFrame({'Name':['Sanjay','Robin','Hugo'],
                    'Email':['[email protected]','[email protected]','[email protected]'],
                    '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)

Here's what I was hoping to see:

     Name  OrderNo     Phone
0  Sanjay       23  555-1212

Here's the output from this exercise:

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)

2 Answers 2

1

You can also use the DataFrame.append() function:

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

    Name    OrderNo Phone
0   Sanjay  23  555-1212
Sign up to request clarification or add additional context in comments.

1 Comment

apologies for not clicking the check mark earlier. super helpful and many thanks!!
0

KeyError: (0, 2, 4) because type of row is pandas series which doesn't work like that.

also use import pandas as pd

df2 = pd.DataFrame()
print(df2)
for index, row in df1.iterrows():
    if index == 0:
        print(row)
        df2 = df2.append(row[['Name', 'OrderNo', 'Phone']])
        # df2 = df2.append(row.iloc[index, :])

print(df2)

output:

     Name  OrderNo     Phone
0  Sanjay     23.0  555-1212

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.