Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to delete a column using Python's Pandas

2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how to delete columns with Python's Pandas". In daily operation, I believe many people have doubts about how to delete columns with Python's Pandas. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the questions of "how to delete columns with Python's Pandas". Next, please follow the editor to study!

Delete columns in DataFrame using the del and drop methods, and delete multiple columns at a time using the drop method

Data preparation:

Import pandas as pd data = pd.read_excel (r 'sales data .xlsx') print (data)

The data are as follows:

Date sales note number of gross margin customers

0 2022-03-14 Zhang San 87000 two transfers 8000 Beijing Winnie 1

1 2022-03-15 Li Si 100000 22000 Beijing Weige 1

2 2022-03-16 Wang Wei 6800 accounts 1000 Beijing Haitao 1

3 2022-03-17 Wang Ping 70000 in cash 5000 Beijing Longgong 1

4 2022-03-18 Daming 70000 in cash 5000 Beijing Longgong 1

With del, you can delete only one column at a time, not more than one column at a time:

Import pandas as pd data = pd.read_excel (r 'sales data .xlsx') print (data) # using del, you can only delete one column at a time, not more than one column at a time # you can only use del df ['remarks], not del df [[' remarks', 'customer']] del data ['remarks'] print (data)

Results:

D:\ Python310\ python.exe C:/Users/Administrator/Desktop/ test code .py

Date sales note number of gross margin customers

0 2022-03-14 Zhang San 87000 two transfers 8000 Beijing Winnie 1

1 2022-03-15 Li Si 100000 22000 Beijing Weige 1

2 2022-03-16 Wang Wei 6800 accounts 1000 Beijing Haitao 1

3 2022-03-17 Wang Ping 70000 in cash 5000 Beijing Longgong 1

4 2022-03-18 Daming 70000 in cash 5000 Beijing Longgong 1

Date sales gross margin number of customers

0 2022-03-14 Zhang San 87000 8000 Beijing Winnie 1

1 2022-03-15 Li Si 100000 22000 Beijing Wei GE 1

2 2022-03-16 Wang Wei 6800 1000 Beijing Haitao 1

3 2022-03-17 Wang Ping 70000 5000 Beijing Longgong 1

4 2022-03-18 Daming 70000 5000 Beijing Longgong 1

The process has ended, exit code 0

Use drop:

Import pandas as pd data = pd.read_excel (r 'sales data .xlsx') print (data) # use drop#data = data.drop (['remarks', 'customer'], axis=1, inplace=False) # axis=1 to delete columns, ['remarks', 'customer'] the list of col to be deleted, you can delete multiple columns # inplace=True at a time, and delete print (data) directly from within

Results:

D:\ Python310\ python.exe C:/Users/Administrator/Desktop/ test code .py

Date sales note number of gross margin customers

0 2022-03-14 Zhang San 87000 two transfers 8000 Beijing Winnie 1

1 2022-03-15 Li Si 100000 22000 Beijing Weige 1

2 2022-03-16 Wang Wei 6800 accounts 1000 Beijing Haitao 1

3 2022-03-17 Wang Ping 70000 in cash 5000 Beijing Longgong 1

4 2022-03-18 Daming 70000 in cash 5000 Beijing Longgong 1

Date sales gross margin quantity

0 2022-03-14 Zhang San 87000 8000 1

1 2022-03-15 Li Si 100000 22000 1

2 2022-03-16 Wang Wei 6800 1000 1

3 2022-03-17 Wang Ping 70000 5000 1

4 2022-03-18 Daming 70000 5000 1

The process has ended, exit code 0

At this point, the study on "how to delete columns with Python's Pandas" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report