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 use python pandas Library to read specified Row or column data in excel/csv

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the knowledge of "how to use python pandas library to read specified row or column data in excel/csv". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Introduction

Key! Use the loc function to find.

Without saying much, demonstrate directly:

There are the following tables called try.xlsx:

1. Query according to index

Condition: the data imported first must have index.

Or add it yourself. The method is simple. Add index_col directly when reading the excel file.

Code example:

Import pandas as pd # Import pandas library excel_file ='. / try.xlsx' # Import excel data data = pd.read_excel (excel_file, index_col=' name') # the index_col of this is index, and you can select any field as the index index and read in the data print (data.loc ['Li Si'])

The printed result is

Department B

Salary 6600

Name: Li Si, dtype: object

(note: index)

two。 The known data finds the desired data in which line.

If we have empty salary data for an employee in our table, how can we find the data we want?

The code is as follows:

For i in data.columns: for j in range (len (data)): if (data [I]. Isnull ()) [j]: bumen = data.iloc [j, [0]] # find out the department where the missing value is located data [I] [j] = charuzhi (bumen)

The principle is simple: first retrieve all the data, and then we can use the iloc function in pandas. In iloc [j, [2]] above, j is the specific location, and [0] is the column where the data you want to get is located.

3. Find the specified row data according to the conditional query

For example, look up the names and salaries of all members of Department An or those whose wages are less than 3000:

The code is as follows:

"" query a row of data according to criteria "" import pandas as pd # Import pandas library excel_file ='. / try.xlsx' # Import file data = pd.read_excel (excel_file) # read data print (data.loc [data ['department'] ='A', ['name', 'salary]]) # Department is A Print name and salary print (data.loc [data ['salary']

< 3000, ['姓名','工资']]) #查找工资小于3000的人 结果如下:

To generate an excel file or csv file from this data independently:

Add the following code

"" Export to excel or csv file "" # single condition dataframe_1 = data.loc [data ['department'] = 'Aids, [' name', 'salary']] # single condition dataframe_2 = data.loc [data ['salary]]

< 3000, ['姓名', '工资']]#多条件dataframe_3 = data.loc[(data['部门'] == 'A')&(data['工资'] < 3000), ['姓名', '工资']]#导出为exceldataframe_1.to_excel('dataframe_1.xlsx')dataframe_2.to_excel('dataframe_2.xlsx')4.找出指定列data['columns'] #columns即你需要的字段名称即可#注意这列的columns不能是index的名称#如果要打印index的话就data.indexdata.columns #与上面的一样 以上全过程用到的库: pandas,xlrd , openpyxl 5.找出指定的行和指定的列 主要使用的就是函数iloc data.iloc[:,:2] #即全部行,前两列的数据 逗号前是行,逗号后是列的范围,很容易理解 6.在规定范围内找出符合条件的数据data.iloc[:10,:][data.工资>

6000]

In this way, we can find out the information of everyone whose salary is more than 6000 in the first 11 lines.

This is the end of the content of "how to use python pandas library to read specified row or column data in excel/csv". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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