Skip to main content
 首页 » 编程设计

Python + Pandas + 数据框 : couldn't append one dataframe to another

2024年12月31日20sharpest

我有两个大 CSV 文件。我已将它们转换为 Pandas 数据帧。它们都有相同名称和相同顺序的列:event_name、category、category_id、description。我想将一个数据帧附加到另一个数据帧,最后想将结果数据帧写入 CSV。我为此写了一段代码:

 #appendind a new dataframe to the older dataframe 
 data = pd.read_csv("dataset.csv") 
 data1 = pd.read_csv("dataset_new.csv") 
 dfs = [data, data1] 
 pd.concat([df.squeeze() for df in dfs], ignore_index=True) 
 dfs = pd.DataFrame(columns=['event_name','category',  'category_id', 'description']) 
 dfs.to_csv('dataset_append.csv', encoding='utf-8', index=False) 

我想向您展示 print(dfs) 的输出,但我不能,因为 Stackoverflow 显示以下错误,因为输出太长:

Body is limited to 30000 characters; you entered 32132. 

你能告诉我一个你成功使用的代码片段吗 附加 Pandas 数据框?

编辑1:

print(dfs) 

输出:

--------------------------------------------------------- 
[     Unnamed: 10 Unnamed: 100 Unnamed: 101 Unnamed: 102 Unnamed: 103  \ 
0            NaN          NaN          NaN          NaN          NaN    
1            NaN          NaN          NaN          NaN          NaN    
2            NaN          NaN          NaN          NaN          NaN    
3            NaN          NaN          NaN          NaN          NaN    
4            NaN          NaN          NaN          NaN          NaN    
5            NaN          NaN          NaN          NaN          NaN    
6            NaN          NaN          NaN          NaN          NaN    
7            NaN          NaN          NaN          NaN          NaN    
8            NaN          NaN          NaN          NaN          NaN    
9            NaN          NaN          NaN          NaN          NaN    
10           NaN          NaN          NaN          NaN          NaN    
11           NaN          NaN          NaN          NaN          NaN    
12           NaN          NaN          NaN          NaN          NaN    
13           NaN          NaN          NaN          NaN          NaN    
14           NaN          NaN          NaN          NaN          NaN    
15           NaN          NaN          NaN          NaN          NaN    
16           NaN          NaN          NaN          NaN          NaN    
17           NaN          NaN          NaN          NaN          NaN    
18           NaN          NaN          NaN          NaN          NaN    
19           NaN          NaN          NaN          NaN          NaN    
20           NaN          NaN          NaN          NaN          NaN    
21           NaN          NaN          NaN          NaN          NaN    
22           NaN          NaN          NaN          NaN          NaN    
23           NaN          NaN          NaN          NaN          NaN    
24           NaN          NaN          NaN          NaN          NaN    
25           NaN          NaN          NaN          NaN          NaN    
26           NaN          NaN          NaN          NaN          NaN    
27           NaN          NaN          NaN          NaN          NaN    
28           NaN          NaN          NaN          NaN          NaN    
29           NaN          NaN          NaN          NaN          NaN    
...          ...          ...          ...          ...          ...    
 
1159         NaN          NaN          NaN          NaN          NaN    
1160         NaN          NaN          NaN          NaN          NaN    
1161         NaN          NaN          NaN          NaN          NaN    
1162         NaN          NaN          NaN          NaN          NaN    
 
     Unnamed: 104 Unnamed: 105 Unnamed: 106 Unnamed: 107 Unnamed: 108  \ 
0             NaN          NaN          NaN          NaN          NaN    
1             NaN          NaN          NaN          NaN          NaN    
2             NaN          NaN          NaN          NaN          NaN    
3             NaN          NaN          NaN          NaN          NaN    
4             NaN          NaN          NaN          NaN          NaN    
5             NaN          NaN          NaN          NaN          NaN    
6             NaN          NaN          NaN          NaN          NaN    
7             NaN          NaN          NaN          NaN          NaN    
...           ...          ...          ...          ...          ...    
 
 
1161          NaN          NaN          NaN          NaN          NaN    
1162          NaN          NaN          NaN          NaN          NaN    
 
                            ...                         Unnamed: 94  \ 
0                           ...                                 NaN    
1                           ...                                 NaN    
2                           ...                                 NaN    
3                           ...                                 NaN    
4                           ...                                 NaN    
5                           ...                                 NaN    
6                           ...                                 NaN    
7                           ...                                 NaN    
8                           ...                                 NaN    
9                           ...                                 NaN    
10                          ...                                 NaN    
11                          ...                                 NaN    
12                          ...                                 NaN    
13                          ...                                 NaN    
14                          ...                                 NaN    
15                          ...                                 NaN    
16                          ...                                 NaN    
17                          ...                                 NaN    
18                          ...                                 NaN    
19                          ...                                 NaN    
20                          ...                                 NaN    
21                          ...                                 NaN    
22                          ...                                 NaN    
23                          ...                                 NaN    
24                          ...                                 NaN    
25                          ...                                 NaN    
26                          ...                                 NaN    
27                          ...                                 NaN    
28                          ...                                 NaN    
29                          ...                                 NaN    
...                         ...                                 ...    
1133                        ...                                 NaN    
1134                        ...                                 NaN    
1135                        ...                                 NaN    
1136                        ...                                 NaN    
1137                        ...                                 NaN    
1138                        ...                                 NaN    
1139                        ...                                 NaN    
1140                        ...                                 NaN    
1141                        ...                                 NaN    
1142                        ...                                 NaN    
1143                        ...                                 NaN    
1144                        ...                                 NaN    
1145                        ...                                 NaN    
1146                        ...                                 NaN    
1147                        ...                                 NaN    
1148                        ...                                 NaN    
1149                        ...                                 NaN    
1150                        ...                                 NaN    
1151                        ...                                 NaN    
1152                        ...                                 NaN    
1153                        ...                                 NaN    
1154                        ...                                 NaN    
1155                        ...                                 NaN    
1156                        ...                                 NaN    
1157                        ...                                 NaN    
1158                        ...                                 NaN    
1159                        ...                                 NaN    
1160                        ...                                 NaN    
1161                        ...                                 NaN    
1162                        ...                                 NaN    
 
     Unnamed: 95 Unnamed: 96 Unnamed: 97 Unnamed: 98 Unnamed: 99  \ 
0            NaN         NaN         NaN         NaN         NaN    
1            NaN         NaN         NaN         NaN         NaN    
2            NaN         NaN         NaN         NaN         NaN    
3            NaN         NaN         NaN         NaN         NaN    
4            NaN         NaN         NaN         NaN         NaN    
 
...          ...         ...         ...         ...         ...    
1133         NaN         NaN         NaN         NaN         NaN    
1134         NaN         NaN         NaN         NaN         NaN    
1135         NaN         NaN         NaN         NaN         NaN    
1136         NaN         NaN         NaN         NaN         NaN    
 
 
                     category category_id  \ 
0                    Business           2    
1                 stage shows          33    
2                  Literature          15    
3        Science & Technology          22    
4                      health          11    
5        Science & Technology          22    
6                     Outdoor          19    
7                 stage shows          33    
8                   nightlife          30    
9         fashion & lifestyle           6    
10      Government & Activism          25    
11                stage shows          33    
12    Religion & Spirituality          21    
13                    Outdoor          19    
14                 management          17    
15       Science & Technology          22    
16                  nightlife          30    
17                    Outdoor          19    
18              FAMILy & kids           5    
19        fashion & lifestyle           6    
20              FAMILy & kids           5    
21                      games          10    
22                    hobbies          32    
23                    hobbies          32    
24    Religion & Spirituality          21    
25                     health          11    
26        fashion & lifestyle           6    
27         career & education          31    
28                     health          11    
29                       arts           1    
...                       ...         ...    
1133         Sports & Fitness          23    
1134         Sports & Fitness          23    
1135         Sports & Fitness          23    
1136         Sports & Fitness          23    
1137         Sports & Fitness          23    
1138         Sports & Fitness          23    
1139         Sports & Fitness          23    
1140         Sports & Fitness          23    
1141         Sports & Fitness          23    
1142         Sports & Fitness          23    
1143         Sports & Fitness          23    
1144         Sports & Fitness          23    
1145         Sports & Fitness          23    
1146         Sports & Fitness          23    
1147         Sports & Fitness          23    
1148         Sports & Fitness          23    
1149         Sports & Fitness          23    
1150         Sports & Fitness          23    
1151         Sports & Fitness          23    
1152         Sports & Fitness          23    
1153         Sports & Fitness          23    
1154         Sports & Fitness          23    
1155         Sports & Fitness          23    
1156         Sports & Fitness          23    
1157         Sports & Fitness          23    
1158         Sports & Fitness          23    
1159         Sports & Fitness          23    
1160         Sports & Fitness          23    
1161         Sports & Fitness          23    
1162         Sports & Fitness          23    
 
                                            description  \ 
0     Josh Talks in partnership with Facebook is all...    
1     Unwind on the strums of Guitar &  immerse your...    
2     Book review for grade 3 and above learners. 3 ...    
3     ..About Organizer:.This is the official page f...    
4     Blood Donation is organized under the banner o...    
5     A day "Etched with Innovation and Learning" to...    
6     Our next destination for Fun with us is "Goa" ...    
7     Enjoy the Soulful and Unplugged Performance of...    
8     Get ready with your dance shoes on as our favo...    
9     FESTIVE HUES -- a fashion and lifestyle exhibi...    
10    On Aug. 8, Dr. Ambedkar presides over the Depr...    
11    It's A Rapper Boys..And M Write A New Rap song...    
12    The Spiritual Makeover..A weekend workshop tha...    
13    Our next destination for Fun with us is "Goa" ...    
14    Project Management is all about getting the th...    
15    World Conference Next Generation Testing 2018 ...    
16    ..About Organizer:.Whitefield is now #Sherlocked!    
17    On occasion of 72th Independence Day , Udaan O...    
18    *Smilofy Special Superstar*.A Talent hunt for ...    
19    ITEEHA is coming back to Bengaluru, after a fa...    
20    This is an exciting course for kids to teach t...    
21    ..About Organizer:.PPG Lounge is a next genera...    
22    Touch Feel Try & Buy the latest #car and #bike...    
23    Sniper Media is organising an exclusive semina...    
24    He has all sorts of powers and able solve any ...    
25    registration fee 50/₹  we r providing free c...    
26    World Biggest Pageant Miss & Mrs World Queen a...    
27    ..About Organizer:.Canam Consultants - India's...    
28    Innopharm is an effort to bring innovations in...    
29    The first Central India Art and Design Expo - ...    
...                                                 ...    
1133  As the cricket fever grips the country again, ...    
1134  An evening of fun, food, drinks and rooting fo...    
1135  The time has come, who will take their place S...    
1136  Do you want to prove that Age is not a barrier...    
1137  We Invite All The Corporate Companies To Be A ...    
1138  PlayTM happy to announce you that conducting o...    
1139  A Mix of fun rules and cricketing skills. Afte...    
1140  Shuttle Swap presents Singles, Doubles and Mix...    
1141  Yonex Mavis 350 Shuttle will be used State/Nat...    
1142  Light up the FIFA World Cup with Bud90 Match S...    
1143  We are charmed to launch the SVSEVENTZ.COM 5-A...    
1144  We corephysio FC invite you for our first foot...    
1145  After completing the 2nd season of Bangalore S...    
1146  As the cricket fever grips the country again, ...    
1147  Introducing BOX Cricket Super 6 Corporate Cric...    
1148  After the sucess of '1st Matt & Mudd T20 Leagu...    
1149  Hi All, It is my pleasure to officially announ...    
1150  Sign up: Get early updates, free movie voucher...    
1151  About VIVO Pro Kabaddi 2018: A new season of t...    
1152  The Hero Indian Super League (ISL) is India's ...    
1153  Limited time offer: Free Paytm Movie Voucher w...    
1154  The 5th edition of the Indian Super League is ...    
1155  Calling all Jamshedpur FC fans! Here's your ch...    
1156  Empower yourself and progress towards a health...    
1157  Making people happy when they feel that its en...    
1158  LOVE YOGA ?- but too busy with work during the...    
1159  The coolest way to tour the city ! Absorb the ...    
1160  Ready to be a part of India's Biggest Walkatho...    
1161  The event will comprise of the following Open ...    
1162  RUN FOR CANCER CHILDREN On world Cancer Day 3r...    
 
                                             event_name   
0                             Josh Talks Hyderabad 2018   
1                        Guitar Night With Ashmik Patil   
2                         Book Review - August 2018 - 2   
3                                               Csaw'18   
4                                   Blood donation camp   
5     Rajasthan Youth Innovation and Technical Intel...   
6               Goa – Fun All the Way!!! - Mom N Kids   
7     The AnshUdhami Project LIVE at Tales & Spirits...   
8                         Friday Fiesta featuring Pearl   
9                                          FESTIVE HUES   
10                                               Nagpur   
11                             Yo Yo Deep SP The Rapper   
12                               The Spiritual Makeover   
13          Goa Fun All the Way - Women Only group Tour   
14                  MS Project 2016 - A one day seminar   
15             World Conference Next Generation Testing   
16                         Weekend Booster - Happy Hour   
17    Ladies Only Camping : Freedom To Travel (Seaso...   
18                                    Special superstar   
19                             Malaysian Batik Workshop   
20                   EQ Enhancement Course (5-10 years)   
21                         CS:GO Tournament 2018 - PPGL   
22                 Auto Mall at Mantri Square Bangalore   
23    A Seminar by Ojas Rajani (Bollywood celebrity ...   
24    rishikesh katti greatest Spirituality guru of ...   
25                    free BMD camp held on 26 jan 2018   
26    Miss and Mrs Bhopal Madhya Pradesh India World...   
27        USA, Canada & Singapore Application Days 2018   
28                                          Innopharm 3   
29                      Kalasrishti Art and Design Expo   
...                                                 ...   
1133  Asia cup live screening at la casa Brewery+ ki...   
1134  Asia Cup 2018 live screening at La Casa Brewer...   
1135  FIFA FINAL AT KORAMANGALA TETTO - With #fifa#f...   
1136              Womenasia Indoor Cricket Championship   
1137            Switch Hit Corporate Cricket Tournament   
1138             PlayTM Sports Arena Box Cricket league   
1139  The Box Cricket League Edition II (16-17-18 No...   
1140  Shuttle Swap Badminton Tournament - With Singl...   
1141             SPARK BADMINTON LEAGUE - OCT 14th 2018   
1142                   Bud90 Match Screenings at Loft38   
1143                       5 A-Side Football Tournament   
1144  5 vs 5 Football league - With Back 2 Track events   
1145  Bangalore Sports Carnival Table Tennis Juniors...   
1146  Asia cup live screening at la casa Brewery+ ki...   
1147                   Super 6 Corporate Cricket League   
1148  Coolulu is organizing MATT & MUD T20 Cricket L...   
1149    United Sportzs Pure Corporate Cricket season-10   
1150  Sign up for updates on the VIVO Pro Kabaddi Se...   
1151  VIVO Pro Kabaddi - UP Yoddha vs Patna Pirates ...   
1152  HERO Indian Super League 2018-19: Kerala Blast...   
1153                       HERO ISL: FC Goa Memberships   
1154  Hero Indian Super League 2018-19: Delhi Dynamo...   
1155  HERO Indian Super League 2018-19: Jamshedpur F...   
1156                  Yoga Therapy Classes in Bangalore   
1157                                    Saree Walkathon   
1158             Weekend Yoga Teachers Training Program   
1159                                    Bangalore Walks   
1160                        Oxfam Trailwalker Bengaluru   
1161       TAD Pune 2018 (Triathlon Aquathlon Duathlon)   
1162                            RUN FOR CANCER CHILDREN   
 
[1163 rows x 241 columns],                                             event_name              category  \ 
0                 Musical Camping at Dahanu Chiku farm               outdoor    
1                            Adventure Camping at Wada               outdoor    
2                                    Kaas Plateau Tour               outdoor    
3                  Pawna Lake Camping, kevre, Lonavala               outdoor    
4               Night Trek and Camping at Korigad Fort               outdoor    
5                                         PARAMOTORING               outdoor    
6    WATERFALL TREK & BEACH CAMPING (NAGALAPURAM: N...               outdoor    
7                      Happiest Land On Earth - Bhutan               outdoor    
8    4 Days serial hiking in Sahyadris - Sep 29 to ...               outdoor    
9                                     Ride To Valparai               outdoor    
10     Dzongri Trek - Gateway to Kanchenjunga Mountain               outdoor    
11                  Skandagiri Night Trek With Camping               outdoor    
12                  Kalsubai Trek | Plan The Unplanned               outdoor    
13                              Bike N Hike Skandagiri               outdoor    
14        Unplanned Stories - Episode 6 - Travel Tales               outdoor    
15                Feast on authentic flavors from Goa!               outdoor    
16                                       The Boot Camp               outdoor    
17   The HandleBards: Romeo and Juliet at Ranga Sha...               outdoor    
18   Workshop on Metagenomic Sequencing on the Grid...  Science & Technology    
19                                          Aerovision  Science & Technology    
20                Electric Vehicle Technology Workshop  Science & Technology    
21                                 BPM Strategy Summit  Science & Technology    
22         Summit of Interior Designers & Architecture  Science & Technology    
23                       SMART ASIA India Expo& Summit  Science & Technology    
24                        A Smart City Life Exhibition  Science & Technology    
25                                   OPEN SOURCE INDIA  Science & Technology    
26                          SolarRoofs India Bangalore  Science & Technology    
27   International Conference on Innovative Researc...  Science & Technology    
28   International Conference on Business Managemen...  Science & Technology    
29    DevOn Summit Bangalore - Digital Transformations  Science & Technology    
..                                                 ...                   ...    
144  Asia cup live screening at la casa Brewery+ ki...      Sports & Fitness    
145  Asia Cup 2018 live screening at La Casa Brewer...      Sports & Fitness    
146  FIFA FINAL AT KORAMANGALA TETTO - With #fifa#f...      Sports & Fitness    
147              Womenasia Indoor Cricket Championship      Sports & Fitness    
148            Switch Hit Corporate Cricket Tournament      Sports & Fitness    
149             PlayTM Sports Arena Box Cricket league      Sports & Fitness    
150  The Box Cricket League Edition II (16-17-18 No...      Sports & Fitness    
151  Shuttle Swap Badminton Tournament - With Singl...      Sports & Fitness    
152             SPARK BADMINTON LEAGUE - OCT 14th 2018      Sports & Fitness    
153                   Bud90 Match Screenings at Loft38      Sports & Fitness    
s    
170                                    Bangalore Walks      Sports & Fitness    
171                        Oxfam Trailwalker Bengaluru      Sports & Fitness    
172       TAD Pune 2018 (Triathlon Aquathlon Duathlon)      Sports & Fitness    
173                            RUN FOR CANCER CHILDREN      Sports & Fitness    
 
     category_id                                        description  \ 
0             19  Dear All Camping Lovers, Come take camping exp...    
1             19  Our Adventure campsite at Wada is developed wi...    
2             19  Type: Eco Tour Height: 3937 FT above MSL (Appr...    
3             19  Our Pawna Lake Camping site is located near Ke...    
4             19  Type: Hill Fort Height: 3050 Feet above MSL (A...    
 
23            22  Making 'Smart Cities Mission' a Reality The SM...    
24            22  A Smart City Life A Smart City Life Exhibition...    
25            22  Asia's No. 1 Convention on Open Source Started...    
26            22  The conference will offer an excellent platfor...    
27            22  Provides a leading forum for the presentation ...    
28            22  Provide opportunity for the global participant...    
29            22  The biggest event about Digital Transformation...    
..           ...                                                ...    
144           23  As the cricket fever grips the country again, ...    
145           23  An evening of fun, food, drinks and rooting fo...    
146           23  The time has come, who will take their place S...    
147           23  Do you want to prove that Age is not a barrier...    
148           23  We Invite All The Corporate Companies To Be A ...    
149           23  PlayTM happy to announce you that conducting o...    
150           23  A Mix of fun rules and cricketing skills. Afte...    
151           23  Shuttle Swap presents Singles, Doubles and Mix...    
152           23  Yonex Mavis 350 Shuttle will be used State/Nat...    
153           23  Light up the FIFA World Cup with Bud90 Match S...    
154           23  We are charmed to launch the SVSEVENTZ.COM 5-A...    
155           23  We corephysio FC invite you for our first foot...    
156           23  After completing the 2nd season of Bangalore S...    
157           23  As the cricket fever grips the country again, ...    
158           23  Introducing BOX Cricket Super 6 Corporate Cric...    
159           23  After the sucess of '1st Matt & Mudd T20 Leagu...    
160           23  Hi All, It is my pleasure to officially announ...    
161           23  Sign up: Get early updates, free movie voucher...    
162           23  About VIVO Pro Kabaddi 2018: A new season of t...    
163           23  The Hero Indian Super League (ISL) is India's ...    
164           23  Limited time offer: Free Paytm Movie Voucher w...    
165           23  The 5th edition of the Indian Super League is ...    
166           23  Calling all Jamshedpur FC fans! Here's your ch...    
167           23  Empower yourself and progress towards a health...    
168           23  Making people happy when they feel that its en...    
169           23  LOVE YOGA ?- but too busy with work during the...    
170           23  The coolest way to tour the city ! Absorb the ...    
171           23  Ready to be a part of India's Biggest Walkatho...    
172           23  The event will comprise of the following Open ...    
173           23  RUN FOR CANCER CHILDREN On world Cancer Day 3r...    
 
    Unnamed: 4 Unnamed: 5   
0          NaN        NaN   
1          NaN        NaN   
2          NaN        NaN   
3          NaN        NaN   
4          NaN        NaN   
 
24         NaN        NaN   
25         NaN        NaN   
26         NaN        NaN   
27         NaN        NaN   
28         NaN        NaN   
29         NaN        NaN   
..         ...        ...   
144        NaN        NaN   
145        NaN        NaN   
146        NaN        NaN   
147        NaN        NaN   
148        NaN        NaN   
149        NaN        NaN   
 
 
[174 rows x 6 columns]] 

请您参考如下方法:

简单的有什么问题:

pd.concat([df1, df2], ignore_index=True)).to_csv('File.csv', index=False) 

如果它们具有相同的列,这将起作用。

提取特定列的更详细的方法是:

(pd.concat([df1[['event_name','category',  'category_id', 'description']],  
df2[['event_name','category',  'category_id', 'description']]],  
ignore_index=True)) 
.to_csv('File.csv', index=False)) 

单独注释:

  1. 您正在初始化一个只有列的 DF,然后将其输出到 CSV。
  2. 为什么要使用 .squeeze 将其转换为一维数据集?