Im having trouble appending rows to dataframes in pandas.
The data is read from a excel sheet and put into a DataFrame. Here is a sample piece:
import pandas as pd
df1 = pd.DataFrame({'date': ['22-jun-18', '22-jun-18', '22-jun-18'],
'id': ['1', '2', '3', ]
'name': ['Mark', 'Kate', 'Rollo' ]
'errors': ['10', '20', '30' ]
'status': ['failed', 'failed', 'failed', ]
'comment': ['Reason: invalid id', 'Reason: invalid id', 'Reason: invalid id']
'system': ['X', 'X', 'X' ]
'version': ['1.1', '1.1', '1.1' ]
'producer': ['Sys', 'Sys', 'Sys' ]})
The code:
find_row = searchById(row['ID'], df1)
Returns a row from df1
using ID, works fine. Print shows a row and all columns with data.
And:
df2 = df2.append(find_row, ignore_index=True)
Adds the row, but puts NaN
in the last column.
the find_row
object looks like this when I print it:
date 22-jun-18
id 2
name Kate
errors 20
status failed
comment Reason: invalid id
system X
version 1.1
producer Sys
Name: 2, dtype: object
A total of 9 values, no problems. After appending to the new DataFrame it displays like this:
date id name errors status comment system version producer
0 22-jun-18 86758 Kate 20 failed Reason: Invalid id System X 1.1 NaN
Everything works fine except column 9 which is now NaN
.
Here is the searchById
function. As said earlier it returns an object with all the data I need
def searchById(id, df):
for index, row in df.iterrows():
if(row['key'] == id):
return row
return None
Is the problem the append
function, or am I handling the rows and DataFrames in the wrong way?