我收到上述类型错误:
TypeError:无法隐式地将'int'对象转换为str。
第34行,<lambda>
output[column] = output['Index_for_s'].apply(lambda x: s(x, column, file_two))
尝试运行以下程序时:
import pandas as pd
import os
def get_index(x, data):
return list(data[data['COL1'] == x].index)
def suvrule(ind, col, data):
data=pd.DataFrame(data)
return data.ix [ind,col].sum()
file_one_path = input('Please enter file one: ')
file_two_path = input('Please enter file two: ')
if os.path.exists(file_one_path) and os.path.exists(file_two_path):
file_one = pd.read_csv(file_one_path)
file_two = pd.read_csv(file_two_path)
try:
assert (file_one.shape[0] == file_one.shape[0])
except AssertionError:
print ("Check Data.")
exit()
output = file_one.groupby('COL1', sort='False')['COL2'].agg('count').reset_index()
output['Index_for_s'] = output['COL1'].apply(lambda x: get_index(x, file_one))
cols_for_s = [col for col in file_two.columns if 'Header' not in col]
for column in cols_for_s:
output[column] = output['Index_for_s'].apply(lambda x: s(x, column, file_two))
output = output.drop('Index_for_s', axis= 1)
print ("\nWriting output to output.csv in current working directory.")
output.to_csv("output.csv", index='False')
else:
print ('Incorrect file path')
答案 0 :(得分:0)
试试这个: 将行更改为此。 [STR(Index_for_s)]