Reputation: 5
I'm trying to add a list as the 3rd column in a dataframe in a simple program, but keep getting the error
RuntimeWarning: '<' not supported between instances of 'str' and 'int', sort order is undefined for incomparable objects result = result.union(other)
Basically the program should look through the data and find matches of names, then add those along with their time value to a new data set. But I can't get anything to add onto the dataframe after the if statement. The dataframe already has 2 columns that use strings (the 'Name' and 'Time' column) and nothing uses ints so it doesn't make sense why this error comes up. The only thing I've been successful in getting to append is the "matches" which is already in the dataframe. But that is a list just like "test1".
Thank you in advance for any help you can give.
import os
import csv
import fileinput
import codecs
import pandas as pd
import os
os.chdir('/home/richardwiggles/Desktop')
df1 = pd.read_csv('df1.csv', names=['Place','Name','Time','City','State','Age'])
df2 = pd.read_csv('df2.csv', names=['Name','Time'])
test1 = ['a','b','c']
matchesDF = pd.DataFrame()
matches = []
for finishers in df1['Name']:
matches.append(df2[df2['Name'].str.match(finishers)])
matchesDF=matchesDF.append(matches, ignore_index=True)
matchesDF=matchesDF.append(test1, ignore_index=True)
matchesDF.to_csv(r'/home/richardwiggles/Desktop/data1.csv')
The df1 in excel looks like this:
Place | Name | City | State | Time |
---|---|---|---|---|
1 | name1 | city1 | state1 | 2:39:06 |
2 | name2 | city2 | state2 | 2:39:46 |
3 | name3 | city3 | state3 | 2:40:25 |
4 | name4 | city4 | state4 | 2:41:44 |
5 | name5 | city5 | state5 | 2:45:07 |
df2 in excel looks like this
Name | Finish |
---|---|
name1 | 2:31:20 |
name2 | 2:33:14 |
name3 | 2:34:44 |
name4 | 2:35:39 |
name5 | 2:36:00 |
Upvotes: 0
Views: 154
Reputation: 26
I created dummy dataframes and worked with your code, its working for me, with different names etc. Without knowing your dataframe, exact issue can not be deciphered. However, try using astype(str) before .str.match .
import os
import csv
import fileinput
import codecs
import pandas as pd
import os
data = {'Place':['EU', 'can', 'in', 'brz'],
'Name':['alex', 'tom', 'ken', 'keth'],
'Time': ['20:30','10:45','5:15','2:10'],
'City' : ['alt','rtx','fgd','hgf'],
'State' : ['az', 'kz','bz', 'cz'],
'Age' : [20,30,40,50]}
data2 = {'Name' : ['alex','keth',' '],
'Time' : ['10:10','7:05','5:15']}
df1 = pd.DataFrame(data)
df2 = pd.DataFrame(data2)
test1 = ['a','b','c']
matchesDF = pd.DataFrame()
matches = []
for finishers in df1['Name']:
matches.append(df2[df2['Name'].astype(str).str.match(finishers)])
matchesDF=matchesDF.append(matches, ignore_index=True)
matchesDF=matchesDF.append(test1, ignore_index=True)
matchesDF
output:
Name Time 0
0 alex 10:10 NaN
1 keth 7:05 NaN
2 NaN NaN a
3 NaN NaN b
4 NaN NaN c
br, Simanti.
Upvotes: 0