Python 在dataframe上使用.apply()将我的列按字母顺序重新排序?奇怪的行为
我看到Python 在dataframe上使用.apply()将我的列按字母顺序重新排序?奇怪的行为,python,pandas,Python,Pandas,我看到.apply()函数有一些奇怪的行为 我正在调用一个API,并用响应编辑行 出于隐私原因,我无法上传数据集,但这基本上是函数和简单示例数据帧: df = pd.DataFrame({'my_customers':['John','Foo'],'email':['email@gmail.com','othermail@yahoo.com']}) print(df) my_customers email 0 John email@
.apply()
函数有一些奇怪的行为
我正在调用一个API,并用响应编辑行
出于隐私原因,我无法上传数据集,但这基本上是函数和简单示例数据帧:
df = pd.DataFrame({'my_customers':['John','Foo'],'email':['email@gmail.com','othermail@yahoo.com']})
print(df)
my_customers email
0 John email@gmail.com
1 Foo othermail@yahoo.com
以及api调用:
def api_func(row):
name=row['my_customers']
email=row['email']
# send api request
response = api(name, email)
# if there is data in the response
if 'data' in response.keys():
# append our data to row
row['api_status'] = 'Data Found'
row['api_response']= response
return row
# otherwise flag no data found
else:
row['api_status'] = 'No Data Found'
return row
以及这个apply()函数后面的数据帧。这些列已按字母顺序重新排序:
df = df.apply(api_func,axis=1)
api_status api_response email my_customers
0 data found xyz email@gmail.com John
1 data found abc othermail@yahoo.com Foo
我已经阅读了文档,找不到任何关于为什么会发生这种情况的线索。我以前从未见过这种情况
有什么想法吗
编辑:完整代码:
def api_append(row):
# set up variables
api=get_api()
firstname=row['firstname']
lastname=row['lastname']
email=row['email']
phone=row['phone_only']
countrycode=row['country_code']
# send request
request = SearchAPIRequest(email=email,minimum_match=0.8,first_name=firstname,
last_name=lastname,phone=phone,country_code=countrycode,api_key=api)
# if there is an error, record it to the dataframe
try:
response = request.send()
response = response.to_dict()
except (ValueError, AttributeError) as e:
# append our error data to the row
row['Pipl_Api_Status'] = 'check error code'
row['Pipl_Api_response']= str(e)
return row
# if there is data in the response
if 'data' in response.keys():
# append our data to row
row['Api_Status'] = 'Data Found'
row['Api_response'] = response['data']
return row
# otherwise flag no data found
else:
row['Api_Status'] = 'No Data Found'
return row
df_sample = df_sample.apply(api_append,axis=1)
df=df.apply(api_func,axis=1)
还编辑了原始问题。如果你需要更多的信息,让我知道你确定它是否会这样做吗?我无法复制这个问题。它非常令人费解。。。我问题中的代码基本相同。我没有遗漏什么。我已经试着调试了一个小时了。它按字母顺序重新排列列的顺序!然而,我想我找到了一个解决办法,尽管我无法解释。在应用函数之前,我首先创建新列:row['api_status']=np.nan
和row['api_response']=np.nan
,而不是在apply()函数中创建它们。列的顺序不再改变。我很好奇。您可以尝试以下代码:import random def api_func(row):name=row['my_customers']email=row['email']response=random.randint(0,9)#如果响应中有数据,如果响应%2==0:row['api_status']='找到数据'row['api_response']='返回行其他:row['api_status']='No Data Found'row['api_response']='No response'返回行df=df.apply(api_func,axis=1)
@gokulkrishnar您的示例运行良好。我已经编辑了我的原始问题,基本上包含了完整的api调用。这是一个谜。df=df.apply(api_func,axis=1)
还编辑了原始问题。如果您需要更多信息,请告诉我您确定它是否会这样做吗?我无法复制该问题。这非常令人费解…我问题中的代码基本相同。我没有遗漏任何内容。我已经试着调试了一个小时。它按字母顺序重新排列列的顺序!呵呵wever,我想我找到了一个解决方案,尽管我无法解释。在应用函数之前,我首先创建了新列:row['api_status']=np.nan
和row['api_response']=np.nan
,而不是在apply()中创建它们
function。列的顺序不再更改。我很好奇。您可以尝试以下代码:import random def api_func(row):name=row['my_customers']email=row['email']response=random.randint(0,9)#如果响应中有数据,如果响应%2==0:row['api_status']['api_response']='response'返回行else:row['api_status']='No Data Found'row['api_response']='No response'返回行df=df.apply(api_func,axis=1)
@gokulkrishnan您的示例工作正常。我已经编辑了我的原始问题,基本上包含了完整的api调用。这是个谜。