我是靠谱客的博主 负责眼睛,这篇文章主要介绍pandas: pd.concat 用法,现在分享给大家,希望可以做个参考。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 17:00:18) [MSC v.1900 64 bit (AMD64)] on win32 Type "copyright", "credits" or "license()" for more information. >>> import pandas as pd >>> from pandas import Series >>> import numpy as np >>> s1=pd.Series(np.arange(10,13)) >>> s1 0 10 1 11 2 12 dtype: int32 >>> np.arange(10,13) array([10, 11, 12]) >>> s2=pd.Series(np.arange(100,103)) >>> s2 0 100 1 101 2 102 dtype: int32 >>> pd.concat([s1,s2]) 0 10 1 11 2 12 0 100 1 101 2 102 dtype: int32 >>> pd.concat([s1,s2],axis=1) 0 1 0 10 100 1 11 101 2 12 102 >>> pd.concat([s1,s2],keys=[1,2]) 1 0 10 1 11 2 12 2 0 100 1 101 2 102 dtype: int32 >>> pd.concat([s1,s2], keys = [1,2],names = ['from','ID']) from ID 1 0 10 1 11 2 12 2 0 100 1 101 2 102 dtype: int32 >>> pd.concat([s1,s2], axis = 1,keys = ['s1','s2'],names = ['from','ID']) s1 s2 0 10 100 1 11 101 2 12 102 >>> idx = 'this is a fake data'.split() >>> idx ['this', 'is', 'a', 'fake', 'data'] >>> df1 = pd.DataFrame({'Country':['China','Japan','Germany','USA','UK'],'Team':['A','B','A','C','D']},index = idx) >>> df1 Country Team this China A is Japan B a Germany A fake USA C data UK D >>> col = 'Country Team'.split() >>> idx_2 = ['fake','world'] >>> values = [['KLR',100],['abc',200]] >>> df2 = pd.DataFrame(values,index = idx_2, columns = col) >>> df2 Country Team fake KLR 100 world abc 200 >>> pd.concat([df1,df2]) Country Team this China A is Japan B a Germany A fake USA C data UK D fake KLR 100 world abc 200 >>> pd.concat([df1,df2],axis=1) Warning (from warnings module): File "__main__", line 1 FutureWarning: Sorting because non-concatenation axis is not aligned. A future version of pandas will change to not sort by default. To accept the future behavior, pass 'sort=False'. To retain the current behavior and silence the warning, pass 'sort=True'. Country Team Country Team a Germany A NaN NaN data UK D NaN NaN fake USA C KLR 100.0 is Japan B NaN NaN this China A NaN NaN world NaN NaN abc 200.0 >>> col = ['Team','SBF'] >>> idx_3= ['true','world'] >>> values3 = [['red','pm'],['orange','pl']] >>> df3 = pd.DataFrame(values3,index = idx_3, columns = col) >>> df3 Team SBF true red pm world orange pl >>> df1 Country Team this China A is Japan B a Germany A fake USA C data UK D >>> pd.concat([df1,df3]) Country SBF Team this China NaN A is Japan NaN B a Germany NaN A fake USA NaN C data UK NaN D true NaN pm red world NaN pl orange >>> df2 Country Team fake KLR 100 world abc 200 >>> pd.concat([df2,df3]) Country SBF Team fake KLR NaN 100 world abc NaN 200 true NaN pm red world NaN pl orange >>> df3 Team SBF true red pm world orange pl >>> df1 Country Team this China A is Japan B a Germany A fake USA C data UK D >>> df3 Team SBF true red pm world orange pl >>> df3 Team SBF true red pm world orange pl >>> df2 Country Team fake KLR 100 world abc 200 >>> pd.concat([df2,df3]) Country SBF Team fake KLR NaN 100 world abc NaN 200 true NaN pm red world NaN pl orange >>> pd.concat([df2,df3],axis = 1) Country Team Team SBF fake KLR 100.0 NaN NaN true NaN NaN red pm world abc 200.0 orange pl >>> pd.concat([df1.Team,df2.Team,df3.Team]) this A is B a A fake C data D fake 100 world 200 true red world orange Name: Team, dtype: object >>> pd.concat(df1['Team'],df2['Team'],df3['Team']) Traceback (most recent call last): File "<pyshell#43>", line 1, in <module> pd.concat(df1['Team'],df2['Team'],df3['Team']) File "C:Python36libsite-packagespandascorereshapeconcat.py", line 225, in concat copy=copy, sort=sort) File "C:Python36libsite-packagespandascorereshapeconcat.py", line 241, in __init__ '"{name}"'.format(name=type(objs).__name__)) TypeError: first argument must be an iterable of pandas objects, you passed an object of type "Series" >>> pd.concat(df1[['Team']],df2[['Team']],df3[['Team']]) Traceback (most recent call last): File "<pyshell#44>", line 1, in <module> pd.concat(df1[['Team']],df2[['Team']],df3[['Team']]) File "C:Python36libsite-packagespandascorereshapeconcat.py", line 225, in concat copy=copy, sort=sort) File "C:Python36libsite-packagespandascorereshapeconcat.py", line 241, in __init__ '"{name}"'.format(name=type(objs).__name__)) TypeError: first argument must be an iterable of pandas objects, you passed an object of type "DataFrame" >>> pd.concat([df2,df3]) Country SBF Team fake KLR NaN 100 world abc NaN 200 true NaN pm red world NaN pl orange >>> pd.concat([df2,df3],join = 'inner') Team fake 100 world 200 true red world orange >>> pd.concat([df2,df3],join = 'outer') Country SBF Team fake KLR NaN 100 world abc NaN 200 true NaN pm red world NaN pl orange >>> df1 Country Team this China A is Japan B a Germany A fake USA C data UK D >>> df2 Country Team fake KLR 100 world abc 200 >>> df3 Team SBF true red pm world orange pl >>> pd.concat([df2,df3]) Country SBF Team fake KLR NaN 100 world abc NaN 200 true NaN pm red world NaN pl orange >>> pd.concat([df2,df3], ignore_index = True) Country SBF Team 0 KLR NaN 100 1 abc NaN 200 2 NaN pm red 3 NaN pl orange >>>

参考:https://blog.csdn.net/ycyrym/article/details/105127980
https://blog.csdn.net/Asher117/article/details/84799845
https://www.cnblogs.com/wzdLY/p/9673767.html
https://blog.csdn.net/Asher117/article/details/84725199
https://blog.csdn.net/stevenkwong/article/details/52528616

最后

以上就是负责眼睛最近收集整理的关于pandas: pd.concat 用法的全部内容,更多相关pandas:内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(48)

评论列表共有 0 条评论

立即
投稿
返回
顶部