Pandas:为数据框添加值

时间:2016-07-25 13:21:19

标签: python pandas

我有df

ID,"url","used_at","active_seconds"
9e871f65d402254b15470881c79cec94,"mvideo.ru/smartfony-sotovye-telefony/smartfony-205",2016-04-01 00:00:00,10
8fe968e88d209b2854c47f812211cd2d,"group.aliexpress.com/ruNew.htm?spm=2114.7749990.120000116.87.rxes4x&catId=0&page=17#product-list-tab",2016-04-01 00:00:01,32
0ba8d1c3feff21ff15a86ad77ba60b9e,"hz.ru.aliexpress.com/item/Newest-1Pcs-Novelty-Design-Funny-Pvc-Fish-Shape-Zipper-Pencil-Bag-Stationery-Supplies-Pencil-Case-Fantastic/32632783689.html?aff_platform=aaf&sk=eub6yrrBy%3A&cpt=1459447196454&af=13862&cv=2043168&cn=1o4x0n v5z81gr2tvie6cjw5apalm3lr4&dp=v5_1o4x0nv5z81gr2tvie6cjw5apalm3lr4&afref=http%3A%2F%2Fvk.com%2Faway.ph&aff_trace_key=52dd417516c0437484c16009b0fab9b2-1459447196454-01255-eub6yrrBy",2016-04-01 00:00:01,24
9dedf5c86704a4ceb7d72d3b465056a0,"ru.aliexpress.com/af/category/202040573/4.html?isAffiliate=y&site=rus&shipCountry=RU&g=y&needQuery=n",2016-04-01 00:00:01,10
0e12c8425fd2dbd919feb1055b007460,"dns-shop.ru/product/4ba05489c41e3120/avtosignalizacia-starline-a61-dialog#comment",2016-04-01 00:00:02,2
dff9169891d7e5b5585c75e2391561e4,"ru.aliexpress.com/item/AEVOGUE-Cool-Cat-Eye-Sunglasses-Women-Summer-Style-Sun-Glasses-Brand-Designer-Vintage-Gafas-Oculos-De/32380450045.html",2016-04-01 00:00:02,4
5cecd2312b300766a7668784b464dd69,"ru.aliexpress.com/category/202001922/girls-clothing.html?site=rus&SortType=price_asc&g=y&shipCountry=RU&needQuery=n&isFreeShip=y&isrefine=y",2016-04-01 00:00:03,6
61d59ec6c83bacdefc510b0fe3d2c449,"ru.aliexpress.com/item/2013-autumn-new-chiffon-harem-pants-trousers-AB7-3-C40/1242434570.html?spm=2114.30010608.3.61.YAjncN&ws_ab_test=searchweb201556_9,searchweb201602_3_10036_10035_10034_507_10020_10017_10005_10006_10021_10022_10007_10018_10019,searchweb201603_9&btsid=1403b8fd-21f4-436e-920a-c329bee1586f",2016-04-01 00:00:03,329

我还有另一个df2

 ID
6a1de956f88cdde3760a09922fa688d7
b285f7d87e9e85ca67b939e77ce5ba1d
fb2a53cf613e7fdfb98ef82af2db1fa6
f3a62fcfb7734537c3d798d866c905e2
8e1f1b728b235532f6e9243f8639c98e
7d276f5712e9ef6f331fc9234f28be5e

我需要从ID添加df2,所有值等于0。 我该怎么办?

1 个答案:

答案 0 :(得分:0)

您可以在pd.concat的dfs列表中使用fillna(0)

In [68]:
new_df = pd.concat([df,df2], ignore_index=True).fillna(0)
new_df

Out[68]:
                                  ID  active_seconds  \
0   9e871f65d402254b15470881c79cec94            10.0   
1   8fe968e88d209b2854c47f812211cd2d            32.0   
2   0ba8d1c3feff21ff15a86ad77ba60b9e            24.0   
3   9dedf5c86704a4ceb7d72d3b465056a0            10.0   
4   0e12c8425fd2dbd919feb1055b007460             2.0   
5   dff9169891d7e5b5585c75e2391561e4             4.0   
6   5cecd2312b300766a7668784b464dd69             6.0   
7   61d59ec6c83bacdefc510b0fe3d2c449           329.0   
8   6a1de956f88cdde3760a09922fa688d7             0.0   
9   b285f7d87e9e85ca67b939e77ce5ba1d             0.0   
10  fb2a53cf613e7fdfb98ef82af2db1fa6             0.0   
11  f3a62fcfb7734537c3d798d866c905e2             0.0   
12  8e1f1b728b235532f6e9243f8639c98e             0.0   
13  7d276f5712e9ef6f331fc9234f28be5e             0.0   

                                                  url              used_at  
0   mvideo.ru/smartfony-sotovye-telefony/smartfony...  2016-04-01 00:00:00  
1   group.aliexpress.com/ruNew.htm?spm=2114.774999...  2016-04-01 00:00:01  
2   hz.ru.aliexpress.com/item/Newest-1Pcs-Novelty-...  2016-04-01 00:00:01  
3   ru.aliexpress.com/af/category/202040573/4.html...  2016-04-01 00:00:01  
4   dns-shop.ru/product/4ba05489c41e3120/avtosigna...  2016-04-01 00:00:02  
5   ru.aliexpress.com/item/AEVOGUE-Cool-Cat-Eye-Su...  2016-04-01 00:00:02  
6   ru.aliexpress.com/category/202001922/girls-clo...  2016-04-01 00:00:03  
7   ru.aliexpress.com/item/2013-autumn-new-chiffon...  2016-04-01 00:00:03  
8                                                   0                    0  
9                                                   0                    0  
10                                                  0                    0  
11                                                  0                    0  
12                                                  0                    0  
13                                                  0                    0