Using pandas to read table row data to judge whether it is the same

Time:2021-10-23

Description:

At the end of the afternoon, my colleagues from the company’s supply chain department came over and asked me if I could help him solve some things that excel needs to be done manually every week. Excel is the most commonly used office tool for data processing and should be proficient in market and operation. Ha ha, but programmers don’t use excel very much. Now let’s introduce pandas,   Pandas is a powerful tool set for analyzing structured data; Its use is based on numpy (providing high-performance matrix operation); It is used for data mining and data analysis, and also provides data cleaning function.

Specific requirements:

Find the same number and combine the English letters corresponding to the number.

Expected final generated value:

TAOCGQ    I 1

TAOCGQ   WY 2

TAOCGQ   BHLPQGRN  3

TAOCGQ    VUE 4

TAOCGQ     Z 5

Using pandas to read table row data to judge whether it is the same

Code implementation:

?
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
import pandas as pd
df = pd.read_excel(r'e: \ code \ tables_ Results \ pricelist. Xlsx ')
pd_result = df.to_dict(orient='split')
'' class information list ['route', 'W', 'I', 'y', 'B', 'H', 'l', 'p', 'Q', 'g', 'V', 'U', 'Z', 'R', 'n', 'e', 'k'] '' '
cabin_list = pd_result['columns']
'' 'route information and price list [[' lhw-zuh ', 2.0, 0.0, 2.0, 2.0, 2.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 2.0, 2.0, 2.0, 2.0, 0]' ''
data_lists = pd_result['data']
for data in data_lists:
 for number in range(0,20):
  '''index ID of circular route information retrieval list' ''
  idxs = [i for i, x in enumerate(data) if x == number]
  if idxs: #Index list
   news_data_list = []
   for idx in idxs: #Circular index list
    news_data_list.append(cabin_list[idx])
   cabin_merge = ''.join(news_data_list) #Merge multiple spaces
   aviation_rebate = idxs[0] #Take out the rebate price, because there are multiple indexes, just take the first one
   air_route = str(data[0]).split('-') #Route lhw-zuh
   dpt = air_route[0] #Departure city
   arr = air_route[1] #Reach the city
   result = dpt + arr + ',' + cabin_merge + ',' + str(int(data[aviation_rebate]))
   '''
   Final result:
   ['LHWZUH', 'IK', '0']
   ['LHWZUH', 'WYBHRNE', '2']
   ['LHWZUH', 'LPQGVUZ', '3']
   ['ZUHLHW', 'IK', '0']
   '''
   print(result.split(','))

Program execution results:

[‘TAOCGQ’, ‘I’, ‘1’]
[‘TAOCGQ’, ‘WY’, ‘2’]
[‘TAOCGQ’, ‘BHLPQGRN’, ‘3’]
[‘TAOCGQ’, ‘VUE’, ‘4’]
[‘TAOCGQ’, ‘Z’, ‘5’]

summary

This is the end of this article about using pandas to read table row data to judge whether it is the same. For more information about reading table row data by pandas, please search for previous articles on developeppaper or continue to browse the relevant articles below. I hope you will support developeppaper in the future!

Recommended Today

Git usage specification

​ 1. Basic operation of GIT -Git init if a project needs to be managed using git, it needs to be initialized -Git status: check the status of the current code (red: in the Development Zone, green: in the temporary storage zone, nothing to commit: there is no change in the Development Zone) -Git checkout […]