我有以下数据框。
company,compid,secid
a,a1,a2
b,b1,b2
我需要输出如下格式
company,compid,secid,url
a,a1,a2,http://www.cssss.com/companydetails.aspx?compid=a1&secid=a2
b,b1,b2,http://www.cssss.com/companydetails.aspx?compid=b1&secid=b2
如何在url列中添加列值?
答案 0 :(得分:3)
使用:
df['url'] = 'http://www.cssss.com/companydetails.aspx?compid=' +
df.compid + '&secid=' + df.secid
print (df)
company compid secid url
0 a a1 a2 http://www.cssss.com/companydetails.aspx?compi...
1 b b1 b2 http://www.cssss.com/companydetails.aspx?compi...
我更改url
进行测试:
df['url'] = 'http:aaa.aspx?compid=' + df.compid + '&secid=' + df.secid
print (df)
company compid secid url
0 a a1 a2 http:aaa.aspx?compid=a1&secid=a2
1 b b1 b2 http:aaa.aspx?compid=b1&secid=b2
如果dtypes
列compid
和secid
int
为str
,请astype
强制转换为df['url'] = 'http:aaa.aspx?compid='+df.compid.astype(str)+'&secid='+df.secid.astype(str)
print (df)
company compid secid url
0 a a1 a2 http:aaa.aspx?compid=a1&secid=a2
1 b b1 b2 http:aaa.aspx?compid=b1&secid=b2
:
compid
通过评论编辑:
如果secid
列NaN
和#code before
data3 = pd.merge(df1,df2,on='company', how='outer')
print (data3)
company compid secid
0 a a1 a2
1 b b1 b2
2 a a1 NaN
3 b NaN NaN
data3.loc[~data3[['compid','secid']].isnull().any(1), 'url'] =
'http:aaa.aspx?compid=' + data3.compid.astype(str) + '&secid=' + data3.secid.astype(str)
data3.fillna('', inplace=True)
print (data3)
company compid secid url
0 a a1 a2 http:aaa.aspx?compid=a1&secid=a2
1 b b1 b2 http:aaa.aspx?compid=b1&secid=b2
2 a a1
3 b
包含class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
def facebook
# raise request.env['omniauth.auth']
# # You need to implement the method below in your model (e.g. app/models/user.rb)
@user = User.from_omniauth(request.env["omniauth.auth"])
if @user.persisted?
sign_in_and_redirect @user, :event => :authentication #this will throw if @user is not activated
set_flash_message(:notice, :success, :kind => "Facebook") if is_navigational_format?
else
session["devise.facebook_data"] = request.env["omniauth.auth"]
redirect_to new_user_registration_url
end
end
def github
# raise request.env['omniauth.auth']
# # You need to implement the method below in your model (e.g. app/models/user.rb)
@user = User.from_omniauth(request.env["omniauth.auth"])
if @user.persisted?
sign_in_and_redirect @user, :event => :authentication #this will throw if @user is not activated
set_flash_message(:notice, :success, :kind => "Github") if is_navigational_format?
else
session["devise.github_data"] = request.env["omniauth.auth"]
redirect_to new_user_registration_url
end
end
def failure
redirect_to root_path
end
end
,则它可以为空字符串:
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :omniauthable
def self.from_omniauth(auth)
where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
user.email = auth.info.email
user.password = Devise.friendly_token[0,20]
user.full_name = auth.info.name # assuming the user model has a name
end
end
end