这是我的数据框 df:
df = pd.DataFrame({'a': [0.671399,0.446172,0.614758],
'b' : [ 0.101208 ,-0.243316 ,0.075793],
'c':[-0.181532 ,0.051767, -0.451460]})
a b c
0 0.671399 0.101208 -0.181532
1 0.446172 -0.243316 0.051767
2 0.614758 0.075793 -0.451460
我想再添加一个索引更多的列,所以我尝试了这个:
df['e'] = pd.Series(data = [-0.335485, -1.166658, -0.385571,-1.166658 ],index=[0,1,2,3])
但结果不是添加index = 3 行。
a b c e
0 0.671399 0.101208 -0.181532 -0.335485
1 0.446172 -0.243316 0.051767 -1.166658
2 0.614758 0.075793 -0.451460 -0.385571
答案 0 :(得分:1)
为Series
添加名称,并使用默认outer
加入concat
或join
使用https://laravel.com/docs/5.5/passport#personal-access-tokens - 需要指定outer
加入,默认为left
:
s = pd.Series(data = [-0.335485, -1.166658, -0.385571,-1.166658 ],index=[0,1,2,3], name='e')
df = pd.concat([df, s], axis=1)
#alternative solution
#df = df.join(s, how='outer')
print (df)
a b c e
0 0.671399 0.101208 -0.181532 -0.335485
1 0.446172 -0.243316 0.051767 -1.166658
2 0.614758 0.075793 -0.451460 -0.385571
3 NaN NaN NaN -1.166658
将名称设置为Series
的另一种方法是使用rename
:
s = pd.Series(data = [-0.335485, -1.166658, -0.385571,-1.166658 ],index=[0,1,2,3])
df = df.join(s.rename('e'), how='outer')
print (df)
a b c e
0 0.671399 0.101208 -0.181532 -0.335485
1 0.446172 -0.243316 0.051767 -1.166658
2 0.614758 0.075793 -0.451460 -0.385571
3 NaN NaN NaN -1.166658
答案 1 :(得分:1)
使用---
- hosts: b1
become: yes
become_user: root
tasks:
- name: who
shell: whoami >> file.txt
join
答案 2 :(得分:0)
以下是使用combine_first
df['e']=np.nan
df.combine_first(s.to_frame().rename(columns={0:'e'}))
Out[374]:
a b c e
0 0.671399 0.101208 -0.181532 -0.335485
1 0.446172 -0.243316 0.051767 -1.166658
2 0.614758 0.075793 -0.451460 -0.385571
3 NaN NaN NaN -1.166658