Explore all Insurance related open source software, libraries, packages, source code, cloud functions and APIs.

The insurance sector is made up of companies that offer risk management in the form of insurance contracts. Life insurance companies focus on legacy planning and replacing human capital value, health insurers cover medical costs, and property, casualty, or accident insurance is aimed at replacing the value of homes, cars, or valuables.

These software components cover functions across Actuarial, Agent, Broker, Claims, Enrollment, Renewal, Underwriting areas.

Popular New Releases in Insurance

Android-Image-Cropper

4.2.1

manage-azure-policy

bLUe_PYSIDE2

v5.1.0

neo-blockchain-toolkit

Neo N3 Support

dnn.azureadb2cprovider

DNN Azure AD B2C Provider v1.5.0

Popular Libraries in Insurance

Android-Image-Cropper

by CanHub doticonkotlindoticon

star image 506 doticonApache-2.0

Image Cropping Library for Android, optimised for Camera / Gallery.

STEAD

by smousavi05 doticonpythondoticon

star image 81 doticonCC-BY-4.0

STanford EArthquake Dataset (STEAD):A Global Data Set of Seismic Signals for AI

tornado-cli

by tornadocash doticonjavascriptdoticon

star image 74 doticon

A command line interface to make transactions in Tornado Cash protocol

enterprise-policy-generator

by cadeyrn doticonjavascriptdoticon

star image 50 doticonMPL-2.0

The Enterprise Policy Engine allows administrators to configure Firefox via a configuration file. The Enterprise Policy Generator helps to create the configuration file.

CPL

by INK-USC doticonpythondoticon

star image 48 doticonMIT

Collaborative Policy Learning for Open Knowledge Graph Reasoning (EMNLP 2019)

Deterministic-GAIL-PyTorch

by nikhilbarhate99 doticonpythondoticon

star image 47 doticonMIT

PyTorch implementation of Deterministic Generative Adversarial Imitation Learning (GAIL) for Off Policy learning

NexusMutual

by somish doticonjavascriptdoticon

star image 42 doticonGPL-3.0

Nexus Mutual uses the power of blockchain technology and Ethereum to allow people from all over the world to share insurance risk together without the need for an insurance company. Nexus Mutual is now launched on Ethereum Mainnet. Buy Smart Contract Cover at https://app.nexusmutual.io

apibusinesshub-api-recipes

by SAP doticonjavascriptdoticon

star image 42 doticonNOASSERTION

Accelerate integration projects using SAP Cloud Platform API Management with crowdsourced best practices, curated by experts, designed for developers.

MOREL

by vik-goel doticonpythondoticon

star image 41 doticonMIT

Trending New libraries in Insurance

Android-Image-Cropper

by CanHub doticonkotlindoticon

star image 506 doticonApache-2.0

Image Cropping Library for Android, optimised for Camera / Gallery.

tornado-cli

by tornadocash doticonjavascriptdoticon

star image 74 doticon

A command line interface to make transactions in Tornado Cash protocol

manage-azure-policy

by Azure doticontypescriptdoticon

star image 35 doticonMIT

GiHub Action to manage Azure policy from GitHub workflows

opa-express-middleware

by build-security doticonjavascriptdoticon

star image 35 doticonMIT

Node.JS Express middleware for working with the Open Policy Agent

Map-Elites-Evolutionary

by uber-research doticonpythondoticon

star image 19 doticonNOASSERTION

Map-Elites based on Evolution Strategies

opa-traefik-microservice-authz

by appsecco doticonjavascriptdoticon

star image 18 doticonMIT

Proof of concept implementation of a scenario using Open Policy Agent for microservices authorization in API Gateway (Traefik).

opa-spring-security

by Bisnode doticonjavadoticon

star image 13 doticonApache-2.0

Open Policy Agent for Spring Security

KinovaGrasping

by OSUrobotics doticonpythondoticon

star image 13 doticon

This contains the simulation of a kinova robot and the code for collecting data and training both a grasp classifier and a RL agent

Readinizer

by CompassSecurity doticoncsharpdoticon

star image 11 doticonMIT

Microsoft GPO Readiness Lateral Movement Detection Tool

Top Authors in Insurance

1

ForgeRock

2 Libraries

star icon29

2

Bisnode

2 Libraries

star icon21

3

DataVisor

2 Libraries

star icon0

4

Turall

1 Libraries

star icon14

5

qiaochen

1 Libraries

star icon7

6

tobiasemrich

1 Libraries

star icon9

7

kimhc6028

1 Libraries

star icon25

8

brutusin

1 Libraries

star icon10

9

SonRossy

1 Libraries

star icon4

10

IBM-Cloud

1 Libraries

star icon6

1

2 Libraries

star icon29

2

2 Libraries

star icon21

3

2 Libraries

star icon0

4

1 Libraries

star icon14

5

1 Libraries

star icon7

6

1 Libraries

star icon9

7

1 Libraries

star icon25

8

1 Libraries

star icon10

9

1 Libraries

star icon4

10

1 Libraries

star icon6

Trending Kits in Insurance

No Trending Kits are available at this moment for Insurance

Trending Discussions on Insurance

What is the best way to Null check input fields in the request object in Java?

How to match and merge XML in database?

Foreign key created in shadow and appending random 1 to column name - ASP:NET EF Core

How to collapse the ExpansionTile on button click? - Flutter

I have a dataset in which i have two columns with time in it but the dat

How do I resolve list index out of range error?

Write an Effective query for django for a FOR Loop?

Combining values in a tuple that contain both string and integers values and storing as a dataframe

Error when using case_when with mutate and functions: getting closest number to zero with NA

How to remove parent json element in python3 if child is object is empty

QUESTION

What is the best way to Null check input fields in the request object in Java?

Asked 2022-Apr-17 at 07:22

I have a Code Like this:

Insurance Dto:

1 private String accidentNo;
2 private String birthDate;
3 private String mobileNo;
4 private String ssn;
5
6 public InsuranceDto(String accidentNo, String birthDate, String mobileNo, String ssn) {
7    this.accidentNo = accidentNo;
8    this.birthDate = birthDate;
9    this.mobileNo = mobileNo;
10    this.ssn = ssn;
11 }
12
13 public InsuranceDto() {}
14
15 public String getAccidentNo() {
16    return accidentNo;
17 }
18
19 public void setAccidentNo(String accidentNo) {
20    this.accidentNo = accidentNo;
21 }
22
23 public String getBirthDate() {
24    return birthDate;
25 }
26
27 public void setBirthDate(String birthDate) {
28    this.birthDate = birthDate;
29 }
30
31 public String getMobileNo() {
32    return mobileNo;
33 }
34
35 public void setMobileNo(String mobileNo) {
36    this.mobileNo = mobileNo;
37 }
38
39 public String getSsn() {
40    return ssn;
41 }
42
43 public void setSsn(String ssn) {
44    this.ssn = ssn;
45 }
46

Main Method:

1 private String accidentNo;
2 private String birthDate;
3 private String mobileNo;
4 private String ssn;
5
6 public InsuranceDto(String accidentNo, String birthDate, String mobileNo, String ssn) {
7    this.accidentNo = accidentNo;
8    this.birthDate = birthDate;
9    this.mobileNo = mobileNo;
10    this.ssn = ssn;
11 }
12
13 public InsuranceDto() {}
14
15 public String getAccidentNo() {
16    return accidentNo;
17 }
18
19 public void setAccidentNo(String accidentNo) {
20    this.accidentNo = accidentNo;
21 }
22
23 public String getBirthDate() {
24    return birthDate;
25 }
26
27 public void setBirthDate(String birthDate) {
28    this.birthDate = birthDate;
29 }
30
31 public String getMobileNo() {
32    return mobileNo;
33 }
34
35 public void setMobileNo(String mobileNo) {
36    this.mobileNo = mobileNo;
37 }
38
39 public String getSsn() {
40    return ssn;
41 }
42
43 public void setSsn(String ssn) {
44    this.ssn = ssn;
45 }
46 InsuranceDto insuranceDto = new InsuranceDto();
47
48 if (insuranceDto.getAccidentNo() == null)
49     throw new RuntimeException("Enter AccidentNo.");
50 if (insuranceDto.getBirthDate() == null)
51     throw new RuntimeException("Enter BirthDate.");
52 if (insuranceDto.getMobileNo() == null)
53     throw new RuntimeException("Enter MobileNo");
54 if (insuranceDto.getSsn() == null)
55     throw new RuntimeException("Enter SSN");
56

So the Code Works but its Kind of Garbage and if I Add 3 Other Fields to InsuranceDTO, I Have to Edit the Entire Code to Validate them. Imagen if the Validation Policy Gets more Complicated and you Need to Validate two and/or more Fields in one If statement At Once.

So How Can I avoid this? Is There a Design Pattern or Something else to Make it Better? Thanks in advance.

ANSWER

Answered 2022-Apr-17 at 06:51

Since you are trying to reduce the amount of code, I can suggest you to Use Lombok @Data annotation to get intrinsic getters and setters as well, this way you can avoid the whole getter setter methods which can reduce a lot of code. The way your InsuranceDto looks after you implement lombok is:

1 private String accidentNo;
2 private String birthDate;
3 private String mobileNo;
4 private String ssn;
5
6 public InsuranceDto(String accidentNo, String birthDate, String mobileNo, String ssn) {
7    this.accidentNo = accidentNo;
8    this.birthDate = birthDate;
9    this.mobileNo = mobileNo;
10    this.ssn = ssn;
11 }
12
13 public InsuranceDto() {}
14
15 public String getAccidentNo() {
16    return accidentNo;
17 }
18
19 public void setAccidentNo(String accidentNo) {
20    this.accidentNo = accidentNo;
21 }
22
23 public String getBirthDate() {
24    return birthDate;
25 }
26
27 public void setBirthDate(String birthDate) {
28    this.birthDate = birthDate;
29 }
30
31 public String getMobileNo() {
32    return mobileNo;
33 }
34
35 public void setMobileNo(String mobileNo) {
36    this.mobileNo = mobileNo;
37 }
38
39 public String getSsn() {
40    return ssn;
41 }
42
43 public void setSsn(String ssn) {
44    this.ssn = ssn;
45 }
46 InsuranceDto insuranceDto = new InsuranceDto();
47
48 if (insuranceDto.getAccidentNo() == null)
49     throw new RuntimeException("Enter AccidentNo.");
50 if (insuranceDto.getBirthDate() == null)
51     throw new RuntimeException("Enter BirthDate.");
52 if (insuranceDto.getMobileNo() == null)
53     throw new RuntimeException("Enter MobileNo");
54 if (insuranceDto.getSsn() == null)
55     throw new RuntimeException("Enter SSN");
56@Data
57@NoArgsConstructor
58@AllArgsConstructor
59@Build
60public class InsuranceDto {
61    @NotNull(message ="Accident Number must not be empty null")
62    private String accidentNo;
63    @NotNull(message ="Sample field 2 must not be empty null")
64    private String samplefield2;
65}
66

Lombok repository in Maven:

1 private String accidentNo;
2 private String birthDate;
3 private String mobileNo;
4 private String ssn;
5
6 public InsuranceDto(String accidentNo, String birthDate, String mobileNo, String ssn) {
7    this.accidentNo = accidentNo;
8    this.birthDate = birthDate;
9    this.mobileNo = mobileNo;
10    this.ssn = ssn;
11 }
12
13 public InsuranceDto() {}
14
15 public String getAccidentNo() {
16    return accidentNo;
17 }
18
19 public void setAccidentNo(String accidentNo) {
20    this.accidentNo = accidentNo;
21 }
22
23 public String getBirthDate() {
24    return birthDate;
25 }
26
27 public void setBirthDate(String birthDate) {
28    this.birthDate = birthDate;
29 }
30
31 public String getMobileNo() {
32    return mobileNo;
33 }
34
35 public void setMobileNo(String mobileNo) {
36    this.mobileNo = mobileNo;
37 }
38
39 public String getSsn() {
40    return ssn;
41 }
42
43 public void setSsn(String ssn) {
44    this.ssn = ssn;
45 }
46 InsuranceDto insuranceDto = new InsuranceDto();
47
48 if (insuranceDto.getAccidentNo() == null)
49     throw new RuntimeException("Enter AccidentNo.");
50 if (insuranceDto.getBirthDate() == null)
51     throw new RuntimeException("Enter BirthDate.");
52 if (insuranceDto.getMobileNo() == null)
53     throw new RuntimeException("Enter MobileNo");
54 if (insuranceDto.getSsn() == null)
55     throw new RuntimeException("Enter SSN");
56@Data
57@NoArgsConstructor
58@AllArgsConstructor
59@Build
60public class InsuranceDto {
61    @NotNull(message ="Accident Number must not be empty null")
62    private String accidentNo;
63    @NotNull(message ="Sample field 2 must not be empty null")
64    private String samplefield2;
65}
66<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
67<dependency>
68    <groupId>org.projectlombok</groupId>
69    <artifactId>lombok</artifactId>
70    <version>1.18.22</version>
71    <scope>provided</scope>
72</dependency>
73

And to your question, In InsuranceDto, you can attach the @Data once you have the lombok, for example above the field accidentNo in the InsuranceDto, you can have

1 private String accidentNo;
2 private String birthDate;
3 private String mobileNo;
4 private String ssn;
5
6 public InsuranceDto(String accidentNo, String birthDate, String mobileNo, String ssn) {
7    this.accidentNo = accidentNo;
8    this.birthDate = birthDate;
9    this.mobileNo = mobileNo;
10    this.ssn = ssn;
11 }
12
13 public InsuranceDto() {}
14
15 public String getAccidentNo() {
16    return accidentNo;
17 }
18
19 public void setAccidentNo(String accidentNo) {
20    this.accidentNo = accidentNo;
21 }
22
23 public String getBirthDate() {
24    return birthDate;
25 }
26
27 public void setBirthDate(String birthDate) {
28    this.birthDate = birthDate;
29 }
30
31 public String getMobileNo() {
32    return mobileNo;
33 }
34
35 public void setMobileNo(String mobileNo) {
36    this.mobileNo = mobileNo;
37 }
38
39 public String getSsn() {
40    return ssn;
41 }
42
43 public void setSsn(String ssn) {
44    this.ssn = ssn;
45 }
46 InsuranceDto insuranceDto = new InsuranceDto();
47
48 if (insuranceDto.getAccidentNo() == null)
49     throw new RuntimeException("Enter AccidentNo.");
50 if (insuranceDto.getBirthDate() == null)
51     throw new RuntimeException("Enter BirthDate.");
52 if (insuranceDto.getMobileNo() == null)
53     throw new RuntimeException("Enter MobileNo");
54 if (insuranceDto.getSsn() == null)
55     throw new RuntimeException("Enter SSN");
56@Data
57@NoArgsConstructor
58@AllArgsConstructor
59@Build
60public class InsuranceDto {
61    @NotNull(message ="Accident Number must not be empty null")
62    private String accidentNo;
63    @NotNull(message ="Sample field 2 must not be empty null")
64    private String samplefield2;
65}
66<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
67<dependency>
68    <groupId>org.projectlombok</groupId>
69    <artifactId>lombok</artifactId>
70    <version>1.18.22</version>
71    <scope>provided</scope>
72</dependency>
73@NotNull(message ="Accident Number must not be empty null")
74private String accidentNo;
75

This way you can completely avoid the null checks within your service class because it will throw a runtime exception when such a null values comes in even before hitting your service. The validation is automatic.

Edit: If you're only trying to avoid null checks you can use Apache commons.

1 private String accidentNo;
2 private String birthDate;
3 private String mobileNo;
4 private String ssn;
5
6 public InsuranceDto(String accidentNo, String birthDate, String mobileNo, String ssn) {
7    this.accidentNo = accidentNo;
8    this.birthDate = birthDate;
9    this.mobileNo = mobileNo;
10    this.ssn = ssn;
11 }
12
13 public InsuranceDto() {}
14
15 public String getAccidentNo() {
16    return accidentNo;
17 }
18
19 public void setAccidentNo(String accidentNo) {
20    this.accidentNo = accidentNo;
21 }
22
23 public String getBirthDate() {
24    return birthDate;
25 }
26
27 public void setBirthDate(String birthDate) {
28    this.birthDate = birthDate;
29 }
30
31 public String getMobileNo() {
32    return mobileNo;
33 }
34
35 public void setMobileNo(String mobileNo) {
36    this.mobileNo = mobileNo;
37 }
38
39 public String getSsn() {
40    return ssn;
41 }
42
43 public void setSsn(String ssn) {
44    this.ssn = ssn;
45 }
46 InsuranceDto insuranceDto = new InsuranceDto();
47
48 if (insuranceDto.getAccidentNo() == null)
49     throw new RuntimeException("Enter AccidentNo.");
50 if (insuranceDto.getBirthDate() == null)
51     throw new RuntimeException("Enter BirthDate.");
52 if (insuranceDto.getMobileNo() == null)
53     throw new RuntimeException("Enter MobileNo");
54 if (insuranceDto.getSsn() == null)
55     throw new RuntimeException("Enter SSN");
56@Data
57@NoArgsConstructor
58@AllArgsConstructor
59@Build
60public class InsuranceDto {
61    @NotNull(message ="Accident Number must not be empty null")
62    private String accidentNo;
63    @NotNull(message ="Sample field 2 must not be empty null")
64    private String samplefield2;
65}
66<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
67<dependency>
68    <groupId>org.projectlombok</groupId>
69    <artifactId>lombok</artifactId>
70    <version>1.18.22</version>
71    <scope>provided</scope>
72</dependency>
73@NotNull(message ="Accident Number must not be empty null")
74private String accidentNo;
75 org.apache.commons.lang3.Validate
76

Usage: If the accidentNo is null, an error is thrown with the string you give as a second argument.

1 private String accidentNo;
2 private String birthDate;
3 private String mobileNo;
4 private String ssn;
5
6 public InsuranceDto(String accidentNo, String birthDate, String mobileNo, String ssn) {
7    this.accidentNo = accidentNo;
8    this.birthDate = birthDate;
9    this.mobileNo = mobileNo;
10    this.ssn = ssn;
11 }
12
13 public InsuranceDto() {}
14
15 public String getAccidentNo() {
16    return accidentNo;
17 }
18
19 public void setAccidentNo(String accidentNo) {
20    this.accidentNo = accidentNo;
21 }
22
23 public String getBirthDate() {
24    return birthDate;
25 }
26
27 public void setBirthDate(String birthDate) {
28    this.birthDate = birthDate;
29 }
30
31 public String getMobileNo() {
32    return mobileNo;
33 }
34
35 public void setMobileNo(String mobileNo) {
36    this.mobileNo = mobileNo;
37 }
38
39 public String getSsn() {
40    return ssn;
41 }
42
43 public void setSsn(String ssn) {
44    this.ssn = ssn;
45 }
46 InsuranceDto insuranceDto = new InsuranceDto();
47
48 if (insuranceDto.getAccidentNo() == null)
49     throw new RuntimeException("Enter AccidentNo.");
50 if (insuranceDto.getBirthDate() == null)
51     throw new RuntimeException("Enter BirthDate.");
52 if (insuranceDto.getMobileNo() == null)
53     throw new RuntimeException("Enter MobileNo");
54 if (insuranceDto.getSsn() == null)
55     throw new RuntimeException("Enter SSN");
56@Data
57@NoArgsConstructor
58@AllArgsConstructor
59@Build
60public class InsuranceDto {
61    @NotNull(message ="Accident Number must not be empty null")
62    private String accidentNo;
63    @NotNull(message ="Sample field 2 must not be empty null")
64    private String samplefield2;
65}
66<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
67<dependency>
68    <groupId>org.projectlombok</groupId>
69    <artifactId>lombok</artifactId>
70    <version>1.18.22</version>
71    <scope>provided</scope>
72</dependency>
73@NotNull(message ="Accident Number must not be empty null")
74private String accidentNo;
75 org.apache.commons.lang3.Validate
76Validate.notNull(accidentNo, "accidentNo cannot be null")
77

Source https://stackoverflow.com/questions/71899535

QUESTION

How to match and merge XML in database?

Asked 2022-Feb-04 at 16:06

I have below mixed documents in one collection.

1B3:
2    <creditRisk>
3      <characteristic>
4        <score>
5            <LID>C230</LID>
6            <SPID>129587</SPID>
7            <Sector>Finance and Insurance</Sector>
8        </score>
9        <score>
10            <LID>C177</LID>
11            <SPID>360720</SPID>
12            <Sector>Mining and Oil and Gas Extraction</Sector>
13        </score>
14      </characteristic>
15    </creditRisk>
16 
17B4:
18    <creditRisk>
19      <pit>
20        <score>
21            <LID>C230</LID>           
22            <SPID>129587</SPID>
23            <LTV>1.4689503</LTV>
24            <LGD>0.5995806998</LGD>
25            <Logarithm>-0.915243031</Logarithm>
26        </score>
27        <score>
28            <LID>C177</LID>            
29            <SPID>360720</SPID>
30            <LTV>1.524224737</LTV>
31            <LGD>0.8989534312</LGD>
32            <Logarithm>-2.292173791</Logarithm>
33        </score>
34      </pit>
35</creditRisk>
36

At the moment to simplify the problem, I need to merge pit/score@B4 when its SPID equals to characteristic/score/SPID@B3 inside MarkLogic. 

Expected Output:

1B3:
2    <creditRisk>
3      <characteristic>
4        <score>
5            <LID>C230</LID>
6            <SPID>129587</SPID>
7            <Sector>Finance and Insurance</Sector>
8        </score>
9        <score>
10            <LID>C177</LID>
11            <SPID>360720</SPID>
12            <Sector>Mining and Oil and Gas Extraction</Sector>
13        </score>
14      </characteristic>
15    </creditRisk>
16 
17B4:
18    <creditRisk>
19      <pit>
20        <score>
21            <LID>C230</LID>           
22            <SPID>129587</SPID>
23            <LTV>1.4689503</LTV>
24            <LGD>0.5995806998</LGD>
25            <Logarithm>-0.915243031</Logarithm>
26        </score>
27        <score>
28            <LID>C177</LID>            
29            <SPID>360720</SPID>
30            <LTV>1.524224737</LTV>
31            <LGD>0.8989534312</LGD>
32            <Logarithm>-2.292173791</Logarithm>
33        </score>
34      </pit>
35</creditRisk>
36<characteristic>
37   <score>
38      <default>
39         <LID>C230</LID>
40         <SPID>129587</SPID>
41         <LTV>1.4689503</LTV>
42         <LGD>0.5995806998</LGD>
43         <Logarithm>-0.915243031</Logarithm>
44      </default>
45      <LID>C230</LID>
46      <SPID>129587</SPID>
47      <Sector>Finance and Insurance</Sector>
48   </score>
49   <score>
50      <default>
51         <LID>C177</LID>
52         <SPID>360720</SPID>
53         <LTV>1.524224737</LTV>
54         <LGD>0.8989534312</LGD>
55         <Logarithm>-2.292173791</Logarithm>
56      </default>
57      <LID>C177</LID>
58      <SPID>360720</SPID>
59      <Sector>Mining and Oil and Gas Extraction</Sector>
60   </score>
61</characteristic>
62

We are facing issue. My xsl comes out all blank results.

1B3:
2    <creditRisk>
3      <characteristic>
4        <score>
5            <LID>C230</LID>
6            <SPID>129587</SPID>
7            <Sector>Finance and Insurance</Sector>
8        </score>
9        <score>
10            <LID>C177</LID>
11            <SPID>360720</SPID>
12            <Sector>Mining and Oil and Gas Extraction</Sector>
13        </score>
14      </characteristic>
15    </creditRisk>
16 
17B4:
18    <creditRisk>
19      <pit>
20        <score>
21            <LID>C230</LID>           
22            <SPID>129587</SPID>
23            <LTV>1.4689503</LTV>
24            <LGD>0.5995806998</LGD>
25            <Logarithm>-0.915243031</Logarithm>
26        </score>
27        <score>
28            <LID>C177</LID>            
29            <SPID>360720</SPID>
30            <LTV>1.524224737</LTV>
31            <LGD>0.8989534312</LGD>
32            <Logarithm>-2.292173791</Logarithm>
33        </score>
34      </pit>
35</creditRisk>
36<characteristic>
37   <score>
38      <default>
39         <LID>C230</LID>
40         <SPID>129587</SPID>
41         <LTV>1.4689503</LTV>
42         <LGD>0.5995806998</LGD>
43         <Logarithm>-0.915243031</Logarithm>
44      </default>
45      <LID>C230</LID>
46      <SPID>129587</SPID>
47      <Sector>Finance and Insurance</Sector>
48   </score>
49   <score>
50      <default>
51         <LID>C177</LID>
52         <SPID>360720</SPID>
53         <LTV>1.524224737</LTV>
54         <LGD>0.8989534312</LGD>
55         <Logarithm>-2.292173791</Logarithm>
56      </default>
57      <LID>C177</LID>
58      <SPID>360720</SPID>
59      <Sector>Mining and Oil and Gas Extraction</Sector>
60   </score>
61</characteristic>
62    <xsl:template match="characteristic">   
63        <characteristic>
64        <xsl:call-template name="scoreSPID">
65          <xsl:with-param name="characterScore" select="score"/>
66        </xsl:call-template>
67        </characteristic>
68    </xsl:template>
69 
70    <xsl:template name="scoreSPID">
71        <xsl:param name="characterScore"/>
72        <xsl:for-each select="$characterScore">
73        <xsl:variable name="spid" select="SPID"/>
74            <score>
75              <xsl:for-each select="/creditRisk/pit/score[SPID eq $spid]">
76              <default>
77                <xsl:copy-of select="./node()"/>
78              </default>
79              <xsl:copy-of select="node()"/>
80              </xsl:for-each>
81            </score>     
82        </xsl:for-each>
83    </xsl:template>  
84   
85    <xsl:template match="node()">
86      <xsl:apply-templates/>
87</xsl:template>
88

How can I get the match/merge work in my xsl? Do note B3 and B4 are different dokuments in the same database.

ANSWER

Answered 2022-Feb-01 at 04:20

In the predicate filter for the for-each:

1B3:
2    <creditRisk>
3      <characteristic>
4        <score>
5            <LID>C230</LID>
6            <SPID>129587</SPID>
7            <Sector>Finance and Insurance</Sector>
8        </score>
9        <score>
10            <LID>C177</LID>
11            <SPID>360720</SPID>
12            <Sector>Mining and Oil and Gas Extraction</Sector>
13        </score>
14      </characteristic>
15    </creditRisk>
16 
17B4:
18    <creditRisk>
19      <pit>
20        <score>
21            <LID>C230</LID>           
22            <SPID>129587</SPID>
23            <LTV>1.4689503</LTV>
24            <LGD>0.5995806998</LGD>
25            <Logarithm>-0.915243031</Logarithm>
26        </score>
27        <score>
28            <LID>C177</LID>            
29            <SPID>360720</SPID>
30            <LTV>1.524224737</LTV>
31            <LGD>0.8989534312</LGD>
32            <Logarithm>-2.292173791</Logarithm>
33        </score>
34      </pit>
35</creditRisk>
36<characteristic>
37   <score>
38      <default>
39         <LID>C230</LID>
40         <SPID>129587</SPID>
41         <LTV>1.4689503</LTV>
42         <LGD>0.5995806998</LGD>
43         <Logarithm>-0.915243031</Logarithm>
44      </default>
45      <LID>C230</LID>
46      <SPID>129587</SPID>
47      <Sector>Finance and Insurance</Sector>
48   </score>
49   <score>
50      <default>
51         <LID>C177</LID>
52         <SPID>360720</SPID>
53         <LTV>1.524224737</LTV>
54         <LGD>0.8989534312</LGD>
55         <Logarithm>-2.292173791</Logarithm>
56      </default>
57      <LID>C177</LID>
58      <SPID>360720</SPID>
59      <Sector>Mining and Oil and Gas Extraction</Sector>
60   </score>
61</characteristic>
62    <xsl:template match="characteristic">   
63        <characteristic>
64        <xsl:call-template name="scoreSPID">
65          <xsl:with-param name="characterScore" select="score"/>
66        </xsl:call-template>
67        </characteristic>
68    </xsl:template>
69 
70    <xsl:template name="scoreSPID">
71        <xsl:param name="characterScore"/>
72        <xsl:for-each select="$characterScore">
73        <xsl:variable name="spid" select="SPID"/>
74            <score>
75              <xsl:for-each select="/creditRisk/pit/score[SPID eq $spid]">
76              <default>
77                <xsl:copy-of select="./node()"/>
78              </default>
79              <xsl:copy-of select="node()"/>
80              </xsl:for-each>
81            </score>     
82        </xsl:for-each>
83    </xsl:template>  
84   
85    <xsl:template match="node()">
86      <xsl:apply-templates/>
87</xsl:template>
88<xsl:for-each select="/creditRisk/pit/score[SPID eq spid]">
89

you want to filter where the SPID is equal to the variable $spid. Without the $ it is looking for a sibling element spid (which doesn't exist).

It should be:

1B3:
2    <creditRisk>
3      <characteristic>
4        <score>
5            <LID>C230</LID>
6            <SPID>129587</SPID>
7            <Sector>Finance and Insurance</Sector>
8        </score>
9        <score>
10            <LID>C177</LID>
11            <SPID>360720</SPID>
12            <Sector>Mining and Oil and Gas Extraction</Sector>
13        </score>
14      </characteristic>
15    </creditRisk>
16 
17B4:
18    <creditRisk>
19      <pit>
20        <score>
21            <LID>C230</LID>           
22            <SPID>129587</SPID>
23            <LTV>1.4689503</LTV>
24            <LGD>0.5995806998</LGD>
25            <Logarithm>-0.915243031</Logarithm>
26        </score>
27        <score>
28            <LID>C177</LID>            
29            <SPID>360720</SPID>
30            <LTV>1.524224737</LTV>
31            <LGD>0.8989534312</LGD>
32            <Logarithm>-2.292173791</Logarithm>
33        </score>
34      </pit>
35</creditRisk>
36<characteristic>
37   <score>
38      <default>
39         <LID>C230</LID>
40         <SPID>129587</SPID>
41         <LTV>1.4689503</LTV>
42         <LGD>0.5995806998</LGD>
43         <Logarithm>-0.915243031</Logarithm>
44      </default>
45      <LID>C230</LID>
46      <SPID>129587</SPID>
47      <Sector>Finance and Insurance</Sector>
48   </score>
49   <score>
50      <default>
51         <LID>C177</LID>
52         <SPID>360720</SPID>
53         <LTV>1.524224737</LTV>
54         <LGD>0.8989534312</LGD>
55         <Logarithm>-2.292173791</Logarithm>
56      </default>
57      <LID>C177</LID>
58      <SPID>360720</SPID>
59      <Sector>Mining and Oil and Gas Extraction</Sector>
60   </score>
61</characteristic>
62    <xsl:template match="characteristic">   
63        <characteristic>
64        <xsl:call-template name="scoreSPID">
65          <xsl:with-param name="characterScore" select="score"/>
66        </xsl:call-template>
67        </characteristic>
68    </xsl:template>
69 
70    <xsl:template name="scoreSPID">
71        <xsl:param name="characterScore"/>
72        <xsl:for-each select="$characterScore">
73        <xsl:variable name="spid" select="SPID"/>
74            <score>
75              <xsl:for-each select="/creditRisk/pit/score[SPID eq $spid]">
76              <default>
77                <xsl:copy-of select="./node()"/>
78              </default>
79              <xsl:copy-of select="node()"/>
80              </xsl:for-each>
81            </score>     
82        </xsl:for-each>
83    </xsl:template>  
84   
85    <xsl:template match="node()">
86      <xsl:apply-templates/>
87</xsl:template>
88<xsl:for-each select="/creditRisk/pit/score[SPID eq spid]">
89<xsl:for-each select="/creditRisk/pit/score[SPID eq $spid]">
90

Source https://stackoverflow.com/questions/70931597

QUESTION

Foreign key created in shadow and appending random 1 to column name - ASP:NET EF Core

Asked 2022-Jan-16 at 18:33

When I migrate my new models and data I get the following error for multiple foreign keys:

The foreign key property 'InsurancePolicy.InsuranceSubjectID1' was created in shadow state because a conflicting property with the simple name 'InsuranceSubjectID' exists in the entity type, but is either not mapped, is already used for another relationship, or is incompatible with the associated primary key type.

The weird thing is that I define my relationships the same throughout all models, but some work okay (FK without 1 is stored) and some don't.

Example of my models:

1public class InsurancePolicy
2{
3    public int InsurancePolicyID { get; set; }
4    [DataType(DataType.Currency)]
5    [Column(TypeName = "money")]
6    public decimal FinalSum { get; set; }
7    public DateTime DateFrom { get; set; }
8    public DateTime DateTo { get; set; }
9    public int? InsuredID { get; set; }
10    public Insured? Insured { get; set; }
11    public int? InsuranceSubjectID;
12    public InsuranceSubject? InsuranceSubject { get; set; }
13    public int? InsuranceSubtypeID;
14    public InsuranceSubtype? InsuranceSubtype { get; set; }
15}
16
17public class InsuranceSubject
18{
19    public int InsuranceSubjectID { get; set; }
20    [Required]
21    [StringLength(50)]
22    public string Title { get; set; }
23    public string Description { get; set; }
24    [DataType(DataType.Currency)]
25    [Column(TypeName = "money")]
26    public decimal EstimatedValue { get; set; }
27    public int? InsuredID;
28    public Insured? Insured;
29    public int? InsuranceSubjectTypeID { get; set; }
30    public InsuranceSubjectType? InsuranceSubjectType { get; set; }
31    public ICollection<InsurancePolicy>? InsurancePolicies { get; set; }
32}
33

I tried my code without the foreign key properties and only left the reference navigation property (deleted the int and left the object) and it worked okay, but I need FK ints for future implementation.

Here is GitHub repo link for more insight (branch model-extension): https://github.com/lenartgolob/Insurance-IS/tree/model-extension

ANSWER

Answered 2022-Jan-02 at 13:11

I added public virtual in front of my reference navigation properties and it solved the issue.

Source https://stackoverflow.com/questions/70306762

QUESTION

How to collapse the ExpansionTile on button click? - Flutter

Asked 2021-Dec-23 at 11:34

I'm opening up a form to enter the details and close it upon filling everything and submitting it, but only the trailing icon is getting changed, also I'm trying to set the changes in the onExpansionChanged but not reflecting in the UI.

Updated Code

1Scaffold(
2        backgroundColor: Colors.grey.shade200,
3        appBar: AppBar(
4          title: TextButton(
5            child: Text(count.toString()),
6            onPressed: () {
7              Navigator.push(context,
8                  MaterialPageRoute(builder: (_) => RegisterLetterBookedListScreen()));
9            },
10          ),
11          backgroundColor: ColorConstants.kPrimaryColor,
12          elevation: 0,
13        ),
14        floatingActionButton: FloatingActionButton(
15          onPressed: () {
16            MotionToast.success(
17              title: 'Amount to be Collected',
18              titleStyle: TextStyle(fontWeight: FontWeight.bold),
19              description: '\u{20B9} $amountToBeCollected',
20              descriptionStyle: TextStyle(fontSize: 20, fontWeight: FontWeight.w500),
21              layoutOrientation: ORIENTATION.LTR,
22              animationType: ANIMATION.FROM_LEFT,
23              width: 300,
24            ).show(context);
25          },
26          child: Text(
27            '\u{20B9} $amountToBeCollected',
28            style: TextStyle(fontSize: 20),
29          ),
30          shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
31        ),
32        body: Padding(
33          padding: const EdgeInsets.all(20.0),
34          child: SingleChildScrollView(
35            child: Column(
36              crossAxisAlignment: CrossAxisAlignment.start,
37              children: [
38                //Article Details
39                Column(
40                  crossAxisAlignment: CrossAxisAlignment.start,
41                  children: [
42                    
43
44                //Sender Details
45                ExpansionTile(
46                  key: GlobalKey(),
47                  trailing: (senderExpansion == false)
48                      ? Icon(
49                          MdiIcons.toggleSwitchOffOutline,
50                          size: 40,
51                          color: Colors.black54,
52                        )
53                      : Icon(MdiIcons.toggleSwitchOutline, size: 40, color: Colors.red),
54                  onExpansionChanged: (value) {
55                      senderExpansion = value;
56                  },
57                  maintainState: true,
58                  initiallyExpanded: senderExpansion,
59                  title: Text(
60                    'Sender Details',
61                    style: TextStyle(
62                        fontWeight: FontWeight.bold,
63                        color: senderHeadingColor,
64                        fontSize: 20,
65                        letterSpacing: 1),
66                  ),
67                  children: [
68                    Column(
69                      children: [
70                        CInputForm(
71                          readOnly: false,
72                          iconData: Icons.person,
73                          labelText: 'Name *',
74                          controller: senderNameController,
75                          textType: TextInputType.text,
76                          typeValue: 'Name',
77                          focusNode: senderNameFocusNode,
78                        ),
79                        CInputForm(
80                          readOnly: false,
81                          iconData: MdiIcons.home,
82                          labelText: 'Address *',
83                          controller: senderAddressController,
84                          textType: TextInputType.multiline,
85                          typeValue: 'Address',
86                          focusNode: senderAddressFocusNode,
87                        ),
88                        Card(
89                          elevation: 0,
90                          child: TypeAheadFormField(
91                            textFieldConfiguration: TextFieldConfiguration(
92                                style: TextStyle(color: Colors.blueGrey),
93                                controller: senderPinCodeCityController,
94                                autofocus: false,
95                                decoration: InputDecoration(
96                                    prefixIcon: Icon(
97                                      Icons.location_on_outlined,
98                                      color: Colors.blueGrey,
99                                    ),
100                                    fillColor: Colors.white,
101                                    filled: true,
102                                    enabledBorder: OutlineInputBorder(
103                                      borderSide: BorderSide(color: Colors.white),
104                                    ),
105                                    labelText: 'Pincode/Office Name *',
106                                    labelStyle: TextStyle(
107                                        color: ColorConstants.kAmberAccentColor),
108                                    border: OutlineInputBorder(
109                                        borderSide: BorderSide(color: Colors.white)))),
110                            onSuggestionSelected:
111                                (Map<String, String> suggestion) async {
112                              senderPinCodeCityController.text = suggestion['pinCode']!;
113                              senderCityController.text = suggestion['city']!;
114                              senderStateController.text = suggestion['state']!;
115                            },
116                            itemBuilder: (context, Map<String, String> suggestion) {
117                              return ListTile(
118                                title: Text(suggestion['officeName']! +
119                                    ", " +
120                                    suggestion['pinCode']!),
121                              );
122                            },
123                            suggestionsCallback: (pattern) async {
124                              return await FetchPin.getSuggestions(pattern);
125                            },
126                            validator: (value) {
127                              if (value!.isEmpty) {
128                                issPin = false;
129                                isLoading = false;
130                              }
131                            },
132                          ),
133                        ),
134                        Visibility(
135                          visible: issPin == false ? senderPinCodeCityController.text.isEmpty ? true : false : false,
136                          child: Align(
137                              alignment: Alignment.topLeft,
138                              child: Padding(
139                                padding: const EdgeInsets.only(left: 17.0),
140                                child: Text('Select a Pincode/Office name', style: TextStyle(fontSize: 10, color: ColorConstants.kPrimaryColor),),
141                              )),
142                        ),
143                        Visibility(
144                          visible:
145                              senderPinCodeCityController.text.isEmpty ? false : true,
146                          child: CInputForm(
147                            readOnly: true,
148                            iconData: Icons.location_city,
149                            labelText: 'City',
150                            controller: senderCityController,
151                            textType: TextInputType.text,
152                            typeValue: 'City',
153                            focusNode: senderPinCodeFocusNode,
154                          ),
155                        ),
156                        Visibility(
157                          visible:
158                          senderPinCodeCityController.text.isEmpty ? false : true,
159                          child: CInputForm(
160                            readOnly: true,
161                            iconData: Icons.location_city,
162                            labelText: 'State',
163                            controller: senderStateController,
164                            textType: TextInputType.text,
165                            typeValue: 'City',
166                            focusNode: senderPinCodeFocusNode,
167                          ),
168                        ),
169                        CInputForm(
170                          readOnly: false,
171                          iconData: MdiIcons.cellphone,
172                          labelText: 'Mobile Number',
173                          controller: senderMobileNumberController,
174                          textType: TextInputType.number,
175                          typeValue: 'MobileNumber',
176                          focusNode: senderMobileFocusNode,
177                        ),
178                        CInputForm(
179                          readOnly: false,
180                          iconData: MdiIcons.email,
181                          labelText: 'Email',
182                          controller: senderEmailController,
183                          textType: TextInputType.emailAddress,
184                          typeValue: 'Email',
185                          focusNode: senderEmailFocusNode,
186                        ),
187                      ],
188                    )
189                  ],
190                ),
191
192                //Addressee Details
193                ExpansionTile(
194                  trailing: (addresseeExpansion == false)
195                      ? Icon(
196                          MdiIcons.toggleSwitchOffOutline,
197                          size: 40,
198                          color: Colors.black54,
199                        )
200                      : Icon(MdiIcons.toggleSwitchOutline, size: 40, color: Colors.red),
201                  onExpansionChanged: (value) {
202                      addresseeExpansion = value;
203                  },
204                  maintainState: true,
205                  title: Text('Addressee Details',
206                      style: TextStyle(
207                          fontWeight: FontWeight.bold,
208                          color: addresseeHeadingColor,
209                          fontSize: 20,
210                          letterSpacing: 1)),
211                  children: [
212                    Column(
213                      children: [
214                        CInputForm(
215                          readOnly: false,
216                          iconData: Icons.person,
217                          labelText: 'Name *',
218                          controller: addresseeNameController,
219                          textType: TextInputType.text,
220                          typeValue: 'Name',
221                          focusNode: addresseeNameFocusNode,
222                        ),
223                        CInputForm(
224                          readOnly: false,
225                          iconData: MdiIcons.home,
226                          labelText: 'Address *',
227                          controller: addresseeAddressController,
228                          textType: TextInputType.multiline,
229                          typeValue: 'Address',
230                          focusNode: addresseeAddressFocusNode,
231                        ),
232                        Card(
233                          elevation: 0,
234                          child: TypeAheadFormField(
235                            textFieldConfiguration: TextFieldConfiguration(
236                                style: TextStyle(color: Colors.blueGrey),
237                                controller: addresseePinCodeCityController,
238                                autofocus: false,
239                                decoration: InputDecoration(
240                                    prefixIcon: Icon(
241                                      Icons.location_on_outlined,
242                                      color: Colors.blueGrey,
243                                    ),
244                                    fillColor: Colors.white,
245                                    filled: true,
246                                    enabledBorder: OutlineInputBorder(
247                                      borderSide: BorderSide(color: Colors.white),
248                                    ),
249                                    labelText: 'Pincode/Office Name *',
250                                    labelStyle: TextStyle(
251                                        color: ColorConstants.kAmberAccentColor),
252                                    border: OutlineInputBorder(
253                                        borderSide: BorderSide(color: Colors.white)))),
254                            onSuggestionSelected:
255                                (Map<String, String> suggestion) async {
256                              addresseePinCodeCityController.text =
257                                  suggestion['pinCode']!;
258                              addresseeCityController.text = suggestion['city']!;
259                              addresseeStateController.text = suggestion['state']!;
260                            },
261                            itemBuilder: (context, Map<String, String> suggestion) {
262                              return ListTile(
263                                title: Text(suggestion['officeName']! +
264                                    ", " +
265                                    suggestion['pinCode']!),
266                              );
267                            },
268                            suggestionsCallback: (pattern) async {
269                              return await FetchPin.getSuggestions(pattern);
270                            },
271                            validator: (value) {
272                              if (value!.isEmpty) {
273                                isLoading = false;
274                                isaPin = false;
275
276                              }
277                            },
278                          ),
279                        ),
280                        Visibility(
281                          visible: isaPin == false ? addresseePinCodeCityController.text.isEmpty ? true : false : false,
282                          child: Align(
283                              alignment: Alignment.topLeft,
284                              child: Padding(
285                                padding: const EdgeInsets.only(left: 17.0),
286                                child: Text('Select a Pincode/Office name', style: TextStyle(fontSize: 10, color: ColorConstants.kPrimaryColor),),
287                              )),
288                        ),
289                        Visibility(
290                          visible: addresseePinCodeCityController.text.isEmpty
291                              ? false
292                              : true,
293                          child: CInputForm(
294                            readOnly: true,
295                            iconData: Icons.location_city,
296                            labelText: 'City',
297                            controller: addresseeCityController,
298                            textType: TextInputType.text,
299                            typeValue: 'City',
300                            focusNode: addresseePinCodeFocusNode,
301                          ),
302                        ),
303                        Visibility(
304                          visible: addresseePinCodeCityController.text.isEmpty
305                              ? false
306                              : true,
307                          child: CInputForm(
308                            readOnly: true,
309                            iconData: Icons.location_city,
310                            labelText: 'State',
311                            controller: addresseeStateController,
312                            textType: TextInputType.text,
313                            typeValue: 'State',
314                            focusNode: addresseePinCodeFocusNode,
315                          ),
316                        ),
317                        CInputForm(
318                          readOnly: false,
319                          iconData: MdiIcons.cellphone,
320                          labelText: 'Mobile Number',
321                          controller: addresseeMobileNumberController,
322                          textType: TextInputType.number,
323                          typeValue: 'MobileNumber',
324                          focusNode: addresseeMobileFocusNode,
325                        ),
326                        CInputForm(
327                          readOnly: false,
328                          iconData: MdiIcons.email,
329                          labelText: 'Email',
330                          controller: addresseeEmailController,
331                          textType: TextInputType.emailAddress,
332                          typeValue: 'Email',
333                          focusNode: addresseeEmailFocusNode,
334                        ),
335                      ],
336                    ),
337                  ],
338                ),
339
340                //Submit
341                Padding(
342                  padding: const EdgeInsets.all(8.0),
343                  child: Center(
344                    child: ElevatedButton(
345                      style: ButtonStyle(
346                          backgroundColor:
347                              MaterialStateProperty.all<Color>(Colors.white),
348                          shape: MaterialStateProperty.all<RoundedRectangleBorder>(
349                              RoundedRectangleBorder(
350                                  borderRadius: BorderRadius.circular(10),
351                                  side: BorderSide(color: Colors.blueGrey)))),
352                      onPressed: () async {
353                        setState(() {
354                          senderExpansion = !senderExpansion;
355                          addresseeExpansion = !addresseeExpansion;
356                        });
357                        final ifPresent = await RegisterLetterTable()
358                            .select()
359                            .ArticleNumber
360                            .equals(articleNumberController.text)
361                            .toMapList();
362                        if (ifPresent.isNotEmpty) {
363                          ScaffoldMessenger.of(context).showSnackBar(SnackBar(
364                            content: Text('Article already scanned'),
365                            behavior: SnackBarBehavior.floating,
366                          ));
367                        } else {
368                          if (articleNumberController.text.isEmpty) {
369                            setState(() {
370                              articleNumberFocusNode.requestFocus();
371                            });
372                          } else if (weightController.text.isEmpty) {
373                            setState(() {
374                              weightFocusNode.requestFocus();
375                            });
376                          } else if (insuranceCheck == true) {
377                            if (insuranceController.text.isEmpty)
378                              insuranceFocusNode.requestFocus();
379                          } else if (valuePayableCheck == true) {
380                            if (valuePayablePostController.text.isEmpty)
381                              valuePayableFocusNode.requestFocus();
382                          }
383
384                          if (senderNameController.text.isEmpty ||
385                              senderAddressController.text.isEmpty ||
386                              senderPinCodeCityController.text.isEmpty) {
387                            setState(() {
388                              senderExpansion = true;
389                            });
390                            if (addresseeNameController.text.isEmpty ||
391                                addresseeAddressController.text.isEmpty ||
392                                addresseePinCodeCityController.text.isEmpty) {
393                              setState(() {
394                                addresseeExpansion = true;
395                              });
396                            } else
397                              setState(() {
398                                addresseeExpansion = false;
399                              });
400                          } else
401                            senderExpansion = false;
402                          if (addresseeNameController.text.isEmpty ||
403                              addresseeAddressController.text.isEmpty ||
404                              addresseePinCodeCityController.text.isEmpty) {
405                            setState(() {
406                              addresseeExpansion = true;
407                            });
408                          } else
409                            setState(() {
410                              addresseeExpansion = false;
411                            });
412
413                          if (formGlobalKey.currentState!.validate()) {
414                            formGlobalKey.currentState!.save();
415
416                            showDialog<void>(
417                                context: context,
418                                barrierDismissible: false,
419                                builder: (BuildContext context) {
420                                  return ConformationDialog(
421                                      articleNumber: articleNumberController.text,
422                                      weight: weightController.text,
423                                      prepaidAmount: prepaidAmountController.text,
424                                      acknowledgement: acknowledgementCheck,
425                                      insurance: insuranceCheck == true
426                                          ? insuranceController.text
427                                          : '',
428                                      valuePayablePost: valuePayableCheck == true
429                                          ? valuePayablePostController.text
430                                          : '',
431                                      amountToBeCollected:
432                                          amountToBeCollectedController.text,
433                                      senderName: senderNameController.text,
434                                      senderAddress: senderAddressController.text,
435                                      senderPinCode: senderPinCodeCityController.text,
436                                      senderCity: senderCityController.text,
437                                      senderState: senderStateController.text,
438                                      senderMobileNumber:
439                                          senderMobileNumberController.text,
440                                      senderEmail: senderEmailController.text,
441                                      addresseeName: addresseeNameController.text,
442                                      addresseeAddress: addresseeAddressController.text,
443                                      addresseePinCode:
444                                          addresseePinCodeCityController.text,
445                                      addresseeCity: addresseeCityController.text,
446                                      addresseeState: addresseeStateController.text,
447                                      addresseeMobileNumber:
448                                          addresseeMobileNumberController.text,
449                                      addresseeEmail: addresseeEmailController.text,
450                                      function: () {
451                                        Navigator.of(context).pop();
452                                        printFunction();
453                                      });
454                                });
455                          }
456                        }
457                      },
458                      child: Padding(
459                        padding: const EdgeInsets.all(8.0),
460                        child: new Text(
461                          'SUBMIT',
462                          overflow: TextOverflow.ellipsis,
463                          style: TextStyle(color: ColorConstants.kAmberAccentColor),
464                        ),
465                      ),
466                    ),
467                  ),
468                )
469              ],
470            ),
471          ),
472        ))
473

ANSWER

Answered 2021-Dec-21 at 12:01

checkout below code it may help you,

1Scaffold(
2        backgroundColor: Colors.grey.shade200,
3        appBar: AppBar(
4          title: TextButton(
5            child: Text(count.toString()),
6            onPressed: () {
7              Navigator.push(context,
8                  MaterialPageRoute(builder: (_) => RegisterLetterBookedListScreen()));
9            },
10          ),
11          backgroundColor: ColorConstants.kPrimaryColor,
12          elevation: 0,
13        ),
14        floatingActionButton: FloatingActionButton(
15          onPressed: () {
16            MotionToast.success(
17              title: 'Amount to be Collected',
18              titleStyle: TextStyle(fontWeight: FontWeight.bold),
19              description: '\u{20B9} $amountToBeCollected',
20              descriptionStyle: TextStyle(fontSize: 20, fontWeight: FontWeight.w500),
21              layoutOrientation: ORIENTATION.LTR,
22              animationType: ANIMATION.FROM_LEFT,
23              width: 300,
24            ).show(context);
25          },
26          child: Text(
27            '\u{20B9} $amountToBeCollected',
28            style: TextStyle(fontSize: 20),
29          ),
30          shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
31        ),
32        body: Padding(
33          padding: const EdgeInsets.all(20.0),
34          child: SingleChildScrollView(
35            child: Column(
36              crossAxisAlignment: CrossAxisAlignment.start,
37              children: [
38                //Article Details
39                Column(
40                  crossAxisAlignment: CrossAxisAlignment.start,
41                  children: [
42                    
43
44                //Sender Details
45                ExpansionTile(
46                  key: GlobalKey(),
47                  trailing: (senderExpansion == false)
48                      ? Icon(
49                          MdiIcons.toggleSwitchOffOutline,
50                          size: 40,
51                          color: Colors.black54,
52                        )
53                      : Icon(MdiIcons.toggleSwitchOutline, size: 40, color: Colors.red),
54                  onExpansionChanged: (value) {
55                      senderExpansion = value;
56                  },
57                  maintainState: true,
58                  initiallyExpanded: senderExpansion,
59                  title: Text(
60                    'Sender Details',
61                    style: TextStyle(
62                        fontWeight: FontWeight.bold,
63                        color: senderHeadingColor,
64                        fontSize: 20,
65                        letterSpacing: 1),
66                  ),
67                  children: [
68                    Column(
69                      children: [
70                        CInputForm(
71                          readOnly: false,
72                          iconData: Icons.person,
73                          labelText: 'Name *',
74                          controller: senderNameController,
75                          textType: TextInputType.text,
76                          typeValue: 'Name',
77                          focusNode: senderNameFocusNode,
78                        ),
79                        CInputForm(
80                          readOnly: false,
81                          iconData: MdiIcons.home,
82                          labelText: 'Address *',
83                          controller: senderAddressController,
84                          textType: TextInputType.multiline,
85                          typeValue: 'Address',
86                          focusNode: senderAddressFocusNode,
87                        ),
88                        Card(
89                          elevation: 0,
90                          child: TypeAheadFormField(
91                            textFieldConfiguration: TextFieldConfiguration(
92                                style: TextStyle(color: Colors.blueGrey),
93                                controller: senderPinCodeCityController,
94                                autofocus: false,
95                                decoration: InputDecoration(
96                                    prefixIcon: Icon(
97                                      Icons.location_on_outlined,
98                                      color: Colors.blueGrey,
99                                    ),
100                                    fillColor: Colors.white,
101                                    filled: true,
102                                    enabledBorder: OutlineInputBorder(
103                                      borderSide: BorderSide(color: Colors.white),
104                                    ),
105                                    labelText: 'Pincode/Office Name *',
106                                    labelStyle: TextStyle(
107                                        color: ColorConstants.kAmberAccentColor),
108                                    border: OutlineInputBorder(
109                                        borderSide: BorderSide(color: Colors.white)))),
110                            onSuggestionSelected:
111                                (Map<String, String> suggestion) async {
112                              senderPinCodeCityController.text = suggestion['pinCode']!;
113                              senderCityController.text = suggestion['city']!;
114                              senderStateController.text = suggestion['state']!;
115                            },
116                            itemBuilder: (context, Map<String, String> suggestion) {
117                              return ListTile(
118                                title: Text(suggestion['officeName']! +
119                                    ", " +
120                                    suggestion['pinCode']!),
121                              );
122                            },
123                            suggestionsCallback: (pattern) async {
124                              return await FetchPin.getSuggestions(pattern);
125                            },
126                            validator: (value) {
127                              if (value!.isEmpty) {
128                                issPin = false;
129                                isLoading = false;
130                              }
131                            },
132                          ),
133                        ),
134                        Visibility(
135                          visible: issPin == false ? senderPinCodeCityController.text.isEmpty ? true : false : false,
136                          child: Align(
137                              alignment: Alignment.topLeft,
138                              child: Padding(
139                                padding: const EdgeInsets.only(left: 17.0),
140                                child: Text('Select a Pincode/Office name', style: TextStyle(fontSize: 10, color: ColorConstants.kPrimaryColor),),
141                              )),
142                        ),
143                        Visibility(
144                          visible:
145                              senderPinCodeCityController.text.isEmpty ? false : true,
146                          child: CInputForm(
147                            readOnly: true,
148                            iconData: Icons.location_city,
149                            labelText: 'City',
150                            controller: senderCityController,
151                            textType: TextInputType.text,
152                            typeValue: 'City',
153                            focusNode: senderPinCodeFocusNode,
154                          ),
155                        ),
156                        Visibility(
157                          visible:
158                          senderPinCodeCityController.text.isEmpty ? false : true,
159                          child: CInputForm(
160                            readOnly: true,
161                            iconData: Icons.location_city,
162                            labelText: 'State',
163                            controller: senderStateController,
164                            textType: TextInputType.text,
165                            typeValue: 'City',
166                            focusNode: senderPinCodeFocusNode,
167                          ),
168                        ),
169                        CInputForm(
170                          readOnly: false,
171                          iconData: MdiIcons.cellphone,
172                          labelText: 'Mobile Number',
173                          controller: senderMobileNumberController,
174                          textType: TextInputType.number,
175                          typeValue: 'MobileNumber',
176                          focusNode: senderMobileFocusNode,
177                        ),
178                        CInputForm(
179                          readOnly: false,
180                          iconData: MdiIcons.email,
181                          labelText: 'Email',
182                          controller: senderEmailController,
183                          textType: TextInputType.emailAddress,
184                          typeValue: 'Email',
185                          focusNode: senderEmailFocusNode,
186                        ),
187                      ],
188                    )
189                  ],
190                ),
191
192                //Addressee Details
193                ExpansionTile(
194                  trailing: (addresseeExpansion == false)
195                      ? Icon(
196                          MdiIcons.toggleSwitchOffOutline,
197                          size: 40,
198                          color: Colors.black54,
199                        )
200                      : Icon(MdiIcons.toggleSwitchOutline, size: 40, color: Colors.red),
201                  onExpansionChanged: (value) {
202                      addresseeExpansion = value;
203                  },
204                  maintainState: true,
205                  title: Text('Addressee Details',
206                      style: TextStyle(
207                          fontWeight: FontWeight.bold,
208                          color: addresseeHeadingColor,
209                          fontSize: 20,
210                          letterSpacing: 1)),
211                  children: [
212                    Column(
213                      children: [
214                        CInputForm(
215                          readOnly: false,
216                          iconData: Icons.person,
217                          labelText: 'Name *',
218                          controller: addresseeNameController,
219                          textType: TextInputType.text,
220                          typeValue: 'Name',
221                          focusNode: addresseeNameFocusNode,
222                        ),
223                        CInputForm(
224                          readOnly: false,
225                          iconData: MdiIcons.home,
226                          labelText: 'Address *',
227                          controller: addresseeAddressController,
228                          textType: TextInputType.multiline,
229                          typeValue: 'Address',
230                          focusNode: addresseeAddressFocusNode,
231                        ),
232                        Card(
233                          elevation: 0,
234                          child: TypeAheadFormField(
235                            textFieldConfiguration: TextFieldConfiguration(
236                                style: TextStyle(color: Colors.blueGrey),
237                                controller: addresseePinCodeCityController,
238                                autofocus: false,
239                                decoration: InputDecoration(
240                                    prefixIcon: Icon(
241                                      Icons.location_on_outlined,
242                                      color: Colors.blueGrey,
243                                    ),
244                                    fillColor: Colors.white,
245                                    filled: true,
246                                    enabledBorder: OutlineInputBorder(
247                                      borderSide: BorderSide(color: Colors.white),
248                                    ),
249                                    labelText: 'Pincode/Office Name *',
250                                    labelStyle: TextStyle(
251                                        color: ColorConstants.kAmberAccentColor),
252                                    border: OutlineInputBorder(
253                                        borderSide: BorderSide(color: Colors.white)))),
254                            onSuggestionSelected:
255                                (Map<String, String> suggestion) async {
256                              addresseePinCodeCityController.text =
257                                  suggestion['pinCode']!;
258                              addresseeCityController.text = suggestion['city']!;
259                              addresseeStateController.text = suggestion['state']!;
260                            },
261                            itemBuilder: (context, Map<String, String> suggestion) {
262                              return ListTile(
263                                title: Text(suggestion['officeName']! +
264                                    ", " +
265                                    suggestion['pinCode']!),
266                              );
267                            },
268                            suggestionsCallback: (pattern) async {
269                              return await FetchPin.getSuggestions(pattern);
270                            },
271                            validator: (value) {
272                              if (value!.isEmpty) {
273                                isLoading = false;
274                                isaPin = false;
275
276                              }
277                            },
278                          ),
279                        ),
280                        Visibility(
281                          visible: isaPin == false ? addresseePinCodeCityController.text.isEmpty ? true : false : false,
282                          child: Align(
283                              alignment: Alignment.topLeft,
284                              child: Padding(
285                                padding: const EdgeInsets.only(left: 17.0),
286                                child: Text('Select a Pincode/Office name', style: TextStyle(fontSize: 10, color: ColorConstants.kPrimaryColor),),
287                              )),
288                        ),
289                        Visibility(
290                          visible: addresseePinCodeCityController.text.isEmpty
291                              ? false
292                              : true,
293                          child: CInputForm(
294                            readOnly: true,
295                            iconData: Icons.location_city,
296                            labelText: 'City',
297                            controller: addresseeCityController,
298                            textType: TextInputType.text,
299                            typeValue: 'City',
300                            focusNode: addresseePinCodeFocusNode,
301                          ),
302                        ),
303                        Visibility(
304                          visible: addresseePinCodeCityController.text.isEmpty
305                              ? false
306                              : true,
307                          child: CInputForm(
308                            readOnly: true,
309                            iconData: Icons.location_city,
310                            labelText: 'State',
311                            controller: addresseeStateController,
312                            textType: TextInputType.text,
313                            typeValue: 'State',
314                            focusNode: addresseePinCodeFocusNode,
315                          ),
316                        ),
317                        CInputForm(
318                          readOnly: false,
319                          iconData: MdiIcons.cellphone,
320                          labelText: 'Mobile Number',
321                          controller: addresseeMobileNumberController,
322                          textType: TextInputType.number,
323                          typeValue: 'MobileNumber',
324                          focusNode: addresseeMobileFocusNode,
325                        ),
326                        CInputForm(
327                          readOnly: false,
328                          iconData: MdiIcons.email,
329                          labelText: 'Email',
330                          controller: addresseeEmailController,
331                          textType: TextInputType.emailAddress,
332                          typeValue: 'Email',
333                          focusNode: addresseeEmailFocusNode,
334                        ),
335                      ],
336                    ),
337                  ],
338                ),
339
340                //Submit
341                Padding(
342                  padding: const EdgeInsets.all(8.0),
343                  child: Center(
344                    child: ElevatedButton(
345                      style: ButtonStyle(
346                          backgroundColor:
347                              MaterialStateProperty.all<Color>(Colors.white),
348                          shape: MaterialStateProperty.all<RoundedRectangleBorder>(
349                              RoundedRectangleBorder(
350                                  borderRadius: BorderRadius.circular(10),
351                                  side: BorderSide(color: Colors.blueGrey)))),
352                      onPressed: () async {
353                        setState(() {
354                          senderExpansion = !senderExpansion;
355                          addresseeExpansion = !addresseeExpansion;
356                        });
357                        final ifPresent = await RegisterLetterTable()
358                            .select()
359                            .ArticleNumber
360                            .equals(articleNumberController.text)
361                            .toMapList();
362                        if (ifPresent.isNotEmpty) {
363                          ScaffoldMessenger.of(context).showSnackBar(SnackBar(
364                            content: Text('Article already scanned'),
365                            behavior: SnackBarBehavior.floating,
366                          ));
367                        } else {
368                          if (articleNumberController.text.isEmpty) {
369                            setState(() {
370                              articleNumberFocusNode.requestFocus();
371                            });
372                          } else if (weightController.text.isEmpty) {
373                            setState(() {
374                              weightFocusNode.requestFocus();
375                            });
376                          } else if (insuranceCheck == true) {
377                            if (insuranceController.text.isEmpty)
378                              insuranceFocusNode.requestFocus();
379                          } else if (valuePayableCheck == true) {
380                            if (valuePayablePostController.text.isEmpty)
381                              valuePayableFocusNode.requestFocus();
382                          }
383
384                          if (senderNameController.text.isEmpty ||
385                              senderAddressController.text.isEmpty ||
386                              senderPinCodeCityController.text.isEmpty) {
387                            setState(() {
388                              senderExpansion = true;
389                            });
390                            if (addresseeNameController.text.isEmpty ||
391                                addresseeAddressController.text.isEmpty ||
392                                addresseePinCodeCityController.text.isEmpty) {
393                              setState(() {
394                                addresseeExpansion = true;
395                              });
396                            } else
397                              setState(() {
398                                addresseeExpansion = false;
399                              });
400                          } else
401                            senderExpansion = false;
402                          if (addresseeNameController.text.isEmpty ||
403                              addresseeAddressController.text.isEmpty ||
404                              addresseePinCodeCityController.text.isEmpty) {
405                            setState(() {
406                              addresseeExpansion = true;
407                            });
408                          } else
409                            setState(() {
410                              addresseeExpansion = false;
411                            });
412
413                          if (formGlobalKey.currentState!.validate()) {
414                            formGlobalKey.currentState!.save();
415
416                            showDialog<void>(
417                                context: context,
418                                barrierDismissible: false,
419                                builder: (BuildContext context) {
420                                  return ConformationDialog(
421                                      articleNumber: articleNumberController.text,
422                                      weight: weightController.text,
423                                      prepaidAmount: prepaidAmountController.text,
424                                      acknowledgement: acknowledgementCheck,
425                                      insurance: insuranceCheck == true
426                                          ? insuranceController.text
427                                          : '',
428                                      valuePayablePost: valuePayableCheck == true
429                                          ? valuePayablePostController.text
430                                          : '',
431                                      amountToBeCollected:
432                                          amountToBeCollectedController.text,
433                                      senderName: senderNameController.text,
434                                      senderAddress: senderAddressController.text,
435                                      senderPinCode: senderPinCodeCityController.text,
436                                      senderCity: senderCityController.text,
437                                      senderState: senderStateController.text,
438                                      senderMobileNumber:
439                                          senderMobileNumberController.text,
440                                      senderEmail: senderEmailController.text,
441                                      addresseeName: addresseeNameController.text,
442                                      addresseeAddress: addresseeAddressController.text,
443                                      addresseePinCode:
444                                          addresseePinCodeCityController.text,
445                                      addresseeCity: addresseeCityController.text,
446                                      addresseeState: addresseeStateController.text,
447                                      addresseeMobileNumber:
448                                          addresseeMobileNumberController.text,
449                                      addresseeEmail: addresseeEmailController.text,
450                                      function: () {
451                                        Navigator.of(context).pop();
452                                        printFunction();
453                                      });
454                                });
455                          }
456                        }
457                      },
458                      child: Padding(
459                        padding: const EdgeInsets.all(8.0),
460                        child: new Text(
461                          'SUBMIT',
462                          overflow: TextOverflow.ellipsis,
463                          style: TextStyle(color: ColorConstants.kAmberAccentColor),
464                        ),
465                      ),
466                    ),
467                  ),
468                )
469              ],
470            ),
471          ),
472        ))
473class TestExpanTile extends StatefulWidget {
474  @override
475  TestExpanTileState createState() => new TestExpanTileState();
476}
477
478class TestExpanTileState extends State<TestExpanTile> {
479
480  String senderDet = 'Sender Details ';
481  bool formOpenFlag = true;
482
483  @override
484  Widget build(BuildContext context) {
485    return new MaterialApp(
486      home: new Scaffold(
487        appBar: new AppBar(
488          title: const Text('ExpansionTile'),
489        ),
490        body: new ExpansionTile(
491          key: GlobalKey(),
492          initiallyExpanded: formOpenFlag,
493          onExpansionChanged: (val) {
494            formOpenFlag = val; // update here on change
495          },
496          title: new Text(this.senderDet),
497          backgroundColor: Theme.of(context).accentColor.withOpacity(0.025),
498          children: <Widget>[
499            new ListTile(
500              title: const Text('Your Filling Form goes here'),
501              onTap: () {},
502            ),
503            RaisedButton(
504              onPressed: () {
505                setState(() {
506                  formOpenFlag = !formOpenFlag;
507                });
508              },
509              child: Text("Close Tile On Submit"),
510            )
511          ],
512        ),
513      ),
514    );
515  }
516}
517
518

Source https://stackoverflow.com/questions/70434902

QUESTION

I have a dataset in which i have two columns with time in it but the dat

Asked 2021-Dec-22 at 17:00
1Unnamed: 0     Created Date             Closed Date            Agency   Agency Name  Complaint Type Descriptor  Location Type   Incident Zip    Address Type    City    Landmark    Status  Borough
22869    2869    10/30/2013 09:14:47 AM  10/30/2013 10:48:51 AM  NYPD    New York City Police Department Illegal Parking Double Parked Blocking Traffic  Street/Sidewalk 11217.0 PLACENAME   BROOKLYN    BARCLAYS CENTER Closed  BROOKLYN
323571   23571   10/25/2013 02:33:54 PM  10/25/2013 03:36:36 PM  NYPD    New York City Police Department Noise - Park    Loud Music/Party    Park/Playground 10000   PLACENAME   NEW YORK    CENTRAL PARK    Closed  MANHATTAN
441625   41625   10/22/2013 09:33:56 PM  10/24/2013 05:37:24 PM  TLC Taxi and Limousine Commission   For Hire Vehicle Complaint  Car Service Company Complaint   Street  11430   PLACENAME   JAMAICA J F K AIRPORT   Closed  QUEENS
544331   44331   10/22/2013 07:25:35 AM  10/25/2013 10:40:35 AM  TLC Taxi and Limousine Commission   Taxi Complaint  Driver Complaint    Street  11430   PLACENAME   JAMAICA J F K AIRPORT   Closed  QUEENS
646913   46913   10/21/2013 05:03:26 PM  10/23/2013 09:59:23 AM  DPR Department of Parks and Recreation  Dead Tree   Dead/Dying Tree Street  11215   PLACENAME   BROOKLYN    BARTEL PRITCHARD SQUARE Closed  BROOKLYN
747459   47459   10/21/2013 02:56:08 PM  10/29/2013 06:17:10 PM  TLC Taxi and Limousine Commission   Taxi Complaint  Driver Complaint    Street  10031   PLACENAME   NEW YORK    CITY COLLEGE    Closed  MANHATTAN
848465   48465   10/21/2013 10:44:10 AM  10/21/2013 11:17:47 AM  NYPD    New York City Police Department Illegal Parking Posted Parking Sign Violation   Street/Sidewalk 11434   PLACENAME   JAMAICA PS 37   Closed  QUEENS
951837   51837   10/20/2013 04:36:12 PM  10/20/2013 06:35:49 PM  NYPD    New York City Police Department Noise - Park    Loud Music/Party    Park/Playground 10031.0 PLACENAME   NEW YORK    JACKIE ROBINSON PARK    Closed  MANHATTAN
1051848   51848   10/20/2013 04:26:03 PM  10/20/2013 06:34:47 PM  NYPD    New York City Police Department Noise - Park    Loud Music/Party    Park/Playground 10031.0 PLACENAME   NEW YORK    JACKIE ROBINSON PARK    Closed  MANHATTAN
1154089   54089   10/19/2013 03:45:47 PM  10/19/2013 04:10:11 PM  NYPD    New York City Police Department Noise - Park    Loud Music/Party    Park/Playground 10000.0 PLACENAME   NEW YORK    CENTRAL PARK    Closed  MANHATTAN
1254343   54343   10/19/2013 01:27:43 PM  10/28/2013 08:42:12 AM  DOT Department of Transportation    Street Condition    Rough, Pitted or Cracked Roads  Street  10003.0 PLACENAME   NEW YORK    UNION SQUARE PARK   Closed  MANHATTAN
1355140   55140   10/19/2013 02:02:28 AM  10/19/2013 02:19:55 AM  NYPD    New York City Police Department Noise - Vehicle Car/Truck Music Street/Sidewalk 11368.0 PLACENAME   CORONA  WORLDS FAIR MARINA  Closed  QUEENS
1457789   57789   10/18/2013 11:55:44 AM  10/23/2013 02:42:14 PM  TLC Taxi and Limousine Commission   Taxi Complaint  Driver Complaint    Street  11369.0 PLACENAME   EAST ELMHURST   LA GUARDIA AIRPORT  Closed  QUEENS
1563119   63119   10/17/2013 06:52:37 AM  10/25/2013 06:49:59 PM  TLC Taxi and Limousine Commission   Taxi Complaint  Driver Complaint    Street  11430.0 PLACENAME   JAMAICA J F K AIRPORT   Closed  QUEENS
1666242   66242   10/16/2013 01:56:24 PM  10/22/2013 03:09:11 PM  TLC Taxi and Limousine Commission   Taxi Complaint  Driver Complaint    Street  11369   PLACENAME   EAST ELMHURST   LA GUARDIA AIRPORT  Closed  QUEENS
1766758   66758   10/16/2013 11:52:43 AM  10/16/2013 04:35:34 PM  NYPD    New York City Police Department Vending Unlicensed  Park/Playground 10036   PLACENAME   NEW YORK    BRYANT PARK Closed  MANHATTAN
1866786   66786   10/16/2013 11:42:23 AM  10/18/2013 04:57:04 PM  TLC Taxi and Limousine Commission   Taxi Complaint  Insurance Information Requested Street  10003   PLACENAME   NEW YORK    BETH ISRAEL MED CENTER  Closed  MANHATTAN
1966809   66809   10/16/2013 11:36:54 AM  10/16/2013 12:34:23 PM  NYPD    New York City Police Department Traffic Congestion/Gridlock Street/Sidewalk 11430   PLACENAME   JAMAICA J F K AIRPORT   Closed  QUEENS
2067465   67465   10/16/2013 09:14:35 AM  10/16/2013 12:43:06 PM  NYPD    New York City Police Department Traffic Drag Racing Street/Sidewalk 11367   PLACENAME   FLUSHING    QUEENS COLLEGE  Closed  QUEENS
2172424   72424   10/15/2013 12:22:00 AM  10/21/2013 12:16:15 PM  TLC Taxi and Limousine Commission   Taxi Complaint  Driver Complaint    Street  11217   PLACENAME   BROOKLYN    BARCLAYS CENTER Closed  BROOKLYN
2275531   75531   10/14/2013 10:59:20 AM  10/14/2013 03:09:51 PM  NYPD    New York City Police Department Vending In Prohibited Area  Park/Playground 10000   PLACENAME   NEW YORK    CENTRAL PARK    Closed  MANHATTAN
2377918   77918   10/13/2013 03:16:03 PM  10/13/2013 03:25:45 PM  NYPD    New York City Police Department Noise - Park    Loud Music/Party    Park/Playground 10000   PLACENAME   NEW YORK    CENTRAL PARK    Closed  MANHATTAN
2478048   78048   10/13/2013 01:06:02 PM  10/21/2013 10:20:21 AM  TLC Taxi and Limousine Commission   Taxi Complaint  Driver Complaint    Street  11369   PLACENAME   EAST ELMHURST   LA GUARDIA AIRPORT  Closed  QUEENS
2578352   78352   10/13/2013 05:14:33 AM  10/16/2013 01:42:42 PM  TLC Taxi and Limousine Commission   For Hire Vehicle Complaint  Car Service Company Complaint   Street  11217   PLACENAME   BROOKLYN    BARCLAYS CENTER Closed  BROOKLYN
2678383   78383   10/13/2013 03:50:02 AM  10/13/2013 05:03:13 AM  NYPD    New York City Police Department Noise - Vehicle Car/Truck Music Street/Sidewalk 11368   PLACENAME   CORONA  WORLDS FAIR MARINA  Closed  QUEENS
2779078   79078   10/12/2013 09:53:17 PM  10/13/2013 02:52:07 AM  NYPD    New York City Police Department Noise - Park    Loud Music/Party    Park/Playground 10011   PLACENAME   NEW YORK    WASHINGTON SQUARE PARK  Closed  MANHATTAN
2884489   84489   10/10/2013 07:16:16 PM  10/10/2013 10:29:16 PM  NYPD    New York City Police Department Noise - Park    Loud Music/Party    Park/Playground 11215   PLACENAME   BROOKLYN    PROSPECT PARK   Closed  BROOKLYN
2984518   84518   10/10/2013 07:02:29 PM  10/10/2013 10:29:16 PM  NYPD    New York City Police Department Noise - Park    Loud Music/Party    Park/Playground 11215   PLACENAME   BROOKLYN    PROSPECT PARK   Closed  BROOKLYN
3084688   84688   10/10/2013 05:39:19 PM  10/10/2013 10:29:17 PM  NYPD    New York City Police Department Noise - Park    Loud Music/Party    Park/Playground 11215   PLACENAME   BROOKLYN    PROSPECT PARK   Closed  BROOKLYN
3184695   84695   10/10/2013 05:37:04 PM  10/10/2013 10:30:19 PM  NYPD    New York City Police Department Noise - Park    Loud Music/Party    Park/Playground 11215   PLACENAME   BROOKLYN    PROSPECT PARK   Closed  BROOKLYN
3288812   88812   10/09/2013 09:17:15 PM  10/23/2013 02:15:21 PM  TLC Taxi and Limousine Commission   Taxi Complaint  Driver Complaint    Street  11430   PLACENAME   JAMAICA J F K AIRPORT   Closed  QUEENS
3389205   89205   10/09/2013 06:01:48 PM  10/09/2013 09:04:26 PM  NYPD    New York City Police Department Vending Unlicensed  Park/Playground 10000   PLACENAME   NEW YORK    CENTRAL PARK    Closed  MANHATTAN
3489382   89382   10/09/2013 04:53:01 PM  10/18/2013 08:35:02 AM  DOT Department of Transportation    Public Toilet   Damaged Door    Sidewalk    11238   PLACENAME   BROOKLYN    GRAND ARMY PLAZA    Closed  BROOKLYN
3589734   89734   10/09/2013 03:13:23 PM  10/09/2013 05:10:45 PM  NYPD    New York City Police Department Noise - Park    Loud Music/Party    Park/Playground 10036   PLACENAME   NEW YORK    BRYANT PARK Closed  MANHATTAN
3693990   93990   10/08/2013 06:14:15 PM  10/09/2013 04:00:59 PM  TLC Taxi and Limousine Commission   Taxi Complaint  Driver Complaint    Street  10003   PLACENAME   NEW YORK    BETH ISRAEL MED CENTER  Closed  MANHATTAN
3799407   99407   10/07/2013 03:56:11 PM  10/08/2013 07:04:14 AM  DPR Department of Parks and Recreation  Overgrown Tree/Branches Traffic Sign or Signal Blocked  Street  11430.0 PLACENAME   JAMAICA J F K AIRPORT   Closed  QUEENS
3899847   99847   10/07/2013 02:33:21 PM  10/09/2013 02:36:42 PM  TLC Taxi and Limousine Commission   Taxi Complaint  Driver Complaint    Street  10036.0 PLACENAME   NEW YORK    PORT AUTH 42 STREET Closed  MANHATTAN
39100073  100073  10/07/2013 01:36:02 PM  10/09/2013 09:56:55 AM  TLC Taxi and Limousine Commission   Taxi Complaint  Driver Complaint    Street  10024.0 PLACENAME   NEW YORK    MUSEUM NATURAL HIST Closed  MANHATTAN
40101013  101013  10/07/2013 10:05:18 AM  10/09/2013 03:36:23 PM  TLC Taxi and Limousine Commission   Taxi Complaint  Driver Complaint    Street  10017.0 PLACENAME   NEW YORK    GRAND CENTRAL TERM  Closed  MANHATTAN
41104020  104020  10/06/2013 02:58:47 PM  10/07/2013 12:11:16 PM  TLC Taxi and Limousine Commission   For Hire Vehicle Complaint  Car Service Company Complaint   Street  11430.0 PLACENAME   JAMAICA JFK Closed  QUEENS
42106118  106118  10/05/2013 03:24:47 PM  10/05/2013 04:20:34 PM  NYPD    New York City Police Department Noise - Park    Loud Music/Party    Park/Playground 10000.0 PLACENAME   NEW YORK    CENTRAL PARK    Closed  MANHATTAN
43106499  106499  10/05/2013 11:52:13 AM  10/07/2013 08:00:28 AM  DOT Department of Transportation    Public Toilet   Dirty/Graffiti  Sidewalk    11369.0 PLACENAME   EAST ELMHURST   LA GUARDIA AIRPORT  Closed  QUEENS
44

this is how my data looks like. What I want to do here is to extract date from created date and closed date I have tried .extract method but I am new to this so, it didn't work. I want to calculate housrs from created date and closed which i can do like this:

1Unnamed: 0     Created Date             Closed Date            Agency   Agency Name  Complaint Type Descriptor  Location Type   Incident Zip    Address Type    City    Landmark    Status  Borough
22869    2869    10/30/2013 09:14:47 AM  10/30/2013 10:48:51 AM  NYPD    New York City Police Department Illegal Parking Double Parked Blocking Traffic  Street/Sidewalk 11217.0 PLACENAME   BROOKLYN    BARCLAYS CENTER Closed  BROOKLYN
323571   23571   10/25/2013 02:33:54 PM  10/25/2013 03:36:36 PM  NYPD    New York City Police Department Noise - Park    Loud Music/Party    Park/Playground 10000   PLACENAME   NEW YORK    CENTRAL PARK    Closed  MANHATTAN
441625   41625   10/22/2013 09:33:56 PM  10/24/2013 05:37:24 PM  TLC Taxi and Limousine Commission   For Hire Vehicle Complaint  Car Service Company Complaint   Street  11430   PLACENAME   JAMAICA J F K AIRPORT   Closed  QUEENS
544331   44331   10/22/2013 07:25:35 AM  10/25/2013 10:40:35 AM  TLC Taxi and Limousine Commission   Taxi Complaint  Driver Complaint    Street  11430   PLACENAME   JAMAICA J F K AIRPORT   Closed  QUEENS
646913   46913   10/21/2013 05:03:26 PM  10/23/2013 09:59:23 AM  DPR Department of Parks and Recreation  Dead Tree   Dead/Dying Tree Street  11215   PLACENAME   BROOKLYN    BARTEL PRITCHARD SQUARE Closed  BROOKLYN
747459   47459   10/21/2013 02:56:08 PM  10/29/2013 06:17:10 PM  TLC Taxi and Limousine Commission   Taxi Complaint  Driver Complaint    Street  10031   PLACENAME   NEW YORK    CITY COLLEGE    Closed  MANHATTAN
848465   48465   10/21/2013 10:44:10 AM  10/21/2013 11:17:47 AM  NYPD    New York City Police Department Illegal Parking Posted Parking Sign Violation   Street/Sidewalk 11434   PLACENAME   JAMAICA PS 37   Closed  QUEENS
951837   51837   10/20/2013 04:36:12 PM  10/20/2013 06:35:49 PM  NYPD    New York City Police Department Noise - Park    Loud Music/Party    Park/Playground 10031.0 PLACENAME   NEW YORK    JACKIE ROBINSON PARK    Closed  MANHATTAN
1051848   51848   10/20/2013 04:26:03 PM  10/20/2013 06:34:47 PM  NYPD    New York City Police Department Noise - Park    Loud Music/Party    Park/Playground 10031.0 PLACENAME   NEW YORK    JACKIE ROBINSON PARK    Closed  MANHATTAN
1154089   54089   10/19/2013 03:45:47 PM  10/19/2013 04:10:11 PM  NYPD    New York City Police Department Noise - Park    Loud Music/Party    Park/Playground 10000.0 PLACENAME   NEW YORK    CENTRAL PARK    Closed  MANHATTAN
1254343   54343   10/19/2013 01:27:43 PM  10/28/2013 08:42:12 AM  DOT Department of Transportation    Street Condition    Rough, Pitted or Cracked Roads  Street  10003.0 PLACENAME   NEW YORK    UNION SQUARE PARK   Closed  MANHATTAN
1355140   55140   10/19/2013 02:02:28 AM  10/19/2013 02:19:55 AM  NYPD    New York City Police Department Noise - Vehicle Car/Truck Music Street/Sidewalk 11368.0 PLACENAME   CORONA  WORLDS FAIR MARINA  Closed  QUEENS
1457789   57789   10/18/2013 11:55:44 AM  10/23/2013 02:42:14 PM  TLC Taxi and Limousine Commission   Taxi Complaint  Driver Complaint    Street  11369.0 PLACENAME   EAST ELMHURST   LA GUARDIA AIRPORT  Closed  QUEENS
1563119   63119   10/17/2013 06:52:37 AM  10/25/2013 06:49:59 PM  TLC Taxi and Limousine Commission   Taxi Complaint  Driver Complaint    Street  11430.0 PLACENAME   JAMAICA J F K AIRPORT   Closed  QUEENS
1666242   66242   10/16/2013 01:56:24 PM  10/22/2013 03:09:11 PM  TLC Taxi and Limousine Commission   Taxi Complaint  Driver Complaint    Street  11369   PLACENAME   EAST ELMHURST   LA GUARDIA AIRPORT  Closed  QUEENS
1766758   66758   10/16/2013 11:52:43 AM  10/16/2013 04:35:34 PM  NYPD    New York City Police Department Vending Unlicensed  Park/Playground 10036   PLACENAME   NEW YORK    BRYANT PARK Closed  MANHATTAN
1866786   66786   10/16/2013 11:42:23 AM  10/18/2013 04:57:04 PM  TLC Taxi and Limousine Commission   Taxi Complaint  Insurance Information Requested Street  10003   PLACENAME   NEW YORK    BETH ISRAEL MED CENTER  Closed  MANHATTAN
1966809   66809   10/16/2013 11:36:54 AM  10/16/2013 12:34:23 PM  NYPD    New York City Police Department Traffic Congestion/Gridlock Street/Sidewalk 11430   PLACENAME   JAMAICA J F K AIRPORT   Closed  QUEENS
2067465   67465   10/16/2013 09:14:35 AM  10/16/2013 12:43:06 PM  NYPD    New York City Police Department Traffic Drag Racing Street/Sidewalk 11367   PLACENAME   FLUSHING    QUEENS COLLEGE  Closed  QUEENS
2172424   72424   10/15/2013 12:22:00 AM  10/21/2013 12:16:15 PM  TLC Taxi and Limousine Commission   Taxi Complaint  Driver Complaint    Street  11217   PLACENAME   BROOKLYN    BARCLAYS CENTER Closed  BROOKLYN
2275531   75531   10/14/2013 10:59:20 AM  10/14/2013 03:09:51 PM  NYPD    New York City Police Department Vending In Prohibited Area  Park/Playground 10000   PLACENAME   NEW YORK    CENTRAL PARK    Closed  MANHATTAN
2377918   77918   10/13/2013 03:16:03 PM  10/13/2013 03:25:45 PM  NYPD    New York City Police Department Noise - Park    Loud Music/Party    Park/Playground 10000   PLACENAME   NEW YORK    CENTRAL PARK    Closed  MANHATTAN
2478048   78048   10/13/2013 01:06:02 PM  10/21/2013 10:20:21 AM  TLC Taxi and Limousine Commission   Taxi Complaint  Driver Complaint    Street  11369   PLACENAME   EAST ELMHURST   LA GUARDIA AIRPORT  Closed  QUEENS
2578352   78352   10/13/2013 05:14:33 AM  10/16/2013 01:42:42 PM  TLC Taxi and Limousine Commission   For Hire Vehicle Complaint  Car Service Company Complaint   Street  11217   PLACENAME   BROOKLYN    BARCLAYS CENTER Closed  BROOKLYN
2678383   78383   10/13/2013 03:50:02 AM  10/13/2013 05:03:13 AM  NYPD    New York City Police Department Noise - Vehicle Car/Truck Music Street/Sidewalk 11368   PLACENAME   CORONA  WORLDS FAIR MARINA  Closed  QUEENS
2779078   79078   10/12/2013 09:53:17 PM  10/13/2013 02:52:07 AM  NYPD    New York City Police Department Noise - Park    Loud Music/Party    Park/Playground 10011   PLACENAME   NEW YORK    WASHINGTON SQUARE PARK  Closed  MANHATTAN
2884489   84489   10/10/2013 07:16:16 PM  10/10/2013 10:29:16 PM  NYPD    New York City Police Department Noise - Park    Loud Music/Party    Park/Playground 11215   PLACENAME   BROOKLYN    PROSPECT PARK   Closed  BROOKLYN
2984518   84518   10/10/2013 07:02:29 PM  10/10/2013 10:29:16 PM  NYPD    New York City Police Department Noise - Park    Loud Music/Party    Park/Playground 11215   PLACENAME   BROOKLYN    PROSPECT PARK   Closed  BROOKLYN
3084688   84688   10/10/2013 05:39:19 PM  10/10/2013 10:29:17 PM  NYPD    New York City Police Department Noise - Park    Loud Music/Party    Park/Playground 11215   PLACENAME   BROOKLYN    PROSPECT PARK   Closed  BROOKLYN
3184695   84695   10/10/2013 05:37:04 PM  10/10/2013 10:30:19 PM  NYPD    New York City Police Department Noise - Park    Loud Music/Party    Park/Playground 11215   PLACENAME   BROOKLYN    PROSPECT PARK   Closed  BROOKLYN
3288812   88812   10/09/2013 09:17:15 PM  10/23/2013 02:15:21 PM  TLC Taxi and Limousine Commission   Taxi Complaint  Driver Complaint    Street  11430   PLACENAME   JAMAICA J F K AIRPORT   Closed  QUEENS
3389205   89205   10/09/2013 06:01:48 PM  10/09/2013 09:04:26 PM  NYPD    New York City Police Department Vending Unlicensed  Park/Playground 10000   PLACENAME   NEW YORK    CENTRAL PARK    Closed  MANHATTAN
3489382   89382   10/09/2013 04:53:01 PM  10/18/2013 08:35:02 AM  DOT Department of Transportation    Public Toilet   Damaged Door    Sidewalk    11238   PLACENAME   BROOKLYN    GRAND ARMY PLAZA    Closed  BROOKLYN
3589734   89734   10/09/2013 03:13:23 PM  10/09/2013 05:10:45 PM  NYPD    New York City Police Department Noise - Park    Loud Music/Party    Park/Playground 10036   PLACENAME   NEW YORK    BRYANT PARK Closed  MANHATTAN
3693990   93990   10/08/2013 06:14:15 PM  10/09/2013 04:00:59 PM  TLC Taxi and Limousine Commission   Taxi Complaint  Driver Complaint    Street  10003   PLACENAME   NEW YORK    BETH ISRAEL MED CENTER  Closed  MANHATTAN
3799407   99407   10/07/2013 03:56:11 PM  10/08/2013 07:04:14 AM  DPR Department of Parks and Recreation  Overgrown Tree/Branches Traffic Sign or Signal Blocked  Street  11430.0 PLACENAME   JAMAICA J F K AIRPORT   Closed  QUEENS
3899847   99847   10/07/2013 02:33:21 PM  10/09/2013 02:36:42 PM  TLC Taxi and Limousine Commission   Taxi Complaint  Driver Complaint    Street  10036.0 PLACENAME   NEW YORK    PORT AUTH 42 STREET Closed  MANHATTAN
39100073  100073  10/07/2013 01:36:02 PM  10/09/2013 09:56:55 AM  TLC Taxi and Limousine Commission   Taxi Complaint  Driver Complaint    Street  10024.0 PLACENAME   NEW YORK    MUSEUM NATURAL HIST Closed  MANHATTAN
40101013  101013  10/07/2013 10:05:18 AM  10/09/2013 03:36:23 PM  TLC Taxi and Limousine Commission   Taxi Complaint  Driver Complaint    Street  10017.0 PLACENAME   NEW YORK    GRAND CENTRAL TERM  Closed  MANHATTAN
41104020  104020  10/06/2013 02:58:47 PM  10/07/2013 12:11:16 PM  TLC Taxi and Limousine Commission   For Hire Vehicle Complaint  Car Service Company Complaint   Street  11430.0 PLACENAME   JAMAICA JFK Closed  QUEENS
42106118  106118  10/05/2013 03:24:47 PM  10/05/2013 04:20:34 PM  NYPD    New York City Police Department Noise - Park    Loud Music/Party    Park/Playground 10000.0 PLACENAME   NEW YORK    CENTRAL PARK    Closed  MANHATTAN
43106499  106499  10/05/2013 11:52:13 AM  10/07/2013 08:00:28 AM  DOT Department of Transportation    Public Toilet   Dirty/Graffiti  Sidewalk    11369.0 PLACENAME   EAST ELMHURST   LA GUARDIA AIRPORT  Closed  QUEENS
44pd.Timedelta(task3['Closed Date'] - task3['Created Date']).seconds / 60.0
45

In the end I want the output Find average completion time in hours for top-10 most frequent complaints. Also calculate how many data points you have for each complaint types. Do this analysis only for closed complaints. The sample output I want is as follows:

1Unnamed: 0     Created Date             Closed Date            Agency   Agency Name  Complaint Type Descriptor  Location Type   Incident Zip    Address Type    City    Landmark    Status  Borough
22869    2869    10/30/2013 09:14:47 AM  10/30/2013 10:48:51 AM  NYPD    New York City Police Department Illegal Parking Double Parked Blocking Traffic  Street/Sidewalk 11217.0 PLACENAME   BROOKLYN    BARCLAYS CENTER Closed  BROOKLYN
323571   23571   10/25/2013 02:33:54 PM  10/25/2013 03:36:36 PM  NYPD    New York City Police Department Noise - Park    Loud Music/Party    Park/Playground 10000   PLACENAME   NEW YORK    CENTRAL PARK    Closed  MANHATTAN
441625   41625   10/22/2013 09:33:56 PM  10/24/2013 05:37:24 PM  TLC Taxi and Limousine Commission   For Hire Vehicle Complaint  Car Service Company Complaint   Street  11430   PLACENAME   JAMAICA J F K AIRPORT   Closed  QUEENS
544331   44331   10/22/2013 07:25:35 AM  10/25/2013 10:40:35 AM  TLC Taxi and Limousine Commission   Taxi Complaint  Driver Complaint    Street  11430   PLACENAME   JAMAICA J F K AIRPORT   Closed  QUEENS
646913   46913   10/21/2013 05:03:26 PM  10/23/2013 09:59:23 AM  DPR Department of Parks and Recreation  Dead Tree   Dead/Dying Tree Street  11215   PLACENAME   BROOKLYN    BARTEL PRITCHARD SQUARE Closed  BROOKLYN
747459   47459   10/21/2013 02:56:08 PM  10/29/2013 06:17:10 PM  TLC Taxi and Limousine Commission   Taxi Complaint  Driver Complaint    Street  10031   PLACENAME   NEW YORK    CITY COLLEGE    Closed  MANHATTAN
848465   48465   10/21/2013 10:44:10 AM  10/21/2013 11:17:47 AM  NYPD    New York City Police Department Illegal Parking Posted Parking Sign Violation   Street/Sidewalk 11434   PLACENAME   JAMAICA PS 37   Closed  QUEENS
951837   51837   10/20/2013 04:36:12 PM  10/20/2013 06:35:49 PM  NYPD    New York City Police Department Noise - Park    Loud Music/Party    Park/Playground 10031.0 PLACENAME   NEW YORK    JACKIE ROBINSON PARK    Closed  MANHATTAN
1051848   51848   10/20/2013 04:26:03 PM  10/20/2013 06:34:47 PM  NYPD    New York City Police Department Noise - Park    Loud Music/Party    Park/Playground 10031.0 PLACENAME   NEW YORK    JACKIE ROBINSON PARK    Closed  MANHATTAN
1154089   54089   10/19/2013 03:45:47 PM  10/19/2013 04:10:11 PM  NYPD    New York City Police Department Noise - Park    Loud Music/Party    Park/Playground 10000.0 PLACENAME   NEW YORK    CENTRAL PARK    Closed  MANHATTAN
1254343   54343   10/19/2013 01:27:43 PM  10/28/2013 08:42:12 AM  DOT Department of Transportation    Street Condition    Rough, Pitted or Cracked Roads  Street  10003.0 PLACENAME   NEW YORK    UNION SQUARE PARK   Closed  MANHATTAN
1355140   55140   10/19/2013 02:02:28 AM  10/19/2013 02:19:55 AM  NYPD    New York City Police Department Noise - Vehicle Car/Truck Music Street/Sidewalk 11368.0 PLACENAME   CORONA  WORLDS FAIR MARINA  Closed  QUEENS
1457789   57789   10/18/2013 11:55:44 AM  10/23/2013 02:42:14 PM  TLC Taxi and Limousine Commission   Taxi Complaint  Driver Complaint    Street  11369.0 PLACENAME   EAST ELMHURST   LA GUARDIA AIRPORT  Closed  QUEENS
1563119   63119   10/17/2013 06:52:37 AM  10/25/2013 06:49:59 PM  TLC Taxi and Limousine Commission   Taxi Complaint  Driver Complaint    Street  11430.0 PLACENAME   JAMAICA J F K AIRPORT   Closed  QUEENS
1666242   66242   10/16/2013 01:56:24 PM  10/22/2013 03:09:11 PM  TLC Taxi and Limousine Commission   Taxi Complaint  Driver Complaint    Street  11369   PLACENAME   EAST ELMHURST   LA GUARDIA AIRPORT  Closed  QUEENS
1766758   66758   10/16/2013 11:52:43 AM  10/16/2013 04:35:34 PM  NYPD    New York City Police Department Vending Unlicensed  Park/Playground 10036   PLACENAME   NEW YORK    BRYANT PARK Closed  MANHATTAN
1866786   66786   10/16/2013 11:42:23 AM  10/18/2013 04:57:04 PM  TLC Taxi and Limousine Commission   Taxi Complaint  Insurance Information Requested Street  10003   PLACENAME   NEW YORK    BETH ISRAEL MED CENTER  Closed  MANHATTAN
1966809   66809   10/16/2013 11:36:54 AM  10/16/2013 12:34:23 PM  NYPD    New York City Police Department Traffic Congestion/Gridlock Street/Sidewalk 11430   PLACENAME   JAMAICA J F K AIRPORT   Closed  QUEENS
2067465   67465   10/16/2013 09:14:35 AM  10/16/2013 12:43:06 PM  NYPD    New York City Police Department Traffic Drag Racing Street/Sidewalk 11367   PLACENAME   FLUSHING    QUEENS COLLEGE  Closed  QUEENS
2172424   72424   10/15/2013 12:22:00 AM  10/21/2013 12:16:15 PM  TLC Taxi and Limousine Commission   Taxi Complaint  Driver Complaint    Street  11217   PLACENAME   BROOKLYN    BARCLAYS CENTER Closed  BROOKLYN
2275531   75531   10/14/2013 10:59:20 AM  10/14/2013 03:09:51 PM  NYPD    New York City Police Department Vending In Prohibited Area  Park/Playground 10000   PLACENAME   NEW YORK    CENTRAL PARK    Closed  MANHATTAN
2377918   77918   10/13/2013 03:16:03 PM  10/13/2013 03:25:45 PM  NYPD    New York City Police Department Noise - Park    Loud Music/Party    Park/Playground 10000   PLACENAME   NEW YORK    CENTRAL PARK    Closed  MANHATTAN
2478048   78048   10/13/2013 01:06:02 PM  10/21/2013 10:20:21 AM  TLC Taxi and Limousine Commission   Taxi Complaint  Driver Complaint    Street  11369   PLACENAME   EAST ELMHURST   LA GUARDIA AIRPORT  Closed  QUEENS
2578352   78352   10/13/2013 05:14:33 AM  10/16/2013 01:42:42 PM  TLC Taxi and Limousine Commission   For Hire Vehicle Complaint  Car Service Company Complaint   Street  11217   PLACENAME   BROOKLYN    BARCLAYS CENTER Closed  BROOKLYN
2678383   78383   10/13/2013 03:50:02 AM  10/13/2013 05:03:13 AM  NYPD    New York City Police Department Noise - Vehicle Car/Truck Music Street/Sidewalk 11368   PLACENAME   CORONA  WORLDS FAIR MARINA  Closed  QUEENS
2779078   79078   10/12/2013 09:53:17 PM  10/13/2013 02:52:07 AM  NYPD    New York City Police Department Noise - Park    Loud Music/Party    Park/Playground 10011   PLACENAME   NEW YORK    WASHINGTON SQUARE PARK  Closed  MANHATTAN
2884489   84489   10/10/2013 07:16:16 PM  10/10/2013 10:29:16 PM  NYPD    New York City Police Department Noise - Park    Loud Music/Party    Park/Playground 11215   PLACENAME   BROOKLYN    PROSPECT PARK   Closed  BROOKLYN
2984518   84518   10/10/2013 07:02:29 PM  10/10/2013 10:29:16 PM  NYPD    New York City Police Department Noise - Park    Loud Music/Party    Park/Playground 11215   PLACENAME   BROOKLYN    PROSPECT PARK   Closed  BROOKLYN
3084688   84688   10/10/2013 05:39:19 PM  10/10/2013 10:29:17 PM  NYPD    New York City Police Department Noise - Park    Loud Music/Party    Park/Playground 11215   PLACENAME   BROOKLYN    PROSPECT PARK   Closed  BROOKLYN
3184695   84695   10/10/2013 05:37:04 PM  10/10/2013 10:30:19 PM  NYPD    New York City Police Department Noise - Park    Loud Music/Party    Park/Playground 11215   PLACENAME   BROOKLYN    PROSPECT PARK   Closed  BROOKLYN
3288812   88812   10/09/2013 09:17:15 PM  10/23/2013 02:15:21 PM  TLC Taxi and Limousine Commission   Taxi Complaint  Driver Complaint    Street  11430   PLACENAME   JAMAICA J F K AIRPORT   Closed  QUEENS
3389205   89205   10/09/2013 06:01:48 PM  10/09/2013 09:04:26 PM  NYPD    New York City Police Department Vending Unlicensed  Park/Playground 10000   PLACENAME   NEW YORK    CENTRAL PARK    Closed  MANHATTAN
3489382   89382   10/09/2013 04:53:01 PM  10/18/2013 08:35:02 AM  DOT Department of Transportation    Public Toilet   Damaged Door    Sidewalk    11238   PLACENAME   BROOKLYN    GRAND ARMY PLAZA    Closed  BROOKLYN
3589734   89734   10/09/2013 03:13:23 PM  10/09/2013 05:10:45 PM  NYPD    New York City Police Department Noise - Park    Loud Music/Party    Park/Playground 10036   PLACENAME   NEW YORK    BRYANT PARK Closed  MANHATTAN
3693990   93990   10/08/2013 06:14:15 PM  10/09/2013 04:00:59 PM  TLC Taxi and Limousine Commission   Taxi Complaint  Driver Complaint    Street  10003   PLACENAME   NEW YORK    BETH ISRAEL MED CENTER  Closed  MANHATTAN
3799407   99407   10/07/2013 03:56:11 PM  10/08/2013 07:04:14 AM  DPR Department of Parks and Recreation  Overgrown Tree/Branches Traffic Sign or Signal Blocked  Street  11430.0 PLACENAME   JAMAICA J F K AIRPORT   Closed  QUEENS
3899847   99847   10/07/2013 02:33:21 PM  10/09/2013 02:36:42 PM  TLC Taxi and Limousine Commission   Taxi Complaint  Driver Complaint    Street  10036.0 PLACENAME   NEW YORK    PORT AUTH 42 STREET Closed  MANHATTAN
39100073  100073  10/07/2013 01:36:02 PM  10/09/2013 09:56:55 AM  TLC Taxi and Limousine Commission   Taxi Complaint  Driver Complaint    Street  10024.0 PLACENAME   NEW YORK    MUSEUM NATURAL HIST Closed  MANHATTAN
40101013  101013  10/07/2013 10:05:18 AM  10/09/2013 03:36:23 PM  TLC Taxi and Limousine Commission   Taxi Complaint  Driver Complaint    Street  10017.0 PLACENAME   NEW YORK    GRAND CENTRAL TERM  Closed  MANHATTAN
41104020  104020  10/06/2013 02:58:47 PM  10/07/2013 12:11:16 PM  TLC Taxi and Limousine Commission   For Hire Vehicle Complaint  Car Service Company Complaint   Street  11430.0 PLACENAME   JAMAICA JFK Closed  QUEENS
42106118  106118  10/05/2013 03:24:47 PM  10/05/2013 04:20:34 PM  NYPD    New York City Police Department Noise - Park    Loud Music/Party    Park/Playground 10000.0 PLACENAME   NEW YORK    CENTRAL PARK    Closed  MANHATTAN
43106499  106499  10/05/2013 11:52:13 AM  10/07/2013 08:00:28 AM  DOT Department of Transportation    Public Toilet   Dirty/Graffiti  Sidewalk    11369.0 PLACENAME   EAST ELMHURST   LA GUARDIA AIRPORT  Closed  QUEENS
44pd.Timedelta(task3['Closed Date'] - task3['Created Date']).seconds / 60.0
45                         mean                   count
46complaint Type           closing_time_hours     closing_time_hours
47Blocked Driveway         3.00                   4581
48DOF Literature Request   30.16                  5481
49General Construction     66.38                  798
50Heating                  54.88                  6704
51Illegal Parking          3.08                   3336
52Nonconst                 65                     100 
53Paint-Plaster            49                     3281 
54Plumbing                 65                     666   
55Strret Condition         81                     2610    
56Street Light Condition   90                     4207
57

these mean and count values are randomly generated The sample output can be produced by groupby function once the hours are extracted from the data. dictionary format

1Unnamed: 0     Created Date             Closed Date            Agency   Agency Name  Complaint Type Descriptor  Location Type   Incident Zip    Address Type    City    Landmark    Status  Borough
22869    2869    10/30/2013 09:14:47 AM  10/30/2013 10:48:51 AM  NYPD    New York City Police Department Illegal Parking Double Parked Blocking Traffic  Street/Sidewalk 11217.0 PLACENAME   BROOKLYN    BARCLAYS CENTER Closed  BROOKLYN
323571   23571   10/25/2013 02:33:54 PM  10/25/2013 03:36:36 PM  NYPD    New York City Police Department Noise - Park    Loud Music/Party    Park/Playground 10000   PLACENAME   NEW YORK    CENTRAL PARK    Closed  MANHATTAN
441625   41625   10/22/2013 09:33:56 PM  10/24/2013 05:37:24 PM  TLC Taxi and Limousine Commission   For Hire Vehicle Complaint  Car Service Company Complaint   Street  11430   PLACENAME   JAMAICA J F K AIRPORT   Closed  QUEENS
544331   44331   10/22/2013 07:25:35 AM  10/25/2013 10:40:35 AM  TLC Taxi and Limousine Commission   Taxi Complaint  Driver Complaint    Street  11430   PLACENAME   JAMAICA J F K AIRPORT   Closed  QUEENS
646913   46913   10/21/2013 05:03:26 PM  10/23/2013 09:59:23 AM  DPR Department of Parks and Recreation  Dead Tree   Dead/Dying Tree Street  11215   PLACENAME   BROOKLYN    BARTEL PRITCHARD SQUARE Closed  BROOKLYN
747459   47459   10/21/2013 02:56:08 PM  10/29/2013 06:17:10 PM  TLC Taxi and Limousine Commission   Taxi Complaint  Driver Complaint    Street  10031   PLACENAME   NEW YORK    CITY COLLEGE    Closed  MANHATTAN
848465   48465   10/21/2013 10:44:10 AM  10/21/2013 11:17:47 AM  NYPD    New York City Police Department Illegal Parking Posted Parking Sign Violation   Street/Sidewalk 11434   PLACENAME   JAMAICA PS 37   Closed  QUEENS
951837   51837   10/20/2013 04:36:12 PM  10/20/2013 06:35:49 PM  NYPD    New York City Police Department Noise - Park    Loud Music/Party    Park/Playground 10031.0 PLACENAME   NEW YORK    JACKIE ROBINSON PARK    Closed  MANHATTAN
1051848   51848   10/20/2013 04:26:03 PM  10/20/2013 06:34:47 PM  NYPD    New York City Police Department Noise - Park    Loud Music/Party    Park/Playground 10031.0 PLACENAME   NEW YORK    JACKIE ROBINSON PARK    Closed  MANHATTAN
1154089   54089   10/19/2013 03:45:47 PM  10/19/2013 04:10:11 PM  NYPD    New York City Police Department Noise - Park    Loud Music/Party    Park/Playground 10000.0 PLACENAME   NEW YORK    CENTRAL PARK    Closed  MANHATTAN
1254343   54343   10/19/2013 01:27:43 PM  10/28/2013 08:42:12 AM  DOT Department of Transportation    Street Condition    Rough, Pitted or Cracked Roads  Street  10003.0 PLACENAME   NEW YORK    UNION SQUARE PARK   Closed  MANHATTAN
1355140   55140   10/19/2013 02:02:28 AM  10/19/2013 02:19:55 AM  NYPD    New York City Police Department Noise - Vehicle Car/Truck Music Street/Sidewalk 11368.0 PLACENAME   CORONA  WORLDS FAIR MARINA  Closed  QUEENS
1457789   57789   10/18/2013 11:55:44 AM  10/23/2013 02:42:14 PM  TLC Taxi and Limousine Commission   Taxi Complaint  Driver Complaint    Street  11369.0 PLACENAME   EAST ELMHURST   LA GUARDIA AIRPORT  Closed  QUEENS
1563119   63119   10/17/2013 06:52:37 AM  10/25/2013 06:49:59 PM  TLC Taxi and Limousine Commission   Taxi Complaint  Driver Complaint    Street  11430.0 PLACENAME   JAMAICA J F K AIRPORT   Closed  QUEENS
1666242   66242   10/16/2013 01:56:24 PM  10/22/2013 03:09:11 PM  TLC Taxi and Limousine Commission   Taxi Complaint  Driver Complaint    Street  11369   PLACENAME   EAST ELMHURST   LA GUARDIA AIRPORT  Closed  QUEENS
1766758   66758   10/16/2013 11:52:43 AM  10/16/2013 04:35:34 PM  NYPD    New York City Police Department Vending Unlicensed  Park/Playground 10036   PLACENAME   NEW YORK    BRYANT PARK Closed  MANHATTAN
1866786   66786   10/16/2013 11:42:23 AM  10/18/2013 04:57:04 PM  TLC Taxi and Limousine Commission   Taxi Complaint  Insurance Information Requested Street  10003   PLACENAME   NEW YORK    BETH ISRAEL MED CENTER  Closed  MANHATTAN
1966809   66809   10/16/2013 11:36:54 AM  10/16/2013 12:34:23 PM  NYPD    New York City Police Department Traffic Congestion/Gridlock Street/Sidewalk 11430   PLACENAME   JAMAICA J F K AIRPORT   Closed  QUEENS
2067465   67465   10/16/2013 09:14:35 AM  10/16/2013 12:43:06 PM  NYPD    New York City Police Department Traffic Drag Racing Street/Sidewalk 11367   PLACENAME   FLUSHING    QUEENS COLLEGE  Closed  QUEENS
2172424   72424   10/15/2013 12:22:00 AM  10/21/2013 12:16:15 PM  TLC Taxi and Limousine Commission   Taxi Complaint  Driver Complaint    Street  11217   PLACENAME   BROOKLYN    BARCLAYS CENTER Closed  BROOKLYN
2275531   75531   10/14/2013 10:59:20 AM  10/14/2013 03:09:51 PM  NYPD    New York City Police Department Vending In Prohibited Area  Park/Playground 10000   PLACENAME   NEW YORK    CENTRAL PARK    Closed  MANHATTAN
2377918   77918   10/13/2013 03:16:03 PM  10/13/2013 03:25:45 PM  NYPD    New York City Police Department Noise - Park    Loud Music/Party    Park/Playground 10000   PLACENAME   NEW YORK    CENTRAL PARK    Closed  MANHATTAN
2478048   78048   10/13/2013 01:06:02 PM  10/21/2013 10:20:21 AM  TLC Taxi and Limousine Commission   Taxi Complaint  Driver Complaint    Street  11369   PLACENAME   EAST ELMHURST   LA GUARDIA AIRPORT  Closed  QUEENS
2578352   78352   10/13/2013 05:14:33 AM  10/16/2013 01:42:42 PM  TLC Taxi and Limousine Commission   For Hire Vehicle Complaint  Car Service Company Complaint   Street  11217   PLACENAME   BROOKLYN    BARCLAYS CENTER Closed  BROOKLYN
2678383   78383   10/13/2013 03:50:02 AM  10/13/2013 05:03:13 AM  NYPD    New York City Police Department Noise - Vehicle Car/Truck Music Street/Sidewalk 11368   PLACENAME   CORONA  WORLDS FAIR MARINA  Closed  QUEENS
2779078   79078   10/12/2013 09:53:17 PM  10/13/2013 02:52:07 AM  NYPD    New York City Police Department Noise - Park    Loud Music/Party    Park/Playground 10011   PLACENAME   NEW YORK    WASHINGTON SQUARE PARK  Closed  MANHATTAN
2884489   84489   10/10/2013 07:16:16 PM  10/10/2013 10:29:16 PM  NYPD    New York City Police Department Noise - Park    Loud Music/Party    Park/Playground 11215   PLACENAME   BROOKLYN    PROSPECT PARK   Closed  BROOKLYN
2984518   84518   10/10/2013 07:02:29 PM  10/10/2013 10:29:16 PM  NYPD    New York City Police Department Noise - Park    Loud Music/Party    Park/Playground 11215   PLACENAME   BROOKLYN    PROSPECT PARK   Closed  BROOKLYN
3084688   84688   10/10/2013 05:39:19 PM  10/10/2013 10:29:17 PM  NYPD    New York City Police Department Noise - Park    Loud Music/Party    Park/Playground 11215   PLACENAME   BROOKLYN    PROSPECT PARK   Closed  BROOKLYN
3184695   84695   10/10/2013 05:37:04 PM  10/10/2013 10:30:19 PM  NYPD    New York City Police Department Noise - Park    Loud Music/Party    Park/Playground 11215   PLACENAME   BROOKLYN    PROSPECT PARK   Closed  BROOKLYN
3288812   88812   10/09/2013 09:17:15 PM  10/23/2013 02:15:21 PM  TLC Taxi and Limousine Commission   Taxi Complaint  Driver Complaint    Street  11430   PLACENAME   JAMAICA J F K AIRPORT   Closed  QUEENS
3389205   89205   10/09/2013 06:01:48 PM  10/09/2013 09:04:26 PM  NYPD    New York City Police Department Vending Unlicensed  Park/Playground 10000   PLACENAME   NEW YORK    CENTRAL PARK    Closed  MANHATTAN
3489382   89382   10/09/2013 04:53:01 PM  10/18/2013 08:35:02 AM  DOT Department of Transportation    Public Toilet   Damaged Door    Sidewalk    11238   PLACENAME   BROOKLYN    GRAND ARMY PLAZA    Closed  BROOKLYN
3589734   89734   10/09/2013 03:13:23 PM  10/09/2013 05:10:45 PM  NYPD    New York City Police Department Noise - Park    Loud Music/Party    Park/Playground 10036   PLACENAME   NEW YORK    BRYANT PARK Closed  MANHATTAN
3693990   93990   10/08/2013 06:14:15 PM  10/09/2013 04:00:59 PM  TLC Taxi and Limousine Commission   Taxi Complaint  Driver Complaint    Street  10003   PLACENAME   NEW YORK    BETH ISRAEL MED CENTER  Closed  MANHATTAN
3799407   99407   10/07/2013 03:56:11 PM  10/08/2013 07:04:14 AM  DPR Department of Parks and Recreation  Overgrown Tree/Branches Traffic Sign or Signal Blocked  Street  11430.0 PLACENAME   JAMAICA J F K AIRPORT   Closed  QUEENS
3899847   99847   10/07/2013 02:33:21 PM  10/09/2013 02:36:42 PM  TLC Taxi and Limousine Commission   Taxi Complaint  Driver Complaint    Street  10036.0 PLACENAME   NEW YORK    PORT AUTH 42 STREET Closed  MANHATTAN
39100073  100073  10/07/2013 01:36:02 PM  10/09/2013 09:56:55 AM  TLC Taxi and Limousine Commission   Taxi Complaint  Driver Complaint    Street  10024.0 PLACENAME   NEW YORK    MUSEUM NATURAL HIST Closed  MANHATTAN
40101013  101013  10/07/2013 10:05:18 AM  10/09/2013 03:36:23 PM  TLC Taxi and Limousine Commission   Taxi Complaint  Driver Complaint    Street  10017.0 PLACENAME   NEW YORK    GRAND CENTRAL TERM  Closed  MANHATTAN
41104020  104020  10/06/2013 02:58:47 PM  10/07/2013 12:11:16 PM  TLC Taxi and Limousine Commission   For Hire Vehicle Complaint  Car Service Company Complaint   Street  11430.0 PLACENAME   JAMAICA JFK Closed  QUEENS
42106118  106118  10/05/2013 03:24:47 PM  10/05/2013 04:20:34 PM  NYPD    New York City Police Department Noise - Park    Loud Music/Party    Park/Playground 10000.0 PLACENAME   NEW YORK    CENTRAL PARK    Closed  MANHATTAN
43106499  106499  10/05/2013 11:52:13 AM  10/07/2013 08:00:28 AM  DOT Department of Transportation    Public Toilet   Dirty/Graffiti  Sidewalk    11369.0 PLACENAME   EAST ELMHURST   LA GUARDIA AIRPORT  Closed  QUEENS
44pd.Timedelta(task3['Closed Date'] - task3['Created Date']).seconds / 60.0
45                         mean                   count
46complaint Type           closing_time_hours     closing_time_hours
47Blocked Driveway         3.00                   4581
48DOF Literature Request   30.16                  5481
49General Construction     66.38                  798
50Heating                  54.88                  6704
51Illegal Parking          3.08                   3336
52Nonconst                 65                     100 
53Paint-Plaster            49                     3281 
54Plumbing                 65                     666   
55Strret Condition         81                     2610    
56Street Light Condition   90                     4207
57{'Unnamed: 0': {2869: 2869,
58  23571: 23571,
59  41625: 41625,
60  44331: 44331,
61  46913: 46913,
62  47459: 47459,
63  48465: 48465,
64  51837: 51837,
65  51848: 51848,
66  54089: 54089,
67  54343: 54343,
68  55140: 55140,
69  57789: 57789,
70  63119: 63119,
71  66242: 66242,
72  66758: 66758,
73  66786: 66786,
74  66809: 66809,
75  67465: 67465,
76  72424: 72424,
77  75531: 75531,
78  77918: 77918,
79  78048: 78048,
80  78352: 78352,
81  78383: 78383,
82  79078: 79078,
83  84489: 84489,
84  84518: 84518,
85  84688: 84688,
86  84695: 84695,
87  88812: 88812,
88  89205: 89205,
89  89382: 89382,
90  89734: 89734,
91  93990: 93990,
92  99407: 99407,
93  99847: 99847,
94  100073: 100073,
95  101013: 101013,
96  104020: 104020,
97  106118: 106118,
98  106499: 106499},
99 'Created Date': {2869: '10/30/2013 09:14:47 AM',
100  23571: '10/25/2013 02:33:54 PM',
101  41625: '10/22/2013 09:33:56 PM',
102  44331: '10/22/2013 07:25:35 AM',
103  46913: '10/21/2013 05:03:26 PM',
104  47459: '10/21/2013 02:56:08 PM',
105  48465: '10/21/2013 10:44:10 AM',
106  51837: '10/20/2013 04:36:12 PM',
107  51848: '10/20/2013 04:26:03 PM',
108  54089: '10/19/2013 03:45:47 PM',
109  54343: '10/19/2013 01:27:43 PM',
110  55140: '10/19/2013 02:02:28 AM',
111  57789: '10/18/2013 11:55:44 AM',
112  63119: '10/17/2013 06:52:37 AM',
113  66242: '10/16/2013 01:56:24 PM',
114  66758: '10/16/2013 11:52:43 AM',
115  66786: '10/16/2013 11:42:23 AM',
116  66809: '10/16/2013 11:36:54 AM',
117  67465: '10/16/2013 09:14:35 AM',
118  72424: '10/15/2013 12:22:00 AM',
119  75531: '10/14/2013 10:59:20 AM',
120  77918: '10/13/2013 03:16:03 PM',
121  78048: '10/13/2013 01:06:02 PM',
122  78352: '10/13/2013 05:14:33 AM',
123  78383: '10/13/2013 03:50:02 AM',
124  79078: '10/12/2013 09:53:17 PM',
125  84489: '10/10/2013 07:16:16 PM',
126  84518: '10/10/2013 07:02:29 PM',
127  84688: '10/10/2013 05:39:19 PM',
128  84695: '10/10/2013 05:37:04 PM',
129  88812: '10/09/2013 09:17:15 PM',
130  89205: '10/09/2013 06:01:48 PM',
131  89382: '10/09/2013 04:53:01 PM',
132  89734: '10/09/2013 03:13:23 PM',
133  93990: '10/08/2013 06:14:15 PM',
134  99407: '10/07/2013 03:56:11 PM',
135  99847: '10/07/2013 02:33:21 PM',
136  100073: '10/07/2013 01:36:02 PM',
137  101013: '10/07/2013 10:05:18 AM',
138  104020: '10/06/2013 02:58:47 PM',
139  106118: '10/05/2013 03:24:47 PM',
140  106499: '10/05/2013 11:52:13 AM'},
141 'Closed Date': {2869: '10/30/2013 10:48:51 AM',
142  23571: '10/25/2013 03:36:36 PM',
143  41625: '10/24/2013 05:37:24 PM',
144  44331: '10/25/2013 10:40:35 AM',
145  46913: '10/23/2013 09:59:23 AM',
146  47459: '10/29/2013 06:17:10 PM',
147  48465: '10/21/2013 11:17:47 AM',
148  51837: '10/20/2013 06:35:49 PM',
149  51848: '10/20/2013 06:34:47 PM',
150  54089: '10/19/2013 04:10:11 PM',
151  54343: '10/28/2013 08:42:12 AM',
152  55140: '10/19/2013 02:19:55 AM',
153  57789: '10/23/2013 02:42:14 PM',
154  63119: '10/25/2013 06:49:59 PM',
155  66242: '10/22/2013 03:09:11 PM',
156  66758: '10/16/2013 04:35:34 PM',
157  66786: '10/18/2013 04:57:04 PM',
158  66809: '10/16/2013 12:34:23 PM',
159  67465: '10/16/2013 12:43:06 PM',
160  72424: '10/21/2013 12:16:15 PM',
161  75531: '10/14/2013 03:09:51 PM',
162  77918: '10/13/2013 03:25:45 PM',
163  78048: '10/21/2013 10:20:21 AM',
164  78352: '10/16/2013 01:42:42 PM',
165  78383: '10/13/2013 05:03:13 AM',
166  79078: '10/13/2013 02:52:07 AM',
167  84489: '10/10/2013 10:29:16 PM',
168  84518: '10/10/2013 10:29:16 PM',
169  84688: '10/10/2013 10:29:17 PM',
170  84695: '10/10/2013 10:30:19 PM',
171  88812: '10/23/2013 02:15:21 PM',
172  89205: '10/09/2013 09:04:26 PM',
173  89382: '10/18/2013 08:35:02 AM',
174  89734: '10/09/2013 05:10:45 PM',
175  93990: '10/09/2013 04:00:59 PM',
176  99407: '10/08/2013 07:04:14 AM',
177  99847: '10/09/2013 02:36:42 PM',
178  100073: '10/09/2013 09:56:55 AM',
179  101013: '10/09/2013 03:36:23 PM',
180  104020: '10/07/2013 12:11:16 PM',
181  106118: '10/05/2013 04:20:34 PM',
182  106499: '10/07/2013 08:00:28 AM'},
183 'Agency': {2869: 'NYPD',
184  23571: 'NYPD',
185  41625: 'TLC',
186  44331: 'TLC',
187  46913: 'DPR',
188  47459: 'TLC',
189  48465: 'NYPD',
190  51837: 'NYPD',
191  51848: 'NYPD',
192  54089: 'NYPD',
193  54343: 'DOT',
194  55140: 'NYPD',
195  57789: 'TLC',
196  63119: 'TLC',
197  66242: 'TLC',
198  66758: 'NYPD',
199  66786: 'TLC',
200  66809: 'NYPD',
201  67465: 'NYPD',
202  72424: 'TLC',
203  75531: 'NYPD',
204  77918: 'NYPD',
205  78048: 'TLC',
206  78352: 'TLC',
207  78383: 'NYPD',
208  79078: 'NYPD',
209  84489: 'NYPD',
210  84518: 'NYPD',
211  84688: 'NYPD',
212  84695: 'NYPD',
213  88812: 'TLC',
214  89205: 'NYPD',
215  89382: 'DOT',
216  89734: 'NYPD',
217  93990: 'TLC',
218  99407: 'DPR',
219  99847: 'TLC',
220  100073: 'TLC',
221  101013: 'TLC',
222  104020: 'TLC',
223  106118: 'NYPD',
224  106499: 'DOT'},
225 'Agency Name': {2869: 'New York City Police Department',
226  23571: 'New York City Police Department',
227  41625: 'Taxi and Limousine Commission',
228  44331: 'Taxi and Limousine Commission',
229  46913: 'Department of Parks and Recreation',
230  47459: 'Taxi and Limousine Commission',
231  48465: 'New York City Police Department',
232  51837: 'New York City Police Department',
233  51848: 'New York City Police Department',
234  54089: 'New York City Police Department',
235  54343: 'Department of Transportation',
236  55140: 'New York City Police Department',
237  57789: 'Taxi and Limousine Commission',
238  63119: 'Taxi and Limousine Commission',
239  66242: 'Taxi and Limousine Commission',
240  66758: 'New York City Police Department',
241  66786: 'Taxi and Limousine Commission',
242  66809: 'New York City Police Department',
243  67465: 'New York City Police Department',
244  72424: 'Taxi and Limousine Commission',
245  75531: 'New York City Police Department',
246  77918: 'New York City Police Department',
247  78048: 'Taxi and Limousine Commission',
248  78352: 'Taxi and Limousine Commission',
249  78383: 'New York City Police Department',
250  79078: 'New York City Police Department',
251  84489: 'New York City Police Department',
252  84518: 'New York City Police Department',
253  84688: 'New York City Police Department',
254  84695: 'New York City Police Department',
255  88812: 'Taxi and Limousine Commission',
256  89205: 'New York City Police Department',
257  89382: 'Department of Transportation',
258  89734: 'New York City Police Department',
259  93990: 'Taxi and Limousine Commission',
260  99407: 'Department of Parks and Recreation',
261  99847: 'Taxi and Limousine Commission',
262  100073: 'Taxi and Limousine Commission',
263  101013: 'Taxi and Limousine Commission',
264  104020: 'Taxi and Limousine Commission',
265  106118: 'New York City Police Department',
266  106499: 'Department of Transportation'},
267 'Complaint Type': {2869: 'Illegal Parking',
268  23571: 'Noise - Park',
269  41625: 'For Hire Vehicle Complaint',
270  44331: 'Taxi Complaint',
271  46913: 'Dead Tree',
272  47459: 'Taxi Complaint',
273  48465: 'Illegal Parking',
274  51837: 'Noise - Park',
275  51848: 'Noise - Park',
276  54089: 'Noise - Park',
277  54343: 'Street Condition',
278  55140: 'Noise - Vehicle',
279  57789: 'Taxi Complaint',
280  63119: 'Taxi Complaint',
281  66242: 'Taxi Complaint',
282  66758: 'Vending',
283  66786: 'Taxi Complaint',
284  66809: 'Traffic',
285  67465: 'Traffic',
286  72424: 'Taxi Complaint',
287  75531: 'Vending',
288  77918: 'Noise - Park',
289  78048: 'Taxi Complaint',
290  78352: 'For Hire Vehicle Complaint',
291  78383: 'Noise - Vehicle',
292  79078: 'Noise - Park',
293  84489: 'Noise - Park',
294  84518: 'Noise - Park',
295  84688: 'Noise - Park',
296  84695: 'Noise - Park',
297  88812: 'Taxi Complaint',
298  89205: 'Vending',
299  89382: 'Public Toilet',
300  89734: 'Noise - Park',
301  93990: 'Taxi Complaint',
302  99407: 'Overgrown Tree/Branches',
303  99847: 'Taxi Complaint',
304  100073: 'Taxi Complaint',
305  101013: 'Taxi Complaint',
306  104020: 'For Hire Vehicle Complaint',
307  106118: 'Noise - Park',
308  106499: 'Public Toilet'},
309 'Descriptor': {2869: 'Double Parked Blocking Traffic',
310  23571: 'Loud Music/Party',
311  41625: 'Car Service Company Complaint',
312  44331: 'Driver Complaint',
313  46913: 'Dead/Dying Tree',
314  47459: 'Driver Complaint',
315  48465: 'Posted Parking Sign Violation',
316  51837: 'Loud Music/Party',
317  51848: 'Loud Music/Party',
318  54089: 'Loud Music/Party',
319  54343: 'Rough, Pitted or Cracked Roads',
320  55140: 'Car/Truck Music',
321  57789: 'Driver Complaint',
322  63119: 'Driver Complaint',
323  66242: 'Driver Complaint',
324  66758: 'Unlicensed',
325  66786: 'Insurance Information Requested',
326  66809: 'Congestion/Gridlock',
327  67465: 'Drag Racing',
328  72424: 'Driver Complaint',
329  75531: 'In Prohibited Area',
330  77918: 'Loud Music/Party',
331  78048: 'Driver Complaint',
332  78352: 'Car Service Company Complaint',
333  78383: 'Car/Truck Music',
334  79078: 'Loud Music/Party',
335  84489: 'Loud Music/Party',
336  84518: 'Loud Music/Party',
337  84688: 'Loud Music/Party',
338  84695: 'Loud Music/Party',
339  88812: 'Driver Complaint',
340  89205: 'Unlicensed',
341  89382: 'Damaged Door',
342  89734: 'Loud Music/Party',
343  93990: 'Driver Complaint',
344  99407: 'Traffic Sign or Signal Blocked',
345  99847: 'Driver Complaint',
346  100073: 'Driver Complaint',
347  101013: 'Driver Complaint',
348  104020: 'Car Service Company Complaint',
349  106118: 'Loud Music/Party',
350  106499: 'Dirty/Graffiti'},
351 'Location Type': {2869: 'Street/Sidewalk',
352  23571: 'Park/Playground',
353  41625: 'Street',
354  44331: 'Street',
355  46913: 'Street',
356  47459: 'Street',
357  48465: 'Street/Sidewalk',
358  51837: 'Park/Playground',
359  51848: 'Park/Playground',
360  54089: 'Park/Playground',
361  54343: 'Street',
362  55140: 'Street/Sidewalk',
363  57789: 'Street',
364  63119: 'Street',
365  66242: 'Street',
366  66758: 'Park/Playground',
367  66786: 'Street',
368  66809: 'Street/Sidewalk',
369  67465: 'Street/Sidewalk',
370  72424: 'Street',
371  75531: 'Park/Playground',
372  77918: 'Park/Playground',
373  78048: 'Street',
374  78352: 'Street',
375  78383: 'Street/Sidewalk',
376  79078: 'Park/Playground',
377  84489: 'Park/Playground',
378  84518: 'Park/Playground',
379  84688: 'Park/Playground',
380  84695: 'Park/Playground',
381  88812: 'Street',
382  89205: 'Park/Playground',
383  89382: 'Sidewalk',
384  89734: 'Park/Playground',
385  93990: 'Street',
386  99407: 'Street',
387  99847: 'Street',
388  100073: 'Street',
389  101013: 'Street',
390  104020: 'Street',
391  106118: 'Park/Playground',
392  106499: 'Sidewalk'},
393 'Incident Zip': {2869: '11217.0',
394  23571: '10000',
395  41625: '11430',
396  44331: '11430',
397  46913: '11215',
398  47459: '10031',
399  48465: '11434',
400  51837: '10031.0',
401  51848: '10031.0',
402  54089: '10000.0',
403  54343: '10003.0',
404  55140: '11368.0',
405  57789: '11369.0',
406  63119: '11430.0',
407  66242: '11369',
408  66758: '10036',
409  66786: '10003',
410  66809: '11430',
411  67465: '11367',
412  72424: '11217',
413  75531: '10000',
414  77918: '10000',
415  78048: '11369',
416  78352: '11217',
417  78383: '11368',
418  79078: '10011',
419  84489: '11215',
420  84518: '11215',
421  84688: '11215',
422  84695: '11215',
423  88812: '11430',
424  89205: '10000',
425  89382: '11238',
426  89734: '10036',
427  93990: '10003',
428  99407: '11430.0',
429  99847: '10036.0',
430  100073: '10024.0',
431  101013: '10017.0',
432  104020: '11430.0',
433  106118: '10000.0',
434  106499: '11369.0'},
435 'Address Type': {2869: 'PLACENAME',
436  23571: 'PLACENAME',
437  41625: 'PLACENAME',
438  44331: 'PLACENAME',
439  46913: 'PLACENAME',
440  47459: 'PLACENAME',
441  48465: 'PLACENAME',
442  51837: 'PLACENAME',
443  51848: 'PLACENAME',
444  54089: 'PLACENAME',
445  54343: 'PLACENAME',
446  55140: 'PLACENAME',
447  57789: 'PLACENAME',
448  63119: 'PLACENAME',
449  66242: 'PLACENAME',
450  66758: 'PLACENAME',
451  66786: 'PLACENAME',
452  66809: 'PLACENAME',
453  67465: 'PLACENAME',
454  72424: 'PLACENAME',
455  75531: 'PLACENAME',
456  77918: 'PLACENAME',
457  78048: 'PLACENAME',
458  78352: 'PLACENAME',
459  78383: 'PLACENAME',
460  79078: 'PLACENAME',
461  84489: 'PLACENAME',
462  84518: 'PLACENAME',
463  84688: 'PLACENAME',
464  84695: 'PLACENAME',
465  88812: 'PLACENAME',
466  89205: 'PLACENAME',
467  89382: 'PLACENAME',
468  89734: 'PLACENAME',
469  93990: 'PLACENAME',
470  99407: 'PLACENAME',
471  99847: 'PLACENAME',
472  100073: 'PLACENAME',
473  101013: 'PLACENAME',
474  104020: 'PLACENAME',
475  106118: 'PLACENAME',
476  106499: 'PLACENAME'},
477 'City': {2869: 'BROOKLYN',
478  23571: 'NEW YORK',
479  41625: 'JAMAICA',
480  44331: 'JAMAICA',
481  46913: 'BROOKLYN',
482  47459: 'NEW YORK',
483  48465: 'JAMAICA',
484  51837: 'NEW YORK',
485  51848: 'NEW YORK',
486  54089: 'NEW YORK',
487  54343: 'NEW YORK',
488  55140: 'CORONA',
489  57789: 'EAST ELMHURST',
490  63119: 'JAMAICA',
491  66242: 'EAST ELMHURST',
492  66758: 'NEW YORK',
493  66786: 'NEW YORK',
494  66809: 'JAMAICA',
495  67465: 'FLUSHING',
496  72424: 'BROOKLYN',
497  75531: 'NEW YORK',
498  77918: 'NEW YORK',
499  78048: 'EAST ELMHURST',
500  78352: 'BROOKLYN',
501  78383: 'CORONA',
502  79078: 'NEW YORK',
503  84489: 'BROOKLYN',
504  84518: 'BROOKLYN',
505  84688: 'BROOKLYN',
506  84695: 'BROOKLYN',
507  88812: 'JAMAICA',
508  89205: 'NEW YORK',
509  89382: 'BROOKLYN',
510  89734: 'NEW YORK',
511  93990: 'NEW YORK',
512  99407: 'JAMAICA',
513  99847: 'NEW YORK',
514  100073: 'NEW YORK',
515  101013: 'NEW YORK',
516  104020: 'JAMAICA',
517  106118: 'NEW YORK',
518  106499: 'EAST ELMHURST'},
519 'Landmark': {2869: 'BARCLAYS CENTER',
520  23571: 'CENTRAL PARK',
521  41625: 'J F K AIRPORT',
522  44331: 'J F K AIRPORT',
523  46913: 'BARTEL PRITCHARD SQUARE',
524  47459: 'CITY COLLEGE',
525  48465: 'PS 37',
526  51837: 'JACKIE ROBINSON PARK',
527  51848: 'JACKIE ROBINSON PARK',
528  54089: 'CENTRAL PARK',
529  54343: 'UNION SQUARE PARK',
530  55140: 'WORLDS FAIR MARINA',
531  57789: 'LA GUARDIA AIRPORT',
532  63119: 'J F K AIRPORT',
533  66242: 'LA GUARDIA AIRPORT',
534  66758: 'BRYANT PARK',
535  66786: 'BETH ISRAEL MED CENTER',
536  66809: 'J F K AIRPORT',
537  67465: 'QUEENS COLLEGE',
538  72424: 'BARCLAYS CENTER',
539  75531: 'CENTRAL PARK',
540  77918: 'CENTRAL PARK',
541  78048: 'LA GUARDIA AIRPORT',
542  78352: 'BARCLAYS CENTER',
543  78383: 'WORLDS FAIR MARINA',
544  79078: 'WASHINGTON SQUARE PARK',
545  84489: 'PROSPECT PARK',
546  84518: 'PROSPECT PARK',
547  84688: 'PROSPECT PARK',
548  84695: 'PROSPECT PARK',
549  88812: 'J F K AIRPORT',
550  89205: 'CENTRAL PARK',
551  89382: 'GRAND ARMY PLAZA',
552  89734: 'BRYANT PARK',
553  93990: 'BETH ISRAEL MED CENTER',
554  99407: 'J F K AIRPORT',
555  99847: 'PORT AUTH 42 STREET',
556  100073: 'MUSEUM NATURAL HIST',
557  101013: 'GRAND CENTRAL TERM',
558  104020: 'JFK',
559  106118: 'CENTRAL PARK',
560  106499: 'LA GUARDIA AIRPORT'},
561 'Status': {2869: 'Closed',
562  23571: 'Closed',
563  41625: 'Closed',
564  44331: 'Closed',
565  46913: 'Closed',
566  47459: 'Closed',
567  48465: 'Closed',
568  51837: 'Closed',
569  51848: 'Closed',
570  54089: 'Closed',
571  54343: 'Closed',
572  55140: 'Closed',
573  57789: 'Closed',
574  63119: 'Closed',
575  66242: 'Closed',
576  66758: 'Closed',
577  66786: 'Closed',
578  66809: 'Closed',
579  67465: 'Closed',
580  72424: 'Closed',
581  75531: 'Closed',
582  77918: 'Closed',
583  78048: 'Closed',
584  78352: 'Closed',
585  78383: 'Closed',
586  79078: 'Closed',
587  84489: 'Closed',
588  84518: 'Closed',
589  84688: 'Closed',
590  84695: 'Closed',
591  88812: 'Closed',
592  89205: 'Closed',
593  89382: 'Closed',
594  89734: 'Closed',
595  93990: 'Closed',
596  99407: 'Closed',
597  99847: 'Closed',
598  100073: 'Closed',
599  101013: 'Closed',
600  104020: 'Closed',
601  106118: 'Closed',
602  106499: 'Closed'},
603 'Borough': {2869: 'BROOKLYN',
604  23571: 'MANHATTAN',
605  41625: 'QUEENS',
606  44331: 'QUEENS',
607  46913: 'BROOKLYN',
608  47459: 'MANHATTAN',
609  48465: 'QUEENS',
610  51837: 'MANHATTAN',
611  51848: 'MANHATTAN',
612  54089: 'MANHATTAN',
613  54343: 'MANHATTAN',
614  55140: 'QUEENS',
615  57789: 'QUEENS',
616  63119: 'QUEENS',
617  66242: 'QUEENS',
618  66758: 'MANHATTAN',
619  66786: 'MANHATTAN',
620  66809: 'QUEENS',
621  67465: 'QUEENS',
622  72424: 'BROOKLYN',
623  75531: 'MANHATTAN',
624  77918: 'MANHATTAN',
625  78048: 'QUEENS',
626  78352: 'BROOKLYN',
627  78383: 'QUEENS',
628  79078: 'MANHATTAN',
629  84489: 'BROOKLYN',
630  84518: 'BROOKLYN',
631  84688: 'BROOKLYN',
632  84695: 'BROOKLYN',
633  88812: 'QUEENS',
634  89205: 'MANHATTAN',
635  89382: 'BROOKLYN',
636  89734: 'MANHATTAN',
637  93990: 'MANHATTAN',
638  99407: 'QUEENS',
639  99847: 'MANHATTAN',
640  100073: 'MANHATTAN',
641  101013: 'MANHATTAN',
642  104020: 'QUEENS',
643  106118: 'MANHATTAN',
644  106499: 'QUEENS'}}
645

ANSWER

Answered 2021-Dec-22 at 17:00

You can first start by converting your date columns to datetime type using pd.to_datetime():

1Unnamed: 0     Created Date             Closed Date            Agency   Agency Name  Complaint Type Descriptor  Location Type   Incident Zip    Address Type    City    Landmark    Status  Borough
22869    2869    10/30/2013 09:14:47 AM  10/30/2013 10:48:51 AM  NYPD    New York City Police Department Illegal Parking Double Parked Blocking Traffic  Street/Sidewalk 11217.0 PLACENAME   BROOKLYN    BARCLAYS CENTER Closed  BROOKLYN
323571   23571   10/25/2013 02:33:54 PM  10/25/2013 03:36:36 PM  NYPD    New York City Police Department Noise - Park    Loud Music/Party    Park/Playground 10000   PLACENAME   NEW YORK    CENTRAL PARK    Closed  MANHATTAN
441625   41625   10/22/2013 09:33:56 PM  10/24/2013 05:37:24 PM  TLC Taxi and Limousine Commission   For Hire Vehicle Complaint  Car Service Company Complaint   Street  11430   PLACENAME   JAMAICA J F K AIRPORT   Closed  QUEENS
544331   44331   10/22/2013 07:25:35 AM  10/25/2013 10:40:35 AM  TLC Taxi and Limousine Commission   Taxi Complaint  Driver Complaint    Street  11430   PLACENAME   JAMAICA J F K AIRPORT   Closed  QUEENS
646913   46913   10/21/2013 05:03:26 PM  10/23/2013 09:59:23 AM  DPR Department of Parks and Recreation  Dead Tree   Dead/Dying Tree Street  11215   PLACENAME   BROOKLYN    BARTEL PRITCHARD SQUARE Closed  BROOKLYN
747459   47459   10/21/2013 02:56:08 PM  10/29/2013 06:17:10 PM  TLC Taxi and Limousine Commission   Taxi Complaint  Driver Complaint    Street  10031   PLACENAME   NEW YORK    CITY COLLEGE    Closed  MANHATTAN
848465   48465   10/21/2013 10:44:10 AM  10/21/2013 11:17:47 AM  NYPD    New York City Police Department Illegal Parking Posted Parking Sign Violation   Street/Sidewalk 11434   PLACENAME   JAMAICA PS 37   Closed  QUEENS
951837   51837   10/20/2013 04:36:12 PM  10/20/2013 06:35:49 PM  NYPD    New York City Police Department Noise - Park    Loud Music/Party    Park/Playground 10031.0 PLACENAME   NEW YORK    JACKIE ROBINSON PARK    Closed  MANHATTAN
1051848   51848   10/20/2013 04:26:03 PM  10/20/2013 06:34:47 PM  NYPD    New York City Police Department Noise - Park    Loud Music/Party    Park/Playground 10031.0 PLACENAME   NEW YORK    JACKIE ROBINSON PARK    Closed  MANHATTAN
1154089   54089   10/19/2013 03:45:47 PM  10/19/2013 04:10:11 PM  NYPD    New York City Police Department Noise - Park    Loud Music/Party    Park/Playground 10000.0 PLACENAME   NEW YORK    CENTRAL PARK    Closed  MANHATTAN
1254343   54343   10/19/2013 01:27:43 PM  10/28/2013 08:42:12 AM  DOT Department of Transportation    Street Condition    Rough, Pitted or Cracked Roads  Street  10003.0 PLACENAME   NEW YORK    UNION SQUARE PARK   Closed  MANHATTAN
1355140   55140   10/19/2013 02:02:28 AM  10/19/2013 02:19:55 AM  NYPD    New York City Police Department Noise - Vehicle Car/Truck Music Street/Sidewalk 11368.0 PLACENAME   CORONA  WORLDS FAIR MARINA  Closed  QUEENS
1457789   57789   10/18/2013 11:55:44 AM  10/23/2013 02:42:14 PM  TLC Taxi and Limousine Commission   Taxi Complaint  Driver Complaint    Street  11369.0 PLACENAME   EAST ELMHURST   LA GUARDIA AIRPORT  Closed  QUEENS
1563119   63119   10/17/2013 06:52:37 AM  10/25/2013 06:49:59 PM  TLC Taxi and Limousine Commission   Taxi Complaint  Driver Complaint    Street  11430.0 PLACENAME   JAMAICA J F K AIRPORT   Closed  QUEENS
1666242   66242   10/16/2013 01:56:24 PM  10/22/2013 03:09:11 PM  TLC Taxi and Limousine Commission   Taxi Complaint  Driver Complaint    Street  11369   PLACENAME   EAST ELMHURST   LA GUARDIA AIRPORT  Closed  QUEENS
1766758   66758   10/16/2013 11:52:43 AM  10/16/2013 04:35:34 PM  NYPD    New York City Police Department Vending Unlicensed  Park/Playground 10036   PLACENAME   NEW YORK    BRYANT PARK Closed  MANHATTAN
1866786   66786   10/16/2013 11:42:23 AM  10/18/2013 04:57:04 PM  TLC Taxi and Limousine Commission   Taxi Complaint  Insurance Information Requested Street  10003   PLACENAME   NEW YORK    BETH ISRAEL MED CENTER  Closed  MANHATTAN
1966809   66809   10/16/2013 11:36:54 AM  10/16/2013 12:34:23 PM  NYPD    New York City Police Department Traffic Congestion/Gridlock Street/Sidewalk 11430   PLACENAME   JAMAICA J F K AIRPORT   Closed  QUEENS
2067465   67465   10/16/2013 09:14:35 AM  10/16/2013 12:43:06 PM  NYPD    New York City Police Department Traffic Drag Racing Street/Sidewalk 11367   PLACENAME   FLUSHING    QUEENS COLLEGE  Closed  QUEENS
2172424   72424   10/15/2013 12:22:00 AM  10/21/2013 12:16:15 PM  TLC Taxi and Limousine Commission   Taxi Complaint  Driver Complaint    Street  11217   PLACENAME   BROOKLYN    BARCLAYS CENTER Closed  BROOKLYN
2275531   75531   10/14/2013 10:59:20 AM  10/14/2013 03:09:51 PM  NYPD    New York City Police Department Vending In Prohibited Area  Park/Playground 10000   PLACENAME   NEW YORK    CENTRAL PARK    Closed  MANHATTAN
2377918   77918   10/13/2013 03:16:03 PM  10/13/2013 03:25:45 PM  NYPD    New York City Police Department Noise - Park    Loud Music/Party    Park/Playground 10000   PLACENAME   NEW YORK    CENTRAL PARK    Closed  MANHATTAN
2478048   78048   10/13/2013 01:06:02 PM  10/21/2013 10:20:21 AM  TLC Taxi and Limousine Commission   Taxi Complaint  Driver Complaint    Street  11369   PLACENAME   EAST ELMHURST   LA GUARDIA AIRPORT  Closed  QUEENS
2578352   78352   10/13/2013 05:14:33 AM  10/16/2013 01:42:42 PM  TLC Taxi and Limousine Commission   For Hire Vehicle Complaint  Car Service Company Complaint   Street  11217   PLACENAME   BROOKLYN    BARCLAYS CENTER Closed  BROOKLYN
2678383   78383   10/13/2013 03:50:02 AM  10/13/2013 05:03:13 AM  NYPD    New York City Police Department Noise - Vehicle Car/Truck Music Street/Sidewalk 11368   PLACENAME   CORONA  WORLDS FAIR MARINA  Closed  QUEENS
2779078   79078   10/12/2013 09:53:17 PM  10/13/2013 02:52:07 AM  NYPD    New York City Police Department Noise - Park    Loud Music/Party    Park/Playground 10011   PLACENAME   NEW YORK    WASHINGTON SQUARE PARK  Closed  MANHATTAN
2884489   84489   10/10/2013 07:16:16 PM  10/10/2013 10:29:16 PM  NYPD    New York City Police Department Noise - Park    Loud Music/Party    Park/Playground 11215   PLACENAME   BROOKLYN    PROSPECT PARK   Closed  BROOKLYN
2984518   84518   10/10/2013 07:02:29 PM  10/10/2013 10:29:16 PM  NYPD    New York City Police Department Noise - Park    Loud Music/Party    Park/Playground 11215   PLACENAME   BROOKLYN    PROSPECT PARK   Closed  BROOKLYN
3084688   84688   10/10/2013 05:39:19 PM  10/10/2013 10:29:17 PM  NYPD    New York City Police Department Noise - Park    Loud Music/Party    Park/Playground 11215   PLACENAME   BROOKLYN    PROSPECT PARK   Closed  BROOKLYN
3184695   84695   10/10/2013 05:37:04 PM  10/10/2013 10:30:19 PM  NYPD    New York City Police Department Noise - Park    Loud Music/Party    Park/Playground 11215   PLACENAME   BROOKLYN    PROSPECT PARK   Closed  BROOKLYN
3288812   88812   10/09/2013 09:17:15 PM  10/23/2013 02:15:21 PM  TLC Taxi and Limousine Commission   Taxi Complaint  Driver Complaint    Street  11430   PLACENAME   JAMAICA J F K AIRPORT   Closed  QUEENS
3389205   89205   10/09/2013 06:01:48 PM  10/09/2013 09:04:26 PM  NYPD    New York City Police Department Vending Unlicensed  Park/Playground 10000   PLACENAME   NEW YORK    CENTRAL PARK    Closed  MANHATTAN
3489382   89382   10/09/2013 04:53:01 PM  10/18/2013 08:35:02 AM  DOT Department of Transportation    Public Toilet   Damaged Door    Sidewalk    11238   PLACENAME   BROOKLYN    GRAND ARMY PLAZA    Closed  BROOKLYN
3589734   89734   10/09/2013 03:13:23 PM  10/09/2013 05:10:45 PM  NYPD    New York City Police Department Noise - Park    Loud Music/Party    Park/Playground 10036   PLACENAME   NEW YORK    BRYANT PARK Closed  MANHATTAN
3693990   93990   10/08/2013 06:14:15 PM  10/09/2013 04:00:59 PM  TLC Taxi and Limousine Commission   Taxi Complaint  Driver Complaint    Street  10003   PLACENAME   NEW YORK    BETH ISRAEL MED CENTER  Closed  MANHATTAN
3799407   99407   10/07/2013 03:56:11 PM  10/08/2013 07:04:14 AM  DPR Department of Parks and Recreation  Overgrown Tree/Branches Traffic Sign or Signal Blocked  Street  11430.0 PLACENAME   JAMAICA J F K AIRPORT   Closed  QUEENS
3899847   99847   10/07/2013 02:33:21 PM  10/09/2013 02:36:42 PM  TLC Taxi and Limousine Commission   Taxi Complaint  Driver Complaint    Street  10036.0 PLACENAME   NEW YORK    PORT AUTH 42 STREET Closed  MANHATTAN
39100073  100073  10/07/2013 01:36:02 PM  10/09/2013 09:56:55 AM  TLC Taxi and Limousine Commission   Taxi Complaint  Driver Complaint    Street  10024.0 PLACENAME   NEW YORK    MUSEUM NATURAL HIST Closed  MANHATTAN
40101013  101013  10/07/2013 10:05:18 AM  10/09/2013 03:36:23 PM  TLC Taxi and Limousine Commission   Taxi Complaint  Driver Complaint    Street  10017.0 PLACENAME   NEW YORK    GRAND CENTRAL TERM  Closed  MANHATTAN
41104020  104020  10/06/2013 02:58:47 PM  10/07/2013 12:11:16 PM  TLC Taxi and Limousine Commission   For Hire Vehicle Complaint  Car Service Company Complaint   Street  11430.0 PLACENAME   JAMAICA JFK Closed  QUEENS
42106118  106118  10/05/2013 03:24:47 PM  10/05/2013 04:20:34 PM  NYPD    New York City Police Department Noise - Park    Loud Music/Party    Park/Playground 10000.0 PLACENAME   NEW YORK    CENTRAL PARK    Closed  MANHATTAN
43106499  106499  10/05/2013 11:52:13 AM  10/07/2013 08:00:28 AM  DOT Department of Transportation    Public Toilet   Dirty/Graffiti  Sidewalk    11369.0 PLACENAME   EAST ELMHURST   LA GUARDIA AIRPORT  Closed  QUEENS
44pd.Timedelta(task3['Closed Date'] - task3['Created Date']).seconds / 60.0
45                         mean                   count
46complaint Type           closing_time_hours     closing_time_hours
47Blocked Driveway         3.00                   4581
48DOF Literature Request   30.16                  5481
49General Construction     66.38                  798
50Heating                  54.88                  6704
51Illegal Parking          3.08                   3336
52Nonconst                 65                     100 
53Paint-Plaster            49                     3281 
54Plumbing                 65                     666   
55Strret Condition         81                     2610    
56Street Light Condition   90                     4207
57{'Unnamed: 0': {2869: 2869,
58  23571: 23571,
59  41625: 41625,
60  44331: 44331,
61  46913: 46913,
62  47459: 47459,
63  48465: 48465,
64  51837: 51837,
65  51848: 51848,
66  54089: 54089,
67  54343: 54343,
68  55140: 55140,
69  57789: 57789,
70  63119: 63119,
71  66242: 66242,
72  66758: 66758,
73  66786: 66786,
74  66809: 66809,
75  67465: 67465,
76  72424: 72424,
77  75531: 75531,
78  77918: 77918,
79  78048: 78048,
80  78352: 78352,
81  78383: 78383,
82  79078: 79078,
83  84489: 84489,
84  84518: 84518,
85  84688: 84688,
86  84695: 84695,
87  88812: 88812,
88  89205: 89205,
89  89382: 89382,
90  89734: 89734,
91  93990: 93990,
92  99407: 99407,
93  99847: 99847,
94  100073: 100073,
95  101013: 101013,
96  104020: 104020,
97  106118: 106118,
98  106499: 106499},
99 'Created Date': {2869: '10/30/2013 09:14:47 AM',
100  23571: '10/25/2013 02:33:54 PM',
101  41625: '10/22/2013 09:33:56 PM',
102  44331: '10/22/2013 07:25:35 AM',
103  46913: '10/21/2013 05:03:26 PM',
104  47459: '10/21/2013 02:56:08 PM',
105  48465: '10/21/2013 10:44:10 AM',
106  51837: '10/20/2013 04:36:12 PM',
107  51848: '10/20/2013 04:26:03 PM',
108  54089: '10/19/2013 03:45:47 PM',
109  54343: '10/19/2013 01:27:43 PM',
110  55140: '10/19/2013 02:02:28 AM',
111  57789: '10/18/2013 11:55:44 AM',
112  63119: '10/17/2013 06:52:37 AM',
113  66242: '10/16/2013 01:56:24 PM',
114  66758: '10/16/2013 11:52:43 AM',
115  66786: '10/16/2013 11:42:23 AM',
116  66809: '10/16/2013 11:36:54 AM',
117  67465: '10/16/2013 09:14:35 AM',
118  72424: '10/15/2013 12:22:00 AM',
119  75531: '10/14/2013 10:59:20 AM',
120  77918: '10/13/2013 03:16:03 PM',
121  78048: '10/13/2013 01:06:02 PM',
122  78352: '10/13/2013 05:14:33 AM',
123  78383: '10/13/2013 03:50:02 AM',
124  79078: '10/12/2013 09:53:17 PM',
125  84489: '10/10/2013 07:16:16 PM',
126  84518: '10/10/2013 07:02:29 PM',
127  84688: '10/10/2013 05:39:19 PM',
128  84695: '10/10/2013 05:37:04 PM',
129  88812: '10/09/2013 09:17:15 PM',
130  89205: '10/09/2013 06:01:48 PM',
131  89382: '10/09/2013 04:53:01 PM',
132  89734: '10/09/2013 03:13:23 PM',
133  93990: '10/08/2013 06:14:15 PM',
134  99407: '10/07/2013 03:56:11 PM',
135  99847: '10/07/2013 02:33:21 PM',
136  100073: '10/07/2013 01:36:02 PM',
137  101013: '10/07/2013 10:05:18 AM',
138  104020: '10/06/2013 02:58:47 PM',
139  106118: '10/05/2013 03:24:47 PM',
140  106499: '10/05/2013 11:52:13 AM'},
141 'Closed Date': {2869: '10/30/2013 10:48:51 AM',
142  23571: '10/25/2013 03:36:36 PM',
143  41625: '10/24/2013 05:37:24 PM',
144  44331: '10/25/2013 10:40:35 AM',
145  46913: '10/23/2013 09:59:23 AM',
146  47459: '10/29/2013 06:17:10 PM',
147  48465: '10/21/2013 11:17:47 AM',
148  51837: '10/20/2013 06:35:49 PM',
149  51848: '10/20/2013 06:34:47 PM',
150  54089: '10/19/2013 04:10:11 PM',
151  54343: '10/28/2013 08:42:12 AM',
152  55140: '10/19/2013 02:19:55 AM',
153  57789: '10/23/2013 02:42:14 PM',
154  63119: '10/25/2013 06:49:59 PM',
155  66242: '10/22/2013 03:09:11 PM',
156  66758: '10/16/2013 04:35:34 PM',
157  66786: '10/18/2013 04:57:04 PM',
158  66809: '10/16/2013 12:34:23 PM',
159  67465: '10/16/2013 12:43:06 PM',
160  72424: '10/21/2013 12:16:15 PM',
161  75531: '10/14/2013 03:09:51 PM',
162  77918: '10/13/2013 03:25:45 PM',
163  78048: '10/21/2013 10:20:21 AM',
164  78352: '10/16/2013 01:42:42 PM',
165  78383: '10/13/2013 05:03:13 AM',
166  79078: '10/13/2013 02:52:07 AM',
167  84489: '10/10/2013 10:29:16 PM',
168  84518: '10/10/2013 10:29:16 PM',
169  84688: '10/10/2013 10:29:17 PM',
170  84695: '10/10/2013 10:30:19 PM',
171  88812: '10/23/2013 02:15:21 PM',
172  89205: '10/09/2013 09:04:26 PM',
173  89382: '10/18/2013 08:35:02 AM',
174  89734: '10/09/2013 05:10:45 PM',
175  93990: '10/09/2013 04:00:59 PM',
176  99407: '10/08/2013 07:04:14 AM',
177  99847: '10/09/2013 02:36:42 PM',
178  100073: '10/09/2013 09:56:55 AM',
179  101013: '10/09/2013 03:36:23 PM',
180  104020: '10/07/2013 12:11:16 PM',
181  106118: '10/05/2013 04:20:34 PM',
182  106499: '10/07/2013 08:00:28 AM'},
183 'Agency': {2869: 'NYPD',
184  23571: 'NYPD',
185  41625: 'TLC',
186  44331: 'TLC',
187  46913: 'DPR',
188  47459: 'TLC',
189  48465: 'NYPD',
190  51837: 'NYPD',
191  51848: 'NYPD',
192  54089: 'NYPD',
193  54343: 'DOT',
194  55140: 'NYPD',
195  57789: 'TLC',
196  63119: 'TLC',
197  66242: 'TLC',
198  66758: 'NYPD',
199  66786: 'TLC',
200  66809: 'NYPD',
201  67465: 'NYPD',
202  72424: 'TLC',
203  75531: 'NYPD',
204  77918: 'NYPD',
205  78048: 'TLC',
206  78352: 'TLC',
207  78383: 'NYPD',
208  79078: 'NYPD',
209  84489: 'NYPD',
210  84518: 'NYPD',
211  84688: 'NYPD',
212  84695: 'NYPD',
213  88812: 'TLC',
214  89205: 'NYPD',
215  89382: 'DOT',
216  89734: 'NYPD',
217  93990: 'TLC',
218  99407: 'DPR',
219  99847: 'TLC',
220  100073: 'TLC',
221  101013: 'TLC',
222  104020: 'TLC',
223  106118: 'NYPD',
224  106499: 'DOT'},
225 'Agency Name': {2869: 'New York City Police Department',
226  23571: 'New York City Police Department',
227  41625: 'Taxi and Limousine Commission',
228  44331: 'Taxi and Limousine Commission',
229  46913: 'Department of Parks and Recreation',
230  47459: 'Taxi and Limousine Commission',
231  48465: 'New York City Police Department',
232  51837: 'New York City Police Department',
233  51848: 'New York City Police Department',
234  54089: 'New York City Police Department',
235  54343: 'Department of Transportation',
236  55140: 'New York City Police Department',
237  57789: 'Taxi and Limousine Commission',
238  63119: 'Taxi and Limousine Commission',
239  66242: 'Taxi and Limousine Commission',
240  66758: 'New York City Police Department',
241  66786: 'Taxi and Limousine Commission',
242  66809: 'New York City Police Department',
243  67465: 'New York City Police Department',
244  72424: 'Taxi and Limousine Commission',
245  75531: 'New York City Police Department',
246  77918: 'New York City Police Department',
247  78048: 'Taxi and Limousine Commission',
248  78352: 'Taxi and Limousine Commission',
249  78383: 'New York City Police Department',
250  79078: 'New York City Police Department',
251  84489: 'New York City Police Department',
252  84518: 'New York City Police Department',
253  84688: 'New York City Police Department',
254  84695: 'New York City Police Department',
255  88812: 'Taxi and Limousine Commission',
256  89205: 'New York City Police Department',
257  89382: 'Department of Transportation',
258  89734: 'New York City Police Department',
259  93990: 'Taxi and Limousine Commission',
260  99407: 'Department of Parks and Recreation',
261  99847: 'Taxi and Limousine Commission',
262  100073: 'Taxi and Limousine Commission',
263  101013: 'Taxi and Limousine Commission',
264  104020: 'Taxi and Limousine Commission',
265  106118: 'New York City Police Department',
266  106499: 'Department of Transportation'},
267 'Complaint Type': {2869: 'Illegal Parking',
268  23571: 'Noise - Park',
269  41625: 'For Hire Vehicle Complaint',
270  44331: 'Taxi Complaint',
271  46913: 'Dead Tree',
272  47459: 'Taxi Complaint',
273  48465: 'Illegal Parking',
274  51837: 'Noise - Park',
275  51848: 'Noise - Park',
276  54089: 'Noise - Park',
277  54343: 'Street Condition',
278  55140: 'Noise - Vehicle',
279  57789: 'Taxi Complaint',
280  63119: 'Taxi Complaint',
281  66242: 'Taxi Complaint',
282  66758: 'Vending',
283  66786: 'Taxi Complaint',
284  66809: 'Traffic',
285  67465: 'Traffic',
286  72424: 'Taxi Complaint',
287  75531: 'Vending',
288  77918: 'Noise - Park',
289  78048: 'Taxi Complaint',
290  78352: 'For Hire Vehicle Complaint',
291  78383: 'Noise - Vehicle',
292  79078: 'Noise - Park',
293  84489: 'Noise - Park',
294  84518: 'Noise - Park',
295  84688: 'Noise - Park',
296  84695: 'Noise - Park',
297  88812: 'Taxi Complaint',
298  89205: 'Vending',
299  89382: 'Public Toilet',
300  89734: 'Noise - Park',
301  93990: 'Taxi Complaint',
302  99407: 'Overgrown Tree/Branches',
303  99847: 'Taxi Complaint',
304  100073: 'Taxi Complaint',
305  101013: 'Taxi Complaint',
306  104020: 'For Hire Vehicle Complaint',
307  106118: 'Noise - Park',
308  106499: 'Public Toilet'},
309 'Descriptor': {2869: 'Double Parked Blocking Traffic',
310  23571: 'Loud Music/Party',
311  41625: 'Car Service Company Complaint',
312  44331: 'Driver Complaint',
313  46913: 'Dead/Dying Tree',
314  47459: 'Driver Complaint',
315  48465: 'Posted Parking Sign Violation',
316  51837: 'Loud Music/Party',
317  51848: 'Loud Music/Party',
318  54089: 'Loud Music/Party',
319  54343: 'Rough, Pitted or Cracked Roads',
320  55140: 'Car/Truck Music',
321  57789: 'Driver Complaint',
322  63119: 'Driver Complaint',
323  66242: 'Driver Complaint',
324  66758: 'Unlicensed',
325  66786: 'Insurance Information Requested',
326  66809: 'Congestion/Gridlock',
327  67465: 'Drag Racing',
328  72424: 'Driver Complaint',
329  75531: 'In Prohibited Area',
330  77918: 'Loud Music/Party',
331  78048: 'Driver Complaint',
332  78352: 'Car Service Company Complaint',
333  78383: 'Car/Truck Music',
334  79078: 'Loud Music/Party',
335  84489: 'Loud Music/Party',
336  84518: 'Loud Music/Party',
337  84688: 'Loud Music/Party',
338  84695: 'Loud Music/Party',
339  88812: 'Driver Complaint',
340  89205: 'Unlicensed',
341  89382: 'Damaged Door',
342  89734: 'Loud Music/Party',
343  93990: 'Driver Complaint',
344  99407: 'Traffic Sign or Signal Blocked',
345  99847: 'Driver Complaint',
346  100073: 'Driver Complaint',
347  101013: 'Driver Complaint',
348  104020: 'Car Service Company Complaint',
349  106118: 'Loud Music/Party',
350  106499: 'Dirty/Graffiti'},
351 'Location Type': {2869: 'Street/Sidewalk',
352  23571: 'Park/Playground',
353  41625: 'Street',
354  44331: 'Street',
355  46913: 'Street',
356  47459: 'Street',
357  48465: 'Street/Sidewalk',
358  51837: 'Park/Playground',
359  51848: 'Park/Playground',
360  54089: 'Park/Playground',
361  54343: 'Street',
362  55140: 'Street/Sidewalk',
363  57789: 'Street',
364  63119: 'Street',
365  66242: 'Street',
366  66758: 'Park/Playground',
367  66786: 'Street',
368  66809: 'Street/Sidewalk',
369  67465: 'Street/Sidewalk',
370  72424: 'Street',
371  75531: 'Park/Playground',
372  77918: 'Park/Playground',
373  78048: 'Street',
374  78352: 'Street',
375  78383: 'Street/Sidewalk',
376  79078: 'Park/Playground',
377  84489: 'Park/Playground',
378  84518: 'Park/Playground',
379  84688: 'Park/Playground',
380  84695: 'Park/Playground',
381  88812: 'Street',
382  89205: 'Park/Playground',
383  89382: 'Sidewalk',
384  89734: 'Park/Playground',
385  93990: 'Street',
386  99407: 'Street',
387  99847: 'Street',
388  100073: 'Street',
389  101013: 'Street',
390  104020: 'Street',
391  106118: 'Park/Playground',
392  106499: 'Sidewalk'},
393 'Incident Zip': {2869: '11217.0',
394  23571: '10000',
395  41625: '11430',
396  44331: '11430',
397  46913: '11215',
398  47459: '10031',
399  48465: '11434',
400  51837: '10031.0',
401  51848: '10031.0',
402  54089: '10000.0',
403  54343: '10003.0',
404  55140: '11368.0',
405  57789: '11369.0',
406  63119: '11430.0',
407  66242: '11369',
408  66758: '10036',
409  66786: '10003',
410  66809: '11430',
411  67465: '11367',
412  72424: '11217',
413  75531: '10000',
414  77918: '10000',
415  78048: '11369',
416  78352: '11217',
417  78383: '11368',
418  79078: '10011',
419  84489: '11215',
420  84518: '11215',
421  84688: '11215',
422  84695: '11215',
423  88812: '11430',
424  89205: '10000',
425  89382: '11238',
426  89734: '10036',
427  93990: '10003',
428  99407: '11430.0',
429  99847: '10036.0',
430  100073: '10024.0',
431  101013: '10017.0',
432  104020: '11430.0',
433  106118: '10000.0',
434  106499: '11369.0'},
435 'Address Type': {2869: 'PLACENAME',
436  23571: 'PLACENAME',
437  41625: 'PLACENAME',
438  44331: 'PLACENAME',
439  46913: 'PLACENAME',
440  47459: 'PLACENAME',
441  48465: 'PLACENAME',
442  51837: 'PLACENAME',
443  51848: 'PLACENAME',
444  54089: 'PLACENAME',
445  54343: 'PLACENAME',
446  55140: 'PLACENAME',
447  57789: 'PLACENAME',
448  63119: 'PLACENAME',
449  66242: 'PLACENAME',
450  66758: 'PLACENAME',
451  66786: 'PLACENAME',
452  66809: 'PLACENAME',
453  67465: 'PLACENAME',
454  72424: 'PLACENAME',
455  75531: 'PLACENAME',
456  77918: 'PLACENAME',
457  78048: 'PLACENAME',
458  78352: 'PLACENAME',
459  78383: 'PLACENAME',
460  79078: 'PLACENAME',
461  84489: 'PLACENAME',
462  84518: 'PLACENAME',
463  84688: 'PLACENAME',
464  84695: 'PLACENAME',
465  88812: 'PLACENAME',
466  89205: 'PLACENAME',
467  89382: 'PLACENAME',
468  89734: 'PLACENAME',
469  93990: 'PLACENAME',
470  99407: 'PLACENAME',
471  99847: 'PLACENAME',
472  100073: 'PLACENAME',
473  101013: 'PLACENAME',
474  104020: 'PLACENAME',
475  106118: 'PLACENAME',
476  106499: 'PLACENAME'},
477 'City': {2869: 'BROOKLYN',
478  23571: 'NEW YORK',
479  41625: 'JAMAICA',
480  44331: 'JAMAICA',
481  46913: 'BROOKLYN',
482  47459: 'NEW YORK',
483  48465: 'JAMAICA',
484  51837: 'NEW YORK',
485  51848: 'NEW YORK',
486  54089: 'NEW YORK',
487  54343: 'NEW YORK',
488  55140: 'CORONA',
489  57789: 'EAST ELMHURST',
490  63119: 'JAMAICA',
491  66242: 'EAST ELMHURST',
492  66758: 'NEW YORK',
493  66786: 'NEW YORK',
494  66809: 'JAMAICA',
495  67465: 'FLUSHING',
496  72424: 'BROOKLYN',
497  75531: 'NEW YORK',
498  77918: 'NEW YORK',
499  78048: 'EAST ELMHURST',
500  78352: 'BROOKLYN',
501  78383: 'CORONA',
502  79078: 'NEW YORK',
503  84489: 'BROOKLYN',
504  84518: 'BROOKLYN',
505  84688: 'BROOKLYN',
506  84695: 'BROOKLYN',
507  88812: 'JAMAICA',
508  89205: 'NEW YORK',
509  89382: 'BROOKLYN',
510  89734: 'NEW YORK',
511  93990: 'NEW YORK',
512  99407: 'JAMAICA',
513  99847: 'NEW YORK',
514  100073: 'NEW YORK',
515  101013: 'NEW YORK',
516  104020: 'JAMAICA',
517  106118: 'NEW YORK',
518  106499: 'EAST ELMHURST'},
519 'Landmark': {2869: 'BARCLAYS CENTER',
520  23571: 'CENTRAL PARK',
521  41625: 'J F K AIRPORT',
522  44331: 'J F K AIRPORT',
523  46913: 'BARTEL PRITCHARD SQUARE',
524  47459: 'CITY COLLEGE',
525  48465: 'PS 37',
526  51837: 'JACKIE ROBINSON PARK',
527  51848: 'JACKIE ROBINSON PARK',
528  54089: 'CENTRAL PARK',
529  54343: 'UNION SQUARE PARK',
530  55140: 'WORLDS FAIR MARINA',
531  57789: 'LA GUARDIA AIRPORT',
532  63119: 'J F K AIRPORT',
533  66242: 'LA GUARDIA AIRPORT',
534  66758: 'BRYANT PARK',
535  66786: 'BETH ISRAEL MED CENTER',
536  66809: 'J F K AIRPORT',
537  67465: 'QUEENS COLLEGE',
538  72424: 'BARCLAYS CENTER',
539  75531: 'CENTRAL PARK',
540  77918: 'CENTRAL PARK',
541  78048: 'LA GUARDIA AIRPORT',
542  78352: 'BARCLAYS CENTER',
543  78383: 'WORLDS FAIR MARINA',
544  79078: 'WASHINGTON SQUARE PARK',
545  84489: 'PROSPECT PARK',
546  84518: 'PROSPECT PARK',
547  84688: 'PROSPECT PARK',
548  84695: 'PROSPECT PARK',
549  88812: 'J F K AIRPORT',
550  89205: 'CENTRAL PARK',
551  89382: 'GRAND ARMY PLAZA',
552  89734: 'BRYANT PARK',
553  93990: 'BETH ISRAEL MED CENTER',
554  99407: 'J F K AIRPORT',
555  99847: 'PORT AUTH 42 STREET',
556  100073: 'MUSEUM NATURAL HIST',
557  101013: 'GRAND CENTRAL TERM',
558  104020: 'JFK',
559  106118: 'CENTRAL PARK',
560  106499: 'LA GUARDIA AIRPORT'},
561 'Status': {2869: 'Closed',
562  23571: 'Closed',
563  41625: 'Closed',
564  44331: 'Closed',
565  46913: 'Closed',
566  47459: 'Closed',
567  48465: 'Closed',
568  51837: 'Closed',
569  51848: 'Closed',
570  54089: 'Closed',
571  54343: 'Closed',
572  55140: 'Closed',
573  57789: 'Closed',
574  63119: 'Closed',
575  66242: 'Closed',
576  66758: 'Closed',
577  66786: 'Closed',
578  66809: 'Closed',
579  67465: 'Closed',
580  72424: 'Closed',
581  75531: 'Closed',
582  77918: 'Closed',
583  78048: 'Closed',
584  78352: 'Closed',
585  78383: 'Closed',
586  79078: 'Closed',
587  84489: 'Closed',
588  84518: 'Closed',
589  84688: 'Closed',
590  84695: 'Closed',
591  88812: 'Closed',
592  89205: 'Closed',
593  89382: 'Closed',
594  89734: 'Closed',
595  93990: 'Closed',
596  99407: 'Closed',
597  99847: 'Closed',
598  100073: 'Closed',
599  101013: 'Closed',
600  104020: 'Closed',
601  106118: 'Closed',
602  106499: 'Closed'},
603 'Borough': {2869: 'BROOKLYN',
604  23571: 'MANHATTAN',
605  41625: 'QUEENS',
606  44331: 'QUEENS',
607  46913: 'BROOKLYN',
608  47459: 'MANHATTAN',
609  48465: 'QUEENS',
610  51837: 'MANHATTAN',
611  51848: 'MANHATTAN',
612  54089: 'MANHATTAN',
613  54343: 'MANHATTAN',
614  55140: 'QUEENS',
615  57789: 'QUEENS',
616  63119: 'QUEENS',
617  66242: 'QUEENS',
618  66758: 'MANHATTAN',
619  66786: 'MANHATTAN',
620  66809: 'QUEENS',
621  67465: 'QUEENS',
622  72424: 'BROOKLYN',
623  75531: 'MANHATTAN',
624  77918: 'MANHATTAN',
625  78048: 'QUEENS',
626  78352: 'BROOKLYN',
627  78383: 'QUEENS',
628  79078: 'MANHATTAN',
629  84489: 'BROOKLYN',
630  84518: 'BROOKLYN',
631  84688: 'BROOKLYN',
632  84695: 'BROOKLYN',
633  88812: 'QUEENS',
634  89205: 'MANHATTAN',
635  89382: 'BROOKLYN',
636  89734: 'MANHATTAN',
637  93990: 'MANHATTAN',
638  99407: 'QUEENS',
639  99847: 'MANHATTAN',
640  100073: 'MANHATTAN',
641  101013: 'MANHATTAN',
642  104020: 'QUEENS',
643  106118: 'MANHATTAN',
644  106499: 'QUEENS'}}
645for c in ['Created Date', 'Closed Date']:
646    df[c] = pd.to_datetime(df[c])
647    #df[c+'_date'] = df[c].dt.date # to extract the date (for created + closed)
648    #df[c+'_time'] = df[c].dt.time # to extract the time (for created + closed)
649

Then you can calculate the difference in time between the two as a new column (as hours) .astype('timedelta64[h]'), and then calculate a grouped mean:

1Unnamed: 0     Created Date             Closed Date            Agency   Agency Name  Complaint Type Descriptor  Location Type   Incident Zip    Address Type    City    Landmark    Status  Borough
22869    2869    10/30/2013 09:14:47 AM  10/30/2013 10:48:51 AM  NYPD    New York City Police Department Illegal Parking Double Parked Blocking Traffic  Street/Sidewalk 11217.0 PLACENAME   BROOKLYN    BARCLAYS CENTER Closed  BROOKLYN
323571   23571   10/25/2013 02:33:54 PM  10/25/2013 03:36:36 PM  NYPD    New York City Police Department Noise - Park    Loud Music/Party    Park/Playground 10000   PLACENAME   NEW YORK    CENTRAL PARK    Closed  MANHATTAN
441625   41625   10/22/2013 09:33:56 PM  10/24/2013 05:37:24 PM  TLC Taxi and Limousine Commission   For Hire Vehicle Complaint  Car Service Company Complaint   Street  11430   PLACENAME   JAMAICA J F K AIRPORT   Closed  QUEENS
544331   44331   10/22/2013 07:25:35 AM  10/25/2013 10:40:35 AM  TLC Taxi and Limousine Commission   Taxi Complaint  Driver Complaint    Street  11430   PLACENAME   JAMAICA J F K AIRPORT   Closed  QUEENS
646913   46913   10/21/2013 05:03:26 PM  10/23/2013 09:59:23 AM  DPR Department of Parks and Recreation  Dead Tree   Dead/Dying Tree Street  11215   PLACENAME   BROOKLYN    BARTEL PRITCHARD SQUARE Closed  BROOKLYN
747459   47459   10/21/2013 02:56:08 PM  10/29/2013 06:17:10 PM  TLC Taxi and Limousine Commission   Taxi Complaint  Driver Complaint    Street  10031   PLACENAME   NEW YORK    CITY COLLEGE    Closed  MANHATTAN
848465   48465   10/21/2013 10:44:10 AM  10/21/2013 11:17:47 AM  NYPD    New York City Police Department Illegal Parking Posted Parking Sign Violation   Street/Sidewalk 11434   PLACENAME   JAMAICA PS 37   Closed  QUEENS
951837   51837   10/20/2013 04:36:12 PM  10/20/2013 06:35:49 PM  NYPD    New York City Police Department Noise - Park    Loud Music/Party    Park/Playground 10031.0 PLACENAME   NEW YORK    JACKIE ROBINSON PARK    Closed  MANHATTAN
1051848   51848   10/20/2013 04:26:03 PM  10/20/2013 06:34:47 PM  NYPD    New York City Police Department Noise - Park    Loud Music/Party    Park/Playground 10031.0 PLACENAME   NEW YORK    JACKIE ROBINSON PARK    Closed  MANHATTAN
1154089   54089   10/19/2013 03:45:47 PM  10/19/2013 04:10:11 PM  NYPD    New York City Police Department Noise - Park    Loud Music/Party    Park/Playground 10000.0 PLACENAME   NEW YORK    CENTRAL PARK    Closed  MANHATTAN
1254343   54343   10/19/2013 01:27:43 PM  10/28/2013 08:42:12 AM  DOT Department of Transportation    Street Condition    Rough, Pitted or Cracked Roads  Street  10003.0 PLACENAME   NEW YORK    UNION SQUARE PARK   Closed  MANHATTAN
1355140   55140   10/19/2013 02:02:28 AM  10/19/2013 02:19:55 AM  NYPD    New York City Police Department Noise - Vehicle Car/Truck Music Street/Sidewalk 11368.0 PLACENAME   CORONA  WORLDS FAIR MARINA  Closed  QUEENS
1457789   57789   10/18/2013 11:55:44 AM  10/23/2013 02:42:14 PM  TLC Taxi and Limousine Commission   Taxi Complaint  Driver Complaint    Street  11369.0 PLACENAME   EAST ELMHURST   LA GUARDIA AIRPORT  Closed  QUEENS
1563119   63119   10/17/2013 06:52:37 AM  10/25/2013 06:49:59 PM  TLC Taxi and Limousine Commission   Taxi Complaint  Driver Complaint    Street  11430.0 PLACENAME   JAMAICA J F K AIRPORT   Closed  QUEENS
1666242   66242   10/16/2013 01:56:24 PM  10/22/2013 03:09:11 PM  TLC Taxi and Limousine Commission   Taxi Complaint  Driver Complaint    Street  11369   PLACENAME   EAST ELMHURST   LA GUARDIA AIRPORT  Closed  QUEENS
1766758   66758   10/16/2013 11:52:43 AM  10/16/2013 04:35:34 PM  NYPD    New York City Police Department Vending Unlicensed  Park/Playground 10036   PLACENAME   NEW YORK    BRYANT PARK Closed  MANHATTAN
1866786   66786   10/16/2013 11:42:23 AM  10/18/2013 04:57:04 PM  TLC Taxi and Limousine Commission   Taxi Complaint  Insurance Information Requested Street  10003   PLACENAME   NEW YORK    BETH ISRAEL MED CENTER  Closed  MANHATTAN
1966809   66809   10/16/2013 11:36:54 AM  10/16/2013 12:34:23 PM  NYPD    New York City Police Department Traffic Congestion/Gridlock Street/Sidewalk 11430   PLACENAME   JAMAICA J F K AIRPORT   Closed  QUEENS
2067465   67465   10/16/2013 09:14:35 AM  10/16/2013 12:43:06 PM  NYPD    New York City Police Department Traffic Drag Racing Street/Sidewalk 11367   PLACENAME   FLUSHING    QUEENS COLLEGE  Closed  QUEENS
2172424   72424   10/15/2013 12:22:00 AM  10/21/2013 12:16:15 PM  TLC Taxi and Limousine Commission   Taxi Complaint  Driver Complaint    Street  11217   PLACENAME   BROOKLYN    BARCLAYS CENTER Closed  BROOKLYN
2275531   75531   10/14/2013 10:59:20 AM  10/14/2013 03:09:51 PM  NYPD    New York City Police Department Vending In Prohibited Area  Park/Playground 10000   PLACENAME   NEW YORK    CENTRAL PARK    Closed  MANHATTAN
2377918   77918   10/13/2013 03:16:03 PM  10/13/2013 03:25:45 PM  NYPD    New York City Police Department Noise - Park    Loud Music/Party    Park/Playground 10000   PLACENAME   NEW YORK    CENTRAL PARK    Closed  MANHATTAN
2478048   78048   10/13/2013 01:06:02 PM  10/21/2013 10:20:21 AM  TLC Taxi and Limousine Commission   Taxi Complaint  Driver Complaint    Street  11369   PLACENAME   EAST ELMHURST   LA GUARDIA AIRPORT  Closed  QUEENS
2578352   78352   10/13/2013 05:14:33 AM  10/16/2013 01:42:42 PM  TLC Taxi and Limousine Commission   For Hire Vehicle Complaint  Car Service Company Complaint   Street  11217   PLACENAME   BROOKLYN    BARCLAYS CENTER Closed  BROOKLYN
2678383   78383   10/13/2013 03:50:02 AM  10/13/2013 05:03:13 AM  NYPD    New York City Police Department Noise - Vehicle Car/Truck Music Street/Sidewalk 11368   PLACENAME   CORONA  WORLDS FAIR MARINA  Closed  QUEENS
2779078   79078   10/12/2013 09:53:17 PM  10/13/2013 02:52:07 AM  NYPD    New York City Police Department Noise - Park    Loud Music/Party    Park/Playground 10011   PLACENAME   NEW YORK    WASHINGTON SQUARE PARK  Closed  MANHATTAN
2884489   84489   10/10/2013 07:16:16 PM  10/10/2013 10:29:16 PM  NYPD    New York City Police Department Noise - Park    Loud Music/Party    Park/Playground 11215   PLACENAME   BROOKLYN    PROSPECT PARK   Closed  BROOKLYN
2984518   84518   10/10/2013 07:02:29 PM  10/10/2013 10:29:16 PM  NYPD    New York City Police Department Noise - Park    Loud Music/Party    Park/Playground 11215   PLACENAME   BROOKLYN    PROSPECT PARK   Closed  BROOKLYN
3084688   84688   10/10/2013 05:39:19 PM  10/10/2013 10:29:17 PM  NYPD    New York City Police Department Noise - Park    Loud Music/Party    Park/Playground 11215   PLACENAME   BROOKLYN    PROSPECT PARK   Closed  BROOKLYN
3184695   84695   10/10/2013 05:37:04 PM  10/10/2013 10:30:19 PM  NYPD    New York City Police Department Noise - Park    Loud Music/Party    Park/Playground 11215   PLACENAME   BROOKLYN    PROSPECT PARK   Closed  BROOKLYN
3288812   88812   10/09/2013 09:17:15 PM  10/23/2013 02:15:21 PM  TLC Taxi and Limousine Commission   Taxi Complaint  Driver Complaint    Street  11430   PLACENAME   JAMAICA J F K AIRPORT   Closed  QUEENS
3389205   89205   10/09/2013 06:01:48 PM  10/09/2013 09:04:26 PM  NYPD    New York City Police Department Vending Unlicensed  Park/Playground 10000   PLACENAME   NEW YORK    CENTRAL PARK    Closed  MANHATTAN
3489382   89382   10/09/2013 04:53:01 PM  10/18/2013 08:35:02 AM  DOT Department of Transportation    Public Toilet   Damaged Door    Sidewalk    11238   PLACENAME   BROOKLYN    GRAND ARMY PLAZA    Closed  BROOKLYN
3589734   89734   10/09/2013 03:13:23 PM  10/09/2013 05:10:45 PM  NYPD    New York City Police Department Noise - Park    Loud Music/Party    Park/Playground 10036   PLACENAME   NEW YORK    BRYANT PARK Closed  MANHATTAN
3693990   93990   10/08/2013 06:14:15 PM  10/09/2013 04:00:59 PM  TLC Taxi and Limousine Commission   Taxi Complaint  Driver Complaint    Street  10003   PLACENAME   NEW YORK    BETH ISRAEL MED CENTER  Closed  MANHATTAN
3799407   99407   10/07/2013 03:56:11 PM  10/08/2013 07:04:14 AM  DPR Department of Parks and Recreation  Overgrown Tree/Branches Traffic Sign or Signal Blocked  Street  11430.0 PLACENAME   JAMAICA J F K AIRPORT   Closed  QUEENS
3899847   99847   10/07/2013 02:33:21 PM  10/09/2013 02:36:42 PM  TLC Taxi and Limousine Commission   Taxi Complaint  Driver Complaint    Street  10036.0 PLACENAME   NEW YORK    PORT AUTH 42 STREET Closed  MANHATTAN
39100073  100073  10/07/2013 01:36:02 PM  10/09/2013 09:56:55 AM  TLC Taxi and Limousine Commission   Taxi Complaint  Driver Complaint    Street  10024.0 PLACENAME   NEW YORK    MUSEUM NATURAL HIST Closed  MANHATTAN
40101013  101013  10/07/2013 10:05:18 AM  10/09/2013 03:36:23 PM  TLC Taxi and Limousine Commission   Taxi Complaint  Driver Complaint    Street  10017.0 PLACENAME   NEW YORK    GRAND CENTRAL TERM  Closed  MANHATTAN
41104020  104020  10/06/2013 02:58:47 PM  10/07/2013 12:11:16 PM  TLC Taxi and Limousine Commission   For Hire Vehicle Complaint  Car Service Company Complaint   Street  11430.0 PLACENAME   JAMAICA JFK Closed  QUEENS
42106118  106118  10/05/2013 03:24:47 PM  10/05/2013 04:20:34 PM  NYPD    New York City Police Department Noise - Park    Loud Music/Party    Park/Playground 10000.0 PLACENAME   NEW YORK    CENTRAL PARK    Closed  MANHATTAN
43106499  106499  10/05/2013 11:52:13 AM  10/07/2013 08:00:28 AM  DOT Department of Transportation    Public Toilet   Dirty/Graffiti  Sidewalk    11369.0 PLACENAME   EAST ELMHURST   LA GUARDIA AIRPORT  Closed  QUEENS
44pd.Timedelta(task3['Closed Date'] - task3['Created Date']).seconds / 60.0
45                         mean                   count
46complaint Type           closing_time_hours     closing_time_hours
47Blocked Driveway         3.00                   4581
48DOF Literature Request   30.16                  5481
49General Construction     66.38                  798
50Heating                  54.88                  6704
51Illegal Parking          3.08                   3336
52Nonconst                 65                     100 
53Paint-Plaster            49                     3281 
54Plumbing                 65                     666   
55Strret Condition         81                     2610    
56Street Light Condition   90                     4207
57{'Unnamed: 0': {2869: 2869,
58  23571: 23571,
59  41625: 41625,
60  44331: 44331,
61  46913: 46913,
62  47459: 47459,
63  48465: 48465,
64  51837: 51837,
65  51848: 51848,
66  54089: 54089,
67  54343: 54343,
68  55140: 55140,
69  57789: 57789,
70  63119: 63119,
71  66242: 66242,
72  66758: 66758,
73  66786: 66786,
74  66809: 66809,
75  67465: 67465,
76  72424: 72424,
77  75531: 75531,
78  77918: 77918,
79  78048: 78048,
80  78352: 78352,
81  78383: 78383,
82  79078: 79078,
83  84489: 84489,
84  84518: 84518,
85  84688: 84688,
86  84695: 84695,
87  88812: 88812,
88  89205: 89205,
89  89382: 89382,
90  89734: 89734,
91  93990: 93990,
92  99407: 99407,
93  99847: 99847,
94  100073: 100073,
95  101013: 101013,
96  104020: 104020,
97  106118: 106118,
98  106499: 106499},
99 'Created Date': {2869: '10/30/2013 09:14:47 AM',
100  23571: '10/25/2013 02:33:54 PM',
101  41625: '10/22/2013 09:33:56 PM',
102  44331: '10/22/2013 07:25:35 AM',
103  46913: '10/21/2013 05:03:26 PM',
104  47459: '10/21/2013 02:56:08 PM',
105  48465: '10/21/2013 10:44:10 AM',
106  51837: '10/20/2013 04:36:12 PM',
107  51848: '10/20/2013 04:26:03 PM',
108  54089: '10/19/2013 03:45:47 PM',
109  54343: '10/19/2013 01:27:43 PM',
110  55140: '10/19/2013 02:02:28 AM',
111  57789: '10/18/2013 11:55:44 AM',
112  63119: '10/17/2013 06:52:37 AM',
113  66242: '10/16/2013 01:56:24 PM',
114  66758: '10/16/2013 11:52:43 AM',
115  66786: '10/16/2013 11:42:23 AM',
116  66809: '10/16/2013 11:36:54 AM',
117  67465: '10/16/2013 09:14:35 AM',
118  72424: '10/15/2013 12:22:00 AM',
119  75531: '10/14/2013 10:59:20 AM',
120  77918: '10/13/2013 03:16:03 PM',
121  78048: '10/13/2013 01:06:02 PM',
122  78352: '10/13/2013 05:14:33 AM',
123  78383: '10/13/2013 03:50:02 AM',
124  79078: '10/12/2013 09:53:17 PM',
125  84489: '10/10/2013 07:16:16 PM',
126  84518: '10/10/2013 07:02:29 PM',
127  84688: '10/10/2013 05:39:19 PM',
128  84695: '10/10/2013 05:37:04 PM',
129  88812: '10/09/2013 09:17:15 PM',
130  89205: '10/09/2013 06:01:48 PM',
131  89382: '10/09/2013 04:53:01 PM',
132  89734: '10/09/2013 03:13:23 PM',
133  93990: '10/08/2013 06:14:15 PM',
134  99407: '10/07/2013 03:56:11 PM',
135  99847: '10/07/2013 02:33:21 PM',
136  100073: '10/07/2013 01:36:02 PM',
137  101013: '10/07/2013 10:05:18 AM',
138  104020: '10/06/2013 02:58:47 PM',
139  106118: '10/05/2013 03:24:47 PM',
140  106499: '10/05/2013 11:52:13 AM'},
141 'Closed Date': {2869: '10/30/2013 10:48:51 AM',
142  23571: '10/25/2013 03:36:36 PM',
143  41625: '10/24/2013 05:37:24 PM',
144  44331: '10/25/2013 10:40:35 AM',
145  46913: '10/23/2013 09:59:23 AM',
146  47459: '10/29/2013 06:17:10 PM',
147  48465: '10/21/2013 11:17:47 AM',
148  51837: '10/20/2013 06:35:49 PM',
149  51848: '10/20/2013 06:34:47 PM',
150  54089: '10/19/2013 04:10:11 PM',
151  54343: '10/28/2013 08:42:12 AM',
152  55140: '10/19/2013 02:19:55 AM',
153  57789: '10/23/2013 02:42:14 PM',
154  63119: '10/25/2013 06:49:59 PM',
155  66242: '10/22/2013 03:09:11 PM',
156  66758: '10/16/2013 04:35:34 PM',
157  66786: '10/18/2013 04:57:04 PM',
158  66809: '10/16/2013 12:34:23 PM',
159  67465: '10/16/2013 12:43:06 PM',
160  72424: '10/21/2013 12:16:15 PM',
161  75531: '10/14/2013 03:09:51 PM',
162  77918: '10/13/2013 03:25:45 PM',
163  78048: '10/21/2013 10:20:21 AM',
164  78352: '10/16/2013 01:42:42 PM',
165  78383: '10/13/2013 05:03:13 AM',
166  79078: '10/13/2013 02:52:07 AM',
167  84489: '10/10/2013 10:29:16 PM',
168  84518: '10/10/2013 10:29:16 PM',
169  84688: '10/10/2013 10:29:17 PM',
170  84695: '10/10/2013 10:30:19 PM',
171  88812: '10/23/2013 02:15:21 PM',
172  89205: '10/09/2013 09:04:26 PM',
173  89382: '10/18/2013 08:35:02 AM',
174  89734: '10/09/2013 05:10:45 PM',
175  93990: '10/09/2013 04:00:59 PM',
176  99407: '10/08/2013 07:04:14 AM',
177  99847: '10/09/2013 02:36:42 PM',
178  100073: '10/09/2013 09:56:55 AM',
179  101013: '10/09/2013 03:36:23 PM',
180  104020: '10/07/2013 12:11:16 PM',
181  106118: '10/05/2013 04:20:34 PM',
182  106499: '10/07/2013 08:00:28 AM'},
183 'Agency': {2869: 'NYPD',
184  23571: 'NYPD',
185  41625: 'TLC',
186  44331: 'TLC',
187  46913: 'DPR',
188  47459: 'TLC',
189  48465: 'NYPD',
190  51837: 'NYPD',
191  51848: 'NYPD',
192  54089: 'NYPD',
193  54343: 'DOT',
194  55140: 'NYPD',
195  57789: 'TLC',
196  63119: 'TLC',
197  66242: 'TLC',
198  66758: 'NYPD',
199  66786: 'TLC',
200  66809: 'NYPD',
201  67465: 'NYPD',
202  72424: 'TLC',
203  75531: 'NYPD',
204  77918: 'NYPD',
205  78048: 'TLC',
206  78352: 'TLC',
207  78383: 'NYPD',
208  79078: 'NYPD',
209  84489: 'NYPD',
210  84518: 'NYPD',
211  84688: 'NYPD',
212  84695: 'NYPD',
213  88812: 'TLC',
214  89205: 'NYPD',
215  89382: 'DOT',
216  89734: 'NYPD',
217  93990: 'TLC',
218  99407: 'DPR',
219  99847: 'TLC',
220  100073: 'TLC',
221  101013: 'TLC',
222  104020: 'TLC',
223  106118: 'NYPD',
224  106499: 'DOT'},
225 'Agency Name': {2869: 'New York City Police Department',
226  23571: 'New York City Police Department',
227  41625: 'Taxi and Limousine Commission',
228  44331: 'Taxi and Limousine Commission',
229  46913: 'Department of Parks and Recreation',
230  47459: 'Taxi and Limousine Commission',
231  48465: 'New York City Police Department',
232  51837: 'New York City Police Department',
233  51848: 'New York City Police Department',
234  54089: 'New York City Police Department',
235  54343: 'Department of Transportation',
236  55140: 'New York City Police Department',
237  57789: 'Taxi and Limousine Commission',
238  63119: 'Taxi and Limousine Commission',
239  66242: 'Taxi and Limousine Commission',
240  66758: 'New York City Police Department',
241  66786: 'Taxi and Limousine Commission',
242  66809: 'New York City Police Department',
243  67465: 'New York City Police Department',
244  72424: 'Taxi and Limousine Commission',
245  75531: 'New York City Police Department',
246  77918: 'New York City Police Department',
247  78048: 'Taxi and Limousine Commission',
248  78352: 'Taxi and Limousine Commission',
249  78383: 'New York City Police Department',
250  79078: 'New York City Police Department',
251  84489: 'New York City Police Department',
252  84518: 'New York City Police Department',
253  84688: 'New York City Police Department',
254  84695: 'New York City Police Department',
255  88812: 'Taxi and Limousine Commission',
256  89205: 'New York City Police Department',
257  89382: 'Department of Transportation',
258  89734: 'New York City Police Department',
259  93990: 'Taxi and Limousine Commission',
260  99407: 'Department of Parks and Recreation',
261  99847: 'Taxi and Limousine Commission',
262  100073: 'Taxi and Limousine Commission',
263  101013: 'Taxi and Limousine Commission',
264  104020: 'Taxi and Limousine Commission',
265  106118: 'New York City Police Department',
266  106499: 'Department of Transportation'},
267 'Complaint Type': {2869: 'Illegal Parking',
268  23571: 'Noise - Park',
269  41625: 'For Hire Vehicle Complaint',
270  44331: 'Taxi Complaint',
271  46913: 'Dead Tree',
272  47459: 'Taxi Complaint',
273  48465: 'Illegal Parking',
274  51837: 'Noise - Park',
275  51848: 'Noise - Park',
276  54089: 'Noise - Park',
277  54343: 'Street Condition',
278  55140: 'Noise - Vehicle',
279  57789: 'Taxi Complaint',
280  63119: 'Taxi Complaint',
281  66242: 'Taxi Complaint',
282  66758: 'Vending',
283  66786: 'Taxi Complaint',
284  66809: 'Traffic',
285  67465: 'Traffic',
286  72424: 'Taxi Complaint',
287  75531: 'Vending',
288  77918: 'Noise - Park',
289  78048: 'Taxi Complaint',
290  78352: 'For Hire Vehicle Complaint',
291  78383: 'Noise - Vehicle',
292  79078: 'Noise - Park',
293  84489: 'Noise - Park',
294  84518: 'Noise - Park',
295  84688: 'Noise - Park',
296  84695: 'Noise - Park',
297  88812: 'Taxi Complaint',
298  89205: 'Vending',
299  89382: 'Public Toilet',
300  89734: 'Noise - Park',
301  93990: 'Taxi Complaint',
302  99407: 'Overgrown Tree/Branches',
303  99847: 'Taxi Complaint',
304  100073: 'Taxi Complaint',
305  101013: 'Taxi Complaint',
306  104020: 'For Hire Vehicle Complaint',
307  106118: 'Noise - Park',
308  106499: 'Public Toilet'},
309 'Descriptor': {2869: 'Double Parked Blocking Traffic',
310  23571: 'Loud Music/Party',
311  41625: 'Car Service Company Complaint',
312  44331: 'Driver Complaint',
313  46913: 'Dead/Dying Tree',
314  47459: 'Driver Complaint',
315  48465: 'Posted Parking Sign Violation',
316  51837: 'Loud Music/Party',
317  51848: 'Loud Music/Party',
318  54089: 'Loud Music/Party',
319  54343: 'Rough, Pitted or Cracked Roads',
320  55140: 'Car/Truck Music',
321  57789: 'Driver Complaint',
322  63119: 'Driver Complaint',
323  66242: 'Driver Complaint',
324  66758: 'Unlicensed',
325  66786: 'Insurance Information Requested',
326  66809: 'Congestion/Gridlock',
327  67465: 'Drag Racing',
328  72424: 'Driver Complaint',
329  75531: 'In Prohibited Area',
330  77918: 'Loud Music/Party',
331  78048: 'Driver Complaint',
332  78352: 'Car Service Company Complaint',
333  78383: 'Car/Truck Music',
334  79078: 'Loud Music/Party',
335  84489: 'Loud Music/Party',
336  84518: 'Loud Music/Party',
337  84688: 'Loud Music/Party',
338  84695: 'Loud Music/Party',
339  88812: 'Driver Complaint',
340  89205: 'Unlicensed',
341  89382: 'Damaged Door',
342  89734: 'Loud Music/Party',
343  93990: 'Driver Complaint',
344  99407: 'Traffic Sign or Signal Blocked',
345  99847: 'Driver Complaint',
346  100073: 'Driver Complaint',
347  101013: 'Driver Complaint',
348  104020: 'Car Service Company Complaint',
349  106118: 'Loud Music/Party',
350  106499: 'Dirty/Graffiti'},
351 'Location Type': {2869: 'Street/Sidewalk',
352  23571: 'Park/Playground',
353  41625: 'Street',
354  44331: 'Street',
355  46913: 'Street',
356  47459: 'Street',
357  48465: 'Street/Sidewalk',
358  51837: 'Park/Playground',
359  51848: 'Park/Playground',
360  54089: 'Park/Playground',
361  54343: 'Street',
362  55140: 'Street/Sidewalk',
363  57789: 'Street',
364  63119: 'Street',
365  66242: 'Street',
366  66758: 'Park/Playground',
367  66786: 'Street',
368  66809: 'Street/Sidewalk',
369  67465: 'Street/Sidewalk',
370  72424: 'Street',
371  75531: 'Park/Playground',
372  77918: 'Park/Playground',
373  78048: 'Street',
374  78352: 'Street',
375  78383: 'Street/Sidewalk',
376  79078: 'Park/Playground',
377  84489: 'Park/Playground',
378  84518: 'Park/Playground',
379  84688: 'Park/Playground',
380  84695: 'Park/Playground',
381  88812: 'Street',
382  89205: 'Park/Playground',
383  89382: 'Sidewalk',
384  89734: 'Park/Playground',
385  93990: 'Street',
386  99407: 'Street',
387  99847: 'Street',
388  100073: 'Street',
389  101013: 'Street',
390  104020: 'Street',
391  106118: 'Park/Playground',
392  106499: 'Sidewalk'},
393 'Incident Zip': {2869: '11217.0',
394  23571: '10000',
395  41625: '11430',
396  44331: '11430',
397  46913: '11215',
398  47459: '10031',
399  48465: '11434',
400  51837: '10031.0',
401  51848: '10031.0',
402  54089: '10000.0',
403  54343: '10003.0',
404  55140: '11368.0',
405  57789: '11369.0',
406  63119: '11430.0',
407  66242: '11369',
408  66758: '10036',
409  66786: '10003',
410  66809: '11430',
411  67465: '11367',
412  72424: '11217',
413  75531: '10000',
414  77918: '10000',
415  78048: '11369',
416  78352: '11217',
417  78383: '11368',
418  79078: '10011',
419  84489: '11215',
420  84518: '11215',
421  84688: '11215',
422  84695: '11215',
423  88812: '11430',
424  89205: '10000',
425  89382: '11238',
426  89734: '10036',
427  93990: '10003',
428  99407: '11430.0',
429  99847: '10036.0',
430  100073: '10024.0',
431  101013: '10017.0',
432  104020: '11430.0',
433  106118: '10000.0',
434  106499: '11369.0'},
435 'Address Type': {2869: 'PLACENAME',
436  23571: 'PLACENAME',
437  41625: 'PLACENAME',
438  44331: 'PLACENAME',
439  46913: 'PLACENAME',
440  47459: 'PLACENAME',
441  48465: 'PLACENAME',
442  51837: 'PLACENAME',
443  51848: 'PLACENAME',
444  54089: 'PLACENAME',
445  54343: 'PLACENAME',
446  55140: 'PLACENAME',
447  57789: 'PLACENAME',
448  63119: 'PLACENAME',
449  66242: 'PLACENAME',
450  66758: 'PLACENAME',
451  66786: 'PLACENAME',
452  66809: 'PLACENAME',
453  67465: 'PLACENAME',
454  72424: 'PLACENAME',
455  75531: 'PLACENAME',
456  77918: 'PLACENAME',
457  78048: 'PLACENAME',
458  78352: 'PLACENAME',
459  78383: 'PLACENAME',
460  79078: 'PLACENAME',
461  84489: 'PLACENAME',
462  84518: 'PLACENAME',
463  84688: 'PLACENAME',
464  84695: 'PLACENAME',
465  88812: 'PLACENAME',
466  89205: 'PLACENAME',
467  89382: 'PLACENAME',
468  89734: 'PLACENAME',
469  93990: 'PLACENAME',
470  99407: 'PLACENAME',
471  99847: 'PLACENAME',
472  100073: 'PLACENAME',
473  101013: 'PLACENAME',
474  104020: 'PLACENAME',
475  106118: 'PLACENAME',
476  106499: 'PLACENAME'},
477 'City': {2869: 'BROOKLYN',
478  23571: 'NEW YORK',
479  41625: 'JAMAICA',
480  44331: 'JAMAICA',
481  46913: 'BROOKLYN',
482  47459: 'NEW YORK',
483  48465: 'JAMAICA',
484  51837: 'NEW YORK',
485  51848: 'NEW YORK',
486  54089: 'NEW YORK',
487  54343: 'NEW YORK',
488  55140: 'CORONA',
489  57789: 'EAST ELMHURST',
490  63119: 'JAMAICA',
491  66242: 'EAST ELMHURST',
492  66758: 'NEW YORK',
493  66786: 'NEW YORK',
494  66809: 'JAMAICA',
495  67465: 'FLUSHING',
496  72424: 'BROOKLYN',
497  75531: 'NEW YORK',
498  77918: 'NEW YORK',
499  78048: 'EAST ELMHURST',
500  78352: 'BROOKLYN',
501  78383: 'CORONA',
502  79078: 'NEW YORK',
503  84489: 'BROOKLYN',
504  84518: 'BROOKLYN',
505  84688: 'BROOKLYN',
506  84695: 'BROOKLYN',
507  88812: 'JAMAICA',
508  89205: 'NEW YORK',
509  89382: 'BROOKLYN',
510  89734: 'NEW YORK',
511  93990: 'NEW YORK',
512  99407: 'JAMAICA',
513  99847: 'NEW YORK',
514  100073: 'NEW YORK',
515  101013: 'NEW YORK',
516  104020: 'JAMAICA',
517  106118: 'NEW YORK',
518  106499: 'EAST ELMHURST'},
519 'Landmark': {2869: 'BARCLAYS CENTER',
520  23571: 'CENTRAL PARK',
521  41625: 'J F K AIRPORT',
522  44331: 'J F K AIRPORT',
523  46913: 'BARTEL PRITCHARD SQUARE',
524  47459: 'CITY COLLEGE',
525  48465: 'PS 37',
526  51837: 'JACKIE ROBINSON PARK',
527  51848: 'JACKIE ROBINSON PARK',
528  54089: 'CENTRAL PARK',
529  54343: 'UNION SQUARE PARK',
530  55140: 'WORLDS FAIR MARINA',
531  57789: 'LA GUARDIA AIRPORT',
532  63119: 'J F K AIRPORT',
533  66242: 'LA GUARDIA AIRPORT',
534  66758: 'BRYANT PARK',
535  66786: 'BETH ISRAEL MED CENTER',
536  66809: 'J F K AIRPORT',
537  67465: 'QUEENS COLLEGE',
538  72424: 'BARCLAYS CENTER',
539  75531: 'CENTRAL PARK',
540  77918: 'CENTRAL PARK',
541  78048: 'LA GUARDIA AIRPORT',
542  78352: 'BARCLAYS CENTER',
543  78383: 'WORLDS FAIR MARINA',
544  79078: 'WASHINGTON SQUARE PARK',
545  84489: 'PROSPECT PARK',
546  84518: 'PROSPECT PARK',
547  84688: 'PROSPECT PARK',
548  84695: 'PROSPECT PARK',
549  88812: 'J F K AIRPORT',
550  89205: 'CENTRAL PARK',
551  89382: 'GRAND ARMY PLAZA',
552  89734: 'BRYANT PARK',
553  93990: 'BETH ISRAEL MED CENTER',
554  99407: 'J F K AIRPORT',
555  99847: 'PORT AUTH 42 STREET',
556  100073: 'MUSEUM NATURAL HIST',
557  101013: 'GRAND CENTRAL TERM',
558  104020: 'JFK',
559  106118: 'CENTRAL PARK',
560  106499: 'LA GUARDIA AIRPORT'},
561 'Status': {2869: 'Closed',
562  23571: 'Closed',
563  41625: 'Closed',
564  44331: 'Closed',
565  46913: 'Closed',
566  47459: 'Closed',
567  48465: 'Closed',
568  51837: 'Closed',
569  51848: 'Closed',
570  54089: 'Closed',
571  54343: 'Closed',
572  55140: 'Closed',
573  57789: 'Closed',
574  63119: 'Closed',
575  66242: 'Closed',
576  66758: 'Closed',
577  66786: 'Closed',
578  66809: 'Closed',
579  67465: 'Closed',
580  72424: 'Closed',
581  75531: 'Closed',
582  77918: 'Closed',
583  78048: 'Closed',
584  78352: 'Closed',
585  78383: 'Closed',
586  79078: 'Closed',
587  84489: 'Closed',
588  84518: 'Closed',
589  84688: 'Closed',
590  84695: 'Closed',
591  88812: 'Closed',
592  89205: 'Closed',
593  89382: 'Closed',
594  89734: 'Closed',
595  93990: 'Closed',
596  99407: 'Closed',
597  99847: 'Closed',
598  100073: 'Closed',
599  101013: 'Closed',
600  104020: 'Closed',
601  106118: 'Closed',
602  106499: 'Closed'},
603 'Borough': {2869: 'BROOKLYN',
604  23571: 'MANHATTAN',
605  41625: 'QUEENS',
606  44331: 'QUEENS',
607  46913: 'BROOKLYN',
608  47459: 'MANHATTAN',
609  48465: 'QUEENS',
610  51837: 'MANHATTAN',
611  51848: 'MANHATTAN',
612  54089: 'MANHATTAN',
613  54343: 'MANHATTAN',
614  55140: 'QUEENS',
615  57789: 'QUEENS',
616  63119: 'QUEENS',
617  66242: 'QUEENS',
618  66758: 'MANHATTAN',
619  66786: 'MANHATTAN',
620  66809: 'QUEENS',
621  67465: 'QUEENS',
622  72424: 'BROOKLYN',
623  75531: 'MANHATTAN',
624  77918: 'MANHATTAN',
625  78048: 'QUEENS',
626  78352: 'BROOKLYN',
627  78383: 'QUEENS',
628  79078: 'MANHATTAN',
629  84489: 'BROOKLYN',
630  84518: 'BROOKLYN',
631  84688: 'BROOKLYN',
632  84695: 'BROOKLYN',
633  88812: 'QUEENS',
634  89205: 'MANHATTAN',
635  89382: 'BROOKLYN',
636  89734: 'MANHATTAN',
637  93990: 'MANHATTAN',
638  99407: 'QUEENS',
639  99847: 'MANHATTAN',
640  100073: 'MANHATTAN',
641  101013: 'MANHATTAN',
642  104020: 'QUEENS',
643  106118: 'MANHATTAN',
644  106499: 'QUEENS'}}
645for c in ['Created Date', 'Closed Date']:
646    df[c] = pd.to_datetime(df[c])
647    #df[c+'_date'] = df[c].dt.date # to extract the date (for created + closed)
648    #df[c+'_time'] = df[c].dt.time # to extract the time (for created + closed)
649df['difference in time'] = (df['Closed Date'] - df['Created Date']).astype('timedelta64[h]')
650print(df.groupby('Complaint Type').agg({'difference in time':'mean'}))
651

returns:

1Unnamed: 0     Created Date             Closed Date            Agency   Agency Name  Complaint Type Descriptor  Location Type   Incident Zip    Address Type    City    Landmark    Status  Borough
22869    2869    10/30/2013 09:14:47 AM  10/30/2013 10:48:51 AM  NYPD    New York City Police Department Illegal Parking Double Parked Blocking Traffic  Street/Sidewalk 11217.0 PLACENAME   BROOKLYN    BARCLAYS CENTER Closed  BROOKLYN
323571   23571   10/25/2013 02:33:54 PM  10/25/2013 03:36:36 PM  NYPD    New York City Police Department Noise - Park    Loud Music/Party    Park/Playground 10000   PLACENAME   NEW YORK    CENTRAL PARK    Closed  MANHATTAN
441625   41625   10/22/2013 09:33:56 PM  10/24/2013 05:37:24 PM  TLC Taxi and Limousine Commission   For Hire Vehicle Complaint  Car Service Company Complaint   Street  11430   PLACENAME   JAMAICA J F K AIRPORT   Closed  QUEENS
544331   44331   10/22/2013 07:25:35 AM  10/25/2013 10:40:35 AM  TLC Taxi and Limousine Commission   Taxi Complaint  Driver Complaint    Street  11430   PLACENAME   JAMAICA J F K AIRPORT   Closed  QUEENS
646913   46913   10/21/2013 05:03:26 PM  10/23/2013 09:59:23 AM  DPR Department of Parks and Recreation  Dead Tree   Dead/Dying Tree Street  11215   PLACENAME   BROOKLYN    BARTEL PRITCHARD SQUARE Closed  BROOKLYN
747459   47459   10/21/2013 02:56:08 PM  10/29/2013 06:17:10 PM  TLC Taxi and Limousine Commission   Taxi Complaint  Driver Complaint    Street  10031   PLACENAME   NEW YORK    CITY COLLEGE    Closed  MANHATTAN
848465   48465   10/21/2013 10:44:10 AM  10/21/2013 11:17:47 AM  NYPD    New York City Police Department Illegal Parking Posted Parking Sign Violation   Street/Sidewalk 11434   PLACENAME   JAMAICA PS 37   Closed  QUEENS
951837   51837   10/20/2013 04:36:12 PM  10/20/2013 06:35:49 PM  NYPD    New York City Police Department Noise - Park    Loud Music/Party    Park/Playground 10031.0 PLACENAME   NEW YORK    JACKIE ROBINSON PARK    Closed  MANHATTAN
1051848   51848   10/20/2013 04:26:03 PM  10/20/2013 06:34:47 PM  NYPD    New York City Police Department Noise - Park    Loud Music/Party    Park/Playground 10031.0 PLACENAME   NEW YORK    JACKIE ROBINSON PARK    Closed  MANHATTAN
1154089   54089   10/19/2013 03:45:47 PM  10/19/2013 04:10:11 PM  NYPD    New York City Police Department Noise - Park    Loud Music/Party    Park/Playground 10000.0 PLACENAME   NEW YORK    CENTRAL PARK    Closed  MANHATTAN
1254343   54343   10/19/2013 01:27:43 PM  10/28/2013 08:42:12 AM  DOT Department of Transportation    Street Condition    Rough, Pitted or Cracked Roads  Street  10003.0 PLACENAME   NEW YORK    UNION SQUARE PARK   Closed  MANHATTAN
1355140   55140   10/19/2013 02:02:28 AM  10/19/2013 02:19:55 AM  NYPD    New York City Police Department Noise - Vehicle Car/Truck Music Street/Sidewalk 11368.0 PLACENAME   CORONA  WORLDS FAIR MARINA  Closed  QUEENS
1457789   57789   10/18/2013 11:55:44 AM  10/23/2013 02:42:14 PM  TLC Taxi and Limousine Commission   Taxi Complaint  Driver Complaint    Street  11369.0 PLACENAME   EAST ELMHURST   LA GUARDIA AIRPORT  Closed  QUEENS
1563119   63119   10/17/2013 06:52:37 AM  10/25/2013 06:49:59 PM  TLC Taxi and Limousine Commission   Taxi Complaint  Driver Complaint    Street  11430.0 PLACENAME   JAMAICA J F K AIRPORT   Closed  QUEENS
1666242   66242   10/16/2013 01:56:24 PM  10/22/2013 03:09:11 PM  TLC Taxi and Limousine Commission   Taxi Complaint  Driver Complaint    Street  11369   PLACENAME   EAST ELMHURST   LA GUARDIA AIRPORT  Closed  QUEENS
1766758   66758   10/16/2013 11:52:43 AM  10/16/2013 04:35:34 PM  NYPD    New York City Police Department Vending Unlicensed  Park/Playground 10036   PLACENAME   NEW YORK    BRYANT PARK Closed  MANHATTAN
1866786   66786   10/16/2013 11:42:23 AM  10/18/2013 04:57:04 PM  TLC Taxi and Limousine Commission   Taxi Complaint  Insurance Information Requested Street  10003   PLACENAME   NEW YORK    BETH ISRAEL MED CENTER  Closed  MANHATTAN
1966809   66809   10/16/2013 11:36:54 AM  10/16/2013 12:34:23 PM  NYPD    New York City Police Department Traffic Congestion/Gridlock Street/Sidewalk 11430   PLACENAME   JAMAICA J F K AIRPORT   Closed  QUEENS
2067465   67465   10/16/2013 09:14:35 AM  10/16/2013 12:43:06 PM  NYPD    New York City Police Department Traffic Drag Racing Street/Sidewalk 11367   PLACENAME   FLUSHING    QUEENS COLLEGE  Closed  QUEENS
2172424   72424   10/15/2013 12:22:00 AM  10/21/2013 12:16:15 PM  TLC Taxi and Limousine Commission   Taxi Complaint  Driver Complaint    Street  11217   PLACENAME   BROOKLYN    BARCLAYS CENTER Closed  BROOKLYN
2275531   75531   10/14/2013 10:59:20 AM  10/14/2013 03:09:51 PM  NYPD    New York City Police Department Vending In Prohibited Area  Park/Playground 10000   PLACENAME   NEW YORK    CENTRAL PARK    Closed  MANHATTAN
2377918   77918   10/13/2013 03:16:03 PM  10/13/2013 03:25:45 PM  NYPD    New York City Police Department Noise - Park    Loud Music/Party    Park/Playground 10000   PLACENAME   NEW YORK    CENTRAL PARK    Closed  MANHATTAN
2478048   78048   10/13/2013 01:06:02 PM  10/21/2013 10:20:21 AM  TLC Taxi and Limousine Commission   Taxi Complaint  Driver Complaint    Street  11369   PLACENAME   EAST ELMHURST   LA GUARDIA AIRPORT  Closed  QUEENS
2578352   78352   10/13/2013 05:14:33 AM  10/16/2013 01:42:42 PM  TLC Taxi and Limousine Commission   For Hire Vehicle Complaint  Car Service Company Complaint   Street  11217   PLACENAME   BROOKLYN    BARCLAYS CENTER Closed  BROOKLYN
2678383   78383   10/13/2013 03:50:02 AM  10/13/2013 05:03:13 AM  NYPD    New York City Police Department Noise - Vehicle Car/Truck Music Street/Sidewalk 11368   PLACENAME   CORONA  WORLDS FAIR MARINA  Closed  QUEENS
2779078   79078   10/12/2013 09:53:17 PM  10/13/2013 02:52:07 AM  NYPD    New York City Police Department Noise - Park    Loud Music/Party    Park/Playground 10011   PLACENAME   NEW YORK    WASHINGTON SQUARE PARK  Closed  MANHATTAN
2884489   84489   10/10/2013 07:16:16 PM  10/10/2013 10:29:16 PM  NYPD    New York City Police Department Noise - Park    Loud Music/Party    Park/Playground 11215   PLACENAME   BROOKLYN    PROSPECT PARK   Closed  BROOKLYN
2984518   84518   10/10/2013 07:02:29 PM  10/10/2013 10:29:16 PM  NYPD    New York City Police Department Noise - Park    Loud Music/Party    Park/Playground 11215   PLACENAME   BROOKLYN    PROSPECT PARK   Closed  BROOKLYN
3084688   84688   10/10/2013 05:39:19 PM  10/10/2013 10:29:17 PM  NYPD    New York City Police Department Noise - Park    Loud Music/Party    Park/Playground 11215   PLACENAME   BROOKLYN    PROSPECT PARK   Closed  BROOKLYN
3184695   84695   10/10/2013 05:37:04 PM  10/10/2013 10:30:19 PM  NYPD    New York City Police Department Noise - Park    Loud Music/Party    Park/Playground 11215   PLACENAME   BROOKLYN    PROSPECT PARK   Closed  BROOKLYN
3288812   88812   10/09/2013 09:17:15 PM  10/23/2013 02:15:21 PM  TLC Taxi and Limousine Commission   Taxi Complaint  Driver Complaint    Street  11430   PLACENAME   JAMAICA J F K AIRPORT   Closed  QUEENS
3389205   89205   10/09/2013 06:01:48 PM  10/09/2013 09:04:26 PM  NYPD    New York City Police Department Vending Unlicensed  Park/Playground 10000   PLACENAME   NEW YORK    CENTRAL PARK    Closed  MANHATTAN
3489382   89382   10/09/2013 04:53:01 PM  10/18/2013 08:35:02 AM  DOT Department of Transportation    Public Toilet   Damaged Door    Sidewalk    11238   PLACENAME   BROOKLYN    GRAND ARMY PLAZA    Closed  BROOKLYN
3589734   89734   10/09/2013 03:13:23 PM  10/09/2013 05:10:45 PM  NYPD    New York City Police Department Noise - Park    Loud Music/Party    Park/Playground 10036   PLACENAME   NEW YORK    BRYANT PARK Closed  MANHATTAN
3693990   93990   10/08/2013 06:14:15 PM  10/09/2013 04:00:59 PM  TLC Taxi and Limousine Commission   Taxi Complaint  Driver Complaint    Street  10003   PLACENAME   NEW YORK    BETH ISRAEL MED CENTER  Closed  MANHATTAN
3799407   99407   10/07/2013 03:56:11 PM  10/08/2013 07:04:14 AM  DPR Department of Parks and Recreation  Overgrown Tree/Branches Traffic Sign or Signal Blocked  Street  11430.0 PLACENAME   JAMAICA J F K AIRPORT   Closed  QUEENS
3899847   99847   10/07/2013 02:33:21 PM  10/09/2013 02:36:42 PM  TLC Taxi and Limousine Commission   Taxi Complaint  Driver Complaint    Street  10036.0 PLACENAME   NEW YORK    PORT AUTH 42 STREET Closed  MANHATTAN
39100073  100073  10/07/2013 01:36:02 PM  10/09/2013 09:56:55 AM  TLC Taxi and Limousine Commission   Taxi Complaint  Driver Complaint    Street  10024.0 PLACENAME   NEW YORK    MUSEUM NATURAL HIST Closed  MANHATTAN
40101013  101013  10/07/2013 10:05:18 AM  10/09/2013 03:36:23 PM  TLC Taxi and Limousine Commission   Taxi Complaint  Driver Complaint    Street  10017.0 PLACENAME   NEW YORK    GRAND CENTRAL TERM  Closed  MANHATTAN
41104020  104020  10/06/2013 02:58:47 PM  10/07/2013 12:11:16 PM  TLC Taxi and Limousine Commission   For Hire Vehicle Complaint  Car Service Company Complaint   Street  11430.0 PLACENAME   JAMAICA JFK Closed  QUEENS
42106118  106118  10/05/2013 03:24:47 PM  10/05/2013 04:20:34 PM  NYPD    New York City Police Department Noise - Park    Loud Music/Party    Park/Playground 10000.0 PLACENAME   NEW YORK    CENTRAL PARK    Closed  MANHATTAN
43106499  106499  10/05/2013 11:52:13 AM  10/07/2013 08:00:28 AM  DOT Department of Transportation    Public Toilet   Dirty/Graffiti  Sidewalk    11369.0 PLACENAME   EAST ELMHURST   LA GUARDIA AIRPORT  Closed  QUEENS
44pd.Timedelta(task3['Closed Date'] - task3['Created Date']).seconds / 60.0
45                         mean                   count
46complaint Type           closing_time_hours     closing_time_hours
47Blocked Driveway         3.00                   4581
48DOF Literature Request   30.16                  5481
49General Construction     66.38                  798
50Heating                  54.88                  6704
51Illegal Parking          3.08                   3336
52Nonconst                 65                     100 
53Paint-Plaster            49                     3281 
54Plumbing                 65                     666   
55Strret Condition         81                     2610    
56Street Light Condition   90                     4207
57{'Unnamed: 0': {2869: 2869,
58  23571: 23571,
59  41625: 41625,
60  44331: 44331,
61  46913: 46913,
62  47459: 47459,
63  48465: 48465,
64  51837: 51837,
65  51848: 51848,
66  54089: 54089,
67  54343: 54343,
68  55140: 55140,
69  57789: 57789,
70  63119: 63119,
71  66242: 66242,
72  66758: 66758,
73  66786: 66786,
74  66809: 66809,
75  67465: 67465,
76  72424: 72424,
77  75531: 75531,
78  77918: 77918,
79  78048: 78048,
80  78352: 78352,
81  78383: 78383,
82  79078: 79078,
83  84489: 84489,
84  84518: 84518,
85  84688: 84688,
86  84695: 84695,
87  88812: 88812,
88  89205: 89205,
89  89382: 89382,
90  89734: 89734,
91  93990: 93990,
92  99407: 99407,
93  99847: 99847,
94  100073: 100073,
95  101013: 101013,
96  104020: 104020,
97  106118: 106118,
98  106499: 106499},
99 'Created Date': {2869: '10/30/2013 09:14:47 AM',
100  23571: '10/25/2013 02:33:54 PM',
101  41625: '10/22/2013 09:33:56 PM',
102  44331: '10/22/2013 07:25:35 AM',
103  46913: '10/21/2013 05:03:26 PM',
104  47459: '10/21/2013 02:56:08 PM',
105  48465: '10/21/2013 10:44:10 AM',
106  51837: '10/20/2013 04:36:12 PM',
107  51848: '10/20/2013 04:26:03 PM',
108  54089: '10/19/2013 03:45:47 PM',
109  54343: '10/19/2013 01:27:43 PM',
110  55140: '10/19/2013 02:02:28 AM',
111  57789: '10/18/2013 11:55:44 AM',
112  63119: '10/17/2013 06:52:37 AM',
113  66242: '10/16/2013 01:56:24 PM',
114  66758: '10/16/2013 11:52:43 AM',
115  66786: '10/16/2013 11:42:23 AM',
116  66809: '10/16/2013 11:36:54 AM',
117  67465: '10/16/2013 09:14:35 AM',
118  72424: '10/15/2013 12:22:00 AM',
119  75531: '10/14/2013 10:59:20 AM',
120  77918: '10/13/2013 03:16:03 PM',
121  78048: '10/13/2013 01:06:02 PM',
122  78352: '10/13/2013 05:14:33 AM',
123  78383: '10/13/2013 03:50:02 AM',
124  79078: '10/12/2013 09:53:17 PM',
125  84489: '10/10/2013 07:16:16 PM',
126  84518: '10/10/2013 07:02:29 PM',
127  84688: '10/10/2013 05:39:19 PM',
128  84695: '10/10/2013 05:37:04 PM',
129  88812: '10/09/2013 09:17:15 PM',
130  89205: '10/09/2013 06:01:48 PM',
131  89382: '10/09/2013 04:53:01 PM',
132  89734: '10/09/2013 03:13:23 PM',
133  93990: '10/08/2013 06:14:15 PM',
134  99407: '10/07/2013 03:56:11 PM',
135  99847: '10/07/2013 02:33:21 PM',
136  100073: '10/07/2013 01:36:02 PM',
137  101013: '10/07/2013 10:05:18 AM',
138  104020: '10/06/2013 02:58:47 PM',
139  106118: '10/05/2013 03:24:47 PM',
140  106499: '10/05/2013 11:52:13 AM'},
141 'Closed Date': {2869: '10/30/2013 10:48:51 AM',
142  23571: '10/25/2013 03:36:36 PM',
143  41625: '10/24/2013 05:37:24 PM',
144  44331: '10/25/2013 10:40:35 AM',
145  46913: '10/23/2013 09:59:23 AM',
146  47459: '10/29/2013 06:17:10 PM',
147  48465: '10/21/2013 11:17:47 AM',
148  51837: '10/20/2013 06:35:49 PM',
149  51848: '10/20/2013 06:34:47 PM',
150  54089: '10/19/2013 04:10:11 PM',
151  54343: '10/28/2013 08:42:12 AM',
152  55140: '10/19/2013 02:19:55 AM',
153  57789: '10/23/2013 02:42:14 PM',
154  63119: '10/25/2013 06:49:59 PM',
155  66242: '10/22/2013 03:09:11 PM',
156  66758: '10/16/2013 04:35:34 PM',
157  66786: '10/18/2013 04:57:04 PM',
158  66809: '10/16/2013 12:34:23 PM',
159  67465: '10/16/2013 12:43:06 PM',
160  72424: '10/21/2013 12:16:15 PM',
161  75531: '10/14/2013 03:09:51 PM',
162  77918: '10/13/2013 03:25:45 PM',
163  78048: '10/21/2013 10:20:21 AM',
164  78352: '10/16/2013 01:42:42 PM',
165  78383: '10/13/2013 05:03:13 AM',
166  79078: '10/13/2013 02:52:07 AM',
167  84489: '10/10/2013 10:29:16 PM',
168  84518: '10/10/2013 10:29:16 PM',
169  84688: '10/10/2013 10:29:17 PM',
170  84695: '10/10/2013 10:30:19 PM',
171  88812: '10/23/2013 02:15:21 PM',
172  89205: '10/09/2013 09:04:26 PM',
173  89382: '10/18/2013 08:35:02 AM',
174  89734: '10/09/2013 05:10:45 PM',
175  93990: '10/09/2013 04:00:59 PM',
176  99407: '10/08/2013 07:04:14 AM',
177  99847: '10/09/2013 02:36:42 PM',
178  100073: '10/09/2013 09:56:55 AM',
179  101013: '10/09/2013 03:36:23 PM',
180  104020: '10/07/2013 12:11:16 PM',
181  106118: '10/05/2013 04:20:34 PM',
182  106499: '10/07/2013 08:00:28 AM'},
183 'Agency': {2869: 'NYPD',
184  23571: 'NYPD',
185  41625: 'TLC',
186  44331: 'TLC',
187  46913: 'DPR',
188  47459: 'TLC',
189  48465: 'NYPD',
190  51837: 'NYPD',
191  51848: 'NYPD',
192  54089: 'NYPD',
193  54343: 'DOT',
194  55140: 'NYPD',
195  57789: 'TLC',
196  63119: 'TLC',
197  66242: 'TLC',
198  66758: 'NYPD',
199  66786: 'TLC',
200  66809: 'NYPD',
201  67465: 'NYPD',
202  72424: 'TLC',
203  75531: 'NYPD',
204  77918: 'NYPD',
205  78048: 'TLC',
206  78352: 'TLC',
207  78383: 'NYPD',
208  79078: 'NYPD',
209  84489: 'NYPD',
210  84518: 'NYPD',
211  84688: 'NYPD',
212  84695: 'NYPD',
213  88812: 'TLC',
214  89205: 'NYPD',
215  89382: 'DOT',
216  89734: 'NYPD',
217  93990: 'TLC',
218  99407: 'DPR',
219  99847: 'TLC',
220  100073: 'TLC',
221  101013: 'TLC',
222  104020: 'TLC',
223  106118: 'NYPD',
224  106499: 'DOT'},
225 'Agency Name': {2869: 'New York City Police Department',
226  23571: 'New York City Police Department',
227  41625: 'Taxi and Limousine Commission',
228  44331: 'Taxi and Limousine Commission',
229  46913: 'Department of Parks and Recreation',
230  47459: 'Taxi and Limousine Commission',
231  48465: 'New York City Police Department',
232  51837: 'New York City Police Department',
233  51848: 'New York City Police Department',
234  54089: 'New York City Police Department',
235  54343: 'Department of Transportation',
236  55140: 'New York City Police Department',
237  57789: 'Taxi and Limousine Commission',
238  63119: 'Taxi and Limousine Commission',
239  66242: 'Taxi and Limousine Commission',
240  66758: 'New York City Police Department',
241  66786: 'Taxi and Limousine Commission',
242  66809: 'New York City Police Department',
243  67465: 'New York City Police Department',
244  72424: 'Taxi and Limousine Commission',
245  75531: 'New York City Police Department',
246  77918: 'New York City Police Department',
247  78048: 'Taxi and Limousine Commission',
248  78352: 'Taxi and Limousine Commission',
249  78383: 'New York City Police Department',
250  79078: 'New York City Police Department',
251  84489: 'New York City Police Department',
252  84518: 'New York City Police Department',
253  84688: 'New York City Police Department',
254  84695: 'New York City Police Department',
255  88812: 'Taxi and Limousine Commission',
256  89205: 'New York City Police Department',
257  89382: 'Department of Transportation',
258  89734: 'New York City Police Department',
259  93990: 'Taxi and Limousine Commission',
260  99407: 'Department of Parks and Recreation',
261  99847: 'Taxi and Limousine Commission',
262  100073: 'Taxi and Limousine Commission',
263  101013: 'Taxi and Limousine Commission',
264  104020: 'Taxi and Limousine Commission',
265  106118: 'New York City Police Department',
266  106499: 'Department of Transportation'},
267 'Complaint Type': {2869: 'Illegal Parking',
268  23571: 'Noise - Park',
269  41625: 'For Hire Vehicle Complaint',
270  44331: 'Taxi Complaint',
271  46913: 'Dead Tree',
272  47459: 'Taxi Complaint',
273  48465: 'Illegal Parking',
274  51837: 'Noise - Park',
275  51848: 'Noise - Park',
276  54089: 'Noise - Park',
277  54343: 'Street Condition',
278  55140: 'Noise - Vehicle',
279  57789: 'Taxi Complaint',
280  63119: 'Taxi Complaint',
281  66242: 'Taxi Complaint',
282  66758: 'Vending',
283  66786: 'Taxi Complaint',
284  66809: 'Traffic',
285  67465: 'Traffic',
286  72424: 'Taxi Complaint',
287  75531: 'Vending',
288  77918: 'Noise - Park',
289  78048: 'Taxi Complaint',
290  78352: 'For Hire Vehicle Complaint',
291  78383: 'Noise - Vehicle',
292  79078: 'Noise - Park',
293  84489: 'Noise - Park',
294  84518: 'Noise - Park',
295  84688: 'Noise - Park',
296  84695: 'Noise - Park',
297  88812: 'Taxi Complaint',
298  89205: 'Vending',
299  89382: 'Public Toilet',
300  89734: 'Noise - Park',
301  93990: 'Taxi Complaint',
302  99407: 'Overgrown Tree/Branches',
303  99847: 'Taxi Complaint',
304  100073: 'Taxi Complaint',
305  101013: 'Taxi Complaint',
306  104020: 'For Hire Vehicle Complaint',
307  106118: 'Noise - Park',
308  106499: 'Public Toilet'},
309 'Descriptor': {2869: 'Double Parked Blocking Traffic',
310  23571: 'Loud Music/Party',
311  41625: 'Car Service Company Complaint',
312  44331: 'Driver Complaint',
313  46913: 'Dead/Dying Tree',
314  47459: 'Driver Complaint',
315  48465: 'Posted Parking Sign Violation',
316  51837: 'Loud Music/Party',
317  51848: 'Loud Music/Party',
318  54089: 'Loud Music/Party',
319  54343: 'Rough, Pitted or Cracked Roads',
320  55140: 'Car/Truck Music',
321  57789: 'Driver Complaint',
322  63119: 'Driver Complaint',
323  66242: 'Driver Complaint',
324  66758: 'Unlicensed',
325  66786: 'Insurance Information Requested',
326  66809: 'Congestion/Gridlock',
327  67465: 'Drag Racing',
328  72424: 'Driver Complaint',
329  75531: 'In Prohibited Area',
330  77918: 'Loud Music/Party',
331  78048: 'Driver Complaint',
332  78352: 'Car Service Company Complaint',
333  78383: 'Car/Truck Music',
334  79078: 'Loud Music/Party',
335  84489: 'Loud Music/Party',
336  84518: 'Loud Music/Party',
337  84688: 'Loud Music/Party',
338  84695: 'Loud Music/Party',
339  88812: 'Driver Complaint',
340  89205: 'Unlicensed',
341  89382: 'Damaged Door',
342  89734: 'Loud Music/Party',
343  93990: 'Driver Complaint',
344  99407: 'Traffic Sign or Signal Blocked',
345  99847: 'Driver Complaint',
346  100073: 'Driver Complaint',
347  101013: 'Driver Complaint',
348  104020: 'Car Service Company Complaint',
349  106118: 'Loud Music/Party',
350  106499: 'Dirty/Graffiti'},
351 'Location Type': {2869: 'Street/Sidewalk',
352  23571: 'Park/Playground',
353  41625: 'Street',
354  44331: 'Street',
355  46913: 'Street',
356  47459: 'Street',
357  48465: 'Street/Sidewalk',
358  51837: 'Park/Playground',
359  51848: 'Park/Playground',
360  54089: 'Park/Playground',
361  54343: 'Street',
362  55140: 'Street/Sidewalk',
363  57789: 'Street',
364  63119: 'Street',
365  66242: 'Street',
366  66758: 'Park/Playground',
367  66786: 'Street',
368  66809: 'Street/Sidewalk',
369  67465: 'Street/Sidewalk',
370  72424: 'Street',
371  75531: 'Park/Playground',
372  77918: 'Park/Playground',
373  78048: 'Street',
374  78352: 'Street',
375  78383: 'Street/Sidewalk',
376  79078: 'Park/Playground',
377  84489: 'Park/Playground',
378  84518: 'Park/Playground',
379  84688: 'Park/Playground',
380  84695: 'Park/Playground',
381  88812: 'Street',
382  89205: 'Park/Playground',
383  89382: 'Sidewalk',
384  89734: 'Park/Playground',
385  93990: 'Street',
386  99407: 'Street',
387  99847: 'Street',
388  100073: 'Street',
389  101013: 'Street',
390  104020: 'Street',
391  106118: 'Park/Playground',
392  106499: 'Sidewalk'},
393 'Incident Zip': {2869: '11217.0',
394  23571: '10000',
395  41625: '11430',
396  44331: '11430',
397  46913: '11215',
398  47459: '10031',
399  48465: '11434',
400  51837: '10031.0',
401  51848: '10031.0',
402  54089: '10000.0',
403  54343: '10003.0',
404  55140: '11368.0',
405  57789: '11369.0',
406  63119: '11430.0',
407  66242: '11369',
408  66758: '10036',
409  66786: '10003',
410  66809: '11430',
411  67465: '11367',
412  72424: '11217',
413  75531: '10000',
414  77918: '10000',
415  78048: '11369',
416  78352: '11217',
417  78383: '11368',
418  79078: '10011',
419  84489: '11215',
420  84518: '11215',
421  84688: '11215',
422  84695: '11215',
423  88812: '11430',
424  89205: '10000',
425  89382: '11238',
426  89734: '10036',
427  93990: '10003',
428  99407: '11430.0',
429  99847: '10036.0',
430  100073: '10024.0',
431  101013: '10017.0',
432  104020: '11430.0',
433  106118: '10000.0',
434  106499: '11369.0'},
435 'Address Type': {2869: 'PLACENAME',
436  23571: 'PLACENAME',
437  41625: 'PLACENAME',
438  44331: 'PLACENAME',
439  46913: 'PLACENAME',
440  47459: 'PLACENAME',
441  48465: 'PLACENAME',
442  51837: 'PLACENAME',
443  51848: 'PLACENAME',
444  54089: 'PLACENAME',
445  54343: 'PLACENAME',
446  55140: 'PLACENAME',
447  57789: 'PLACENAME',
448  63119: 'PLACENAME',
449  66242: 'PLACENAME',
450  66758: 'PLACENAME',
451  66786: 'PLACENAME',
452  66809: 'PLACENAME',
453  67465: 'PLACENAME',
454  72424: 'PLACENAME',
455  75531: 'PLACENAME',
456  77918: 'PLACENAME',
457  78048: 'PLACENAME',
458  78352: 'PLACENAME',
459  78383: 'PLACENAME',
460  79078: 'PLACENAME',
461  84489: 'PLACENAME',
462  84518: 'PLACENAME',
463  84688: 'PLACENAME',
464  84695: 'PLACENAME',
465  88812: 'PLACENAME',
466  89205: 'PLACENAME',
467  89382: 'PLACENAME',
468  89734: 'PLACENAME',
469  93990: 'PLACENAME',
470  99407: 'PLACENAME',
471  99847: 'PLACENAME',
472  100073: 'PLACENAME',
473  101013: 'PLACENAME',
474  104020: 'PLACENAME',
475  106118: 'PLACENAME',
476  106499: 'PLACENAME'},
477 'City': {2869: 'BROOKLYN',
478  23571: 'NEW YORK',
479  41625: 'JAMAICA',
480  44331: 'JAMAICA',
481  46913: 'BROOKLYN',
482  47459: 'NEW YORK',
483  48465: 'JAMAICA',
484  51837: 'NEW YORK',
485  51848: 'NEW YORK',
486  54089: 'NEW YORK',
487  54343: 'NEW YORK',
488  55140: 'CORONA',
489  57789: 'EAST ELMHURST',
490  63119: 'JAMAICA',
491  66242: 'EAST ELMHURST',
492  66758: 'NEW YORK',
493  66786: 'NEW YORK',
494  66809: 'JAMAICA',
495  67465: 'FLUSHING',
496  72424: 'BROOKLYN',
497  75531: 'NEW YORK',
498  77918: 'NEW YORK',
499  78048: 'EAST ELMHURST',
500  78352: 'BROOKLYN',
501  78383: 'CORONA',
502  79078: 'NEW YORK',
503  84489: 'BROOKLYN',
504  84518: 'BROOKLYN',
505  84688: 'BROOKLYN',
506  84695: 'BROOKLYN',
507  88812: 'JAMAICA',
508  89205: 'NEW YORK',
509  89382: 'BROOKLYN',
510  89734: 'NEW YORK',
511  93990: 'NEW YORK',
512  99407: 'JAMAICA',
513  99847: 'NEW YORK',
514  100073: 'NEW YORK',
515  101013: 'NEW YORK',
516  104020: 'JAMAICA',
517  106118: 'NEW YORK',
518  106499: 'EAST ELMHURST'},
519 'Landmark': {2869: 'BARCLAYS CENTER',
520  23571: 'CENTRAL PARK',
521  41625: 'J F K AIRPORT',
522  44331: 'J F K AIRPORT',
523  46913: 'BARTEL PRITCHARD SQUARE',
524  47459: 'CITY COLLEGE',
525  48465: 'PS 37',
526  51837: 'JACKIE ROBINSON PARK',
527  51848: 'JACKIE ROBINSON PARK',
528  54089: 'CENTRAL PARK',
529  54343: 'UNION SQUARE PARK',
530  55140: 'WORLDS FAIR MARINA',
531  57789: 'LA GUARDIA AIRPORT',
532  63119: 'J F K AIRPORT',
533  66242: 'LA GUARDIA AIRPORT',
534  66758: 'BRYANT PARK',
535  66786: 'BETH ISRAEL MED CENTER',
536  66809: 'J F K AIRPORT',
537  67465: 'QUEENS COLLEGE',
538  72424: 'BARCLAYS CENTER',
539  75531: 'CENTRAL PARK',
540  77918: 'CENTRAL PARK',
541  78048: 'LA GUARDIA AIRPORT',
542  78352: 'BARCLAYS CENTER',
543  78383: 'WORLDS FAIR MARINA',
544  79078: 'WASHINGTON SQUARE PARK',
545  84489: 'PROSPECT PARK',
546  84518: 'PROSPECT PARK',
547  84688: 'PROSPECT PARK',
548  84695: 'PROSPECT PARK',
549  88812: 'J F K AIRPORT',
550  89205: 'CENTRAL PARK',
551  89382: 'GRAND ARMY PLAZA',
552  89734: 'BRYANT PARK',
553  93990: 'BETH ISRAEL MED CENTER',
554  99407: 'J F K AIRPORT',
555  99847: 'PORT AUTH 42 STREET',
556  100073: 'MUSEUM NATURAL HIST',
557  101013: 'GRAND CENTRAL TERM',
558  104020: 'JFK',
559  106118: 'CENTRAL PARK',
560  106499: 'LA GUARDIA AIRPORT'},
561 'Status': {2869: 'Closed',
562  23571: 'Closed',
563  41625: 'Closed',
564  44331: 'Closed',
565  46913: 'Closed',
566  47459: 'Closed',
567  48465: 'Closed',
568  51837: 'Closed',
569  51848: 'Closed',
570  54089: 'Closed',
571  54343: 'Closed',
572  55140: 'Closed',
573  57789: 'Closed',
574  63119: 'Closed',
575  66242: 'Closed',
576  66758: 'Closed',
577  66786: 'Closed',
578  66809: 'Closed',
579  67465: 'Closed',
580  72424: 'Closed',
581  75531: 'Closed',
582  77918: 'Closed',
583  78048: 'Closed',
584  78352: 'Closed',
585  78383: 'Closed',
586  79078: 'Closed',
587  84489: 'Closed',
588  84518: 'Closed',
589  84688: 'Closed',
590  84695: 'Closed',
591  88812: 'Closed',
592  89205: 'Closed',
593  89382: 'Closed',
594  89734: 'Closed',
595  93990: 'Closed',
596  99407: 'Closed',
597  99847: 'Closed',
598  100073: 'Closed',
599  101013: 'Closed',
600  104020: 'Closed',
601  106118: 'Closed',
602  106499: 'Closed'},
603 'Borough': {2869: 'BROOKLYN',
604  23571: 'MANHATTAN',
605  41625: 'QUEENS',
606  44331: 'QUEENS',
607  46913: 'BROOKLYN',
608  47459: 'MANHATTAN',
609  48465: 'QUEENS',
610  51837: 'MANHATTAN',
611  51848: 'MANHATTAN',
612  54089: 'MANHATTAN',
613  54343: 'MANHATTAN',
614  55140: 'QUEENS',
615  57789: 'QUEENS',
616  63119: 'QUEENS',
617  66242: 'QUEENS',
618  66758: 'MANHATTAN',
619  66786: 'MANHATTAN',
620  66809: 'QUEENS',
621  67465: 'QUEENS',
622  72424: 'BROOKLYN',
623  75531: 'MANHATTAN',
624  77918: 'MANHATTAN',
625  78048: 'QUEENS',
626  78352: 'BROOKLYN',
627  78383: 'QUEENS',
628  79078: 'MANHATTAN',
629  84489: 'BROOKLYN',
630  84518: 'BROOKLYN',
631  84688: 'BROOKLYN',
632  84695: 'BROOKLYN',
633  88812: 'QUEENS',
634  89205: 'MANHATTAN',
635  89382: 'BROOKLYN',
636  89734: 'MANHATTAN',
637  93990: 'MANHATTAN',
638  99407: 'QUEENS',
639  99847: 'MANHATTAN',
640  100073: 'MANHATTAN',
641  101013: 'MANHATTAN',
642  104020: 'QUEENS',
643  106118: 'MANHATTAN',
644  106499: 'QUEENS'}}
645for c in ['Created Date', 'Closed Date']:
646    df[c] = pd.to_datetime(df[c])
647    #df[c+'_date'] = df[c].dt.date # to extract the date (for created + closed)
648    #df[c+'_time'] = df[c].dt.time # to extract the time (for created + closed)
649df['difference in time'] = (df['Closed Date'] - df['Created Date']).astype('timedelta64[h]')
650print(df.groupby('Complaint Type').agg({'difference in time':'mean'}))
651                            difference in time
652Complaint Type                                
653Dead Tree                            40.000000
654For Hire Vehicle Complaint           48.333333
655Illegal Parking                       0.500000
656Noise - Park                          1.916667
657Noise - Vehicle                       0.500000
658Overgrown Tree/Branches              15.000000
659Public Toilet                       125.500000
660Street Condition                    211.000000
661Taxi Complaint                      125.461538
662Traffic                               1.500000
663Vending                               3.666667
664

Source https://stackoverflow.com/questions/70447276

QUESTION

How do I resolve list index out of range error?

Asked 2021-Dec-18 at 07:42

I have a code that scrapes data into a dataframe

1import os
2import re
3import threading
4from multiprocessing.pool import ThreadPool
5import pandas as pd
6from bs4 import BeautifulSoup as bs
7from selenium import webdriver
8
9
10
11class Driver:
12    def __init__(self):
13        options = webdriver.ChromeOptions()
14        options.add_argument("--headless")
15        # Un-comment next line to supress logging:
16        options.add_experimental_option('excludeSwitches', ['enable-logging'])
17        self.driver = webdriver.Chrome(options=options)
18
19    def __del__(self):
20        self.driver.quit()  # clean up driver when we are cleaned up
21        # print('The driver has been "quitted".')
22
23
24threadLocal = threading.local()
25
26
27def create_driver():
28    the_driver = getattr(threadLocal, 'the_driver', None)
29    if the_driver is None:
30        the_driver = Driver()
31        setattr(threadLocal, 'the_driver', the_driver)
32    return the_driver.driver
33
34
35class GameData:
36    def __init__(self):
37        self.date = []
38        self.time = []
39        self.game = []
40        self.score = []
41        self.home_odds = []
42        self.draw_odds = []
43        self.away_odds = []
44        self.country = []
45        self.league = []
46
47
48def generate_matches(table):
49    tr_tags = table.findAll('tr')
50    for tr_tag in tr_tags:
51        if 'class' in tr_tag.attrs and 'dark' in tr_tag['class']:
52            th_tag = tr_tag.find('th', {'class': 'first2 tl'})
53            a_tags = th_tag.findAll('a')
54            country = a_tags[0].text
55            league = a_tags[1].text
56        else:
57            td_tags = tr_tag.findAll('td')
58            if len(td_tags) > 0:  # or just if td_tags
59                yield [td_tags[0].text, td_tags[1].text, td_tags[2].text, td_tags[3].text,
60                       td_tags[4].text, td_tags[5].text, country, league]
61
62
63def parse_data(url, return_urls=False):
64    browser = create_driver()
65    browser.get(url)
66    soup = bs(browser.page_source, "lxml")
67    div = soup.find('div', {'id': 'col-content'})
68    table = div.find('table', {'class': 'table-main'})
69    h1 = soup.find('h1').text
70    print(h1)
71    m = re.search(r'\d+ \w+ \d{4}$', h1)
72    game_date = m[0]
73    game_data = GameData()
74    for row in generate_matches(table):
75        game_data.date.append(game_date)
76        game_data.time.append(row[0])
77        game_data.game.append(row[1])
78        # Score present?
79        if ':' not in row[2]:
80            # No, shift a few columns right:
81            row[5], row[4], row[3], row[2] = row[4], row[3], row[2], nan
82        game_data.score.append(row[2])
83        game_data.home_odds.append(nan if row[3] == '-' else row[3])
84        game_data.draw_odds.append(nan if row[4] == '-' else row[4])
85        game_data.away_odds.append(nan if row[5] == '-' else row[5])
86        game_data.country.append(row[6])
87        game_data.league.append(row[7])
88
89    if return_urls:
90        span = soup.find('span', {'class': 'next-games-date'})
91        a_tags = span.findAll('a')
92        urls = ['https://www.oddsportal.com' + a_tag['href'] for a_tag in a_tags]
93        return game_data, urls
94    return game_data
95
96
97if __name__ == '__main__':
98    results = None
99    pool = ThreadPool(5)  # We will be getting, however, 7 URLs
100    # Get today's data and the Urls for the other days:
101    game_data_today, urls = pool.apply(parse_data, args=('https://www.oddsportal.com/matches/soccer', True))
102    urls.pop(1)  # Remove url for today: We already have the data for that
103    game_data_results = pool.imap(parse_data, urls)
104    for i in range(8):
105        try:
106            game_data = game_data_today if i == 1 else next(game_data_results)
107            result = pd.DataFrame(game_data.__dict__)
108            if results is None:
109                results = result
110            else:
111                results = results.append(result, ignore_index=True)
112    print(results)
113    # ensure all the drivers are "quitted":
114    del threadLocal
115    import gc
116
117    gc.collect()  # a little extra insurance
118

However I get partial output at:

1import os
2import re
3import threading
4from multiprocessing.pool import ThreadPool
5import pandas as pd
6from bs4 import BeautifulSoup as bs
7from selenium import webdriver
8
9
10
11class Driver:
12    def __init__(self):
13        options = webdriver.ChromeOptions()
14        options.add_argument("--headless")
15        # Un-comment next line to supress logging:
16        options.add_experimental_option('excludeSwitches', ['enable-logging'])
17        self.driver = webdriver.Chrome(options=options)
18
19    def __del__(self):
20        self.driver.quit()  # clean up driver when we are cleaned up
21        # print('The driver has been "quitted".')
22
23
24threadLocal = threading.local()
25
26
27def create_driver():
28    the_driver = getattr(threadLocal, 'the_driver', None)
29    if the_driver is None:
30        the_driver = Driver()
31        setattr(threadLocal, 'the_driver', the_driver)
32    return the_driver.driver
33
34
35class GameData:
36    def __init__(self):
37        self.date = []
38        self.time = []
39        self.game = []
40        self.score = []
41        self.home_odds = []
42        self.draw_odds = []
43        self.away_odds = []
44        self.country = []
45        self.league = []
46
47
48def generate_matches(table):
49    tr_tags = table.findAll('tr')
50    for tr_tag in tr_tags:
51        if 'class' in tr_tag.attrs and 'dark' in tr_tag['class']:
52            th_tag = tr_tag.find('th', {'class': 'first2 tl'})
53            a_tags = th_tag.findAll('a')
54            country = a_tags[0].text
55            league = a_tags[1].text
56        else:
57            td_tags = tr_tag.findAll('td')
58            if len(td_tags) > 0:  # or just if td_tags
59                yield [td_tags[0].text, td_tags[1].text, td_tags[2].text, td_tags[3].text,
60                       td_tags[4].text, td_tags[5].text, country, league]
61
62
63def parse_data(url, return_urls=False):
64    browser = create_driver()
65    browser.get(url)
66    soup = bs(browser.page_source, "lxml")
67    div = soup.find('div', {'id': 'col-content'})
68    table = div.find('table', {'class': 'table-main'})
69    h1 = soup.find('h1').text
70    print(h1)
71    m = re.search(r'\d+ \w+ \d{4}$', h1)
72    game_date = m[0]
73    game_data = GameData()
74    for row in generate_matches(table):
75        game_data.date.append(game_date)
76        game_data.time.append(row[0])
77        game_data.game.append(row[1])
78        # Score present?
79        if ':' not in row[2]:
80            # No, shift a few columns right:
81            row[5], row[4], row[3], row[2] = row[4], row[3], row[2], nan
82        game_data.score.append(row[2])
83        game_data.home_odds.append(nan if row[3] == '-' else row[3])
84        game_data.draw_odds.append(nan if row[4] == '-' else row[4])
85        game_data.away_odds.append(nan if row[5] == '-' else row[5])
86        game_data.country.append(row[6])
87        game_data.league.append(row[7])
88
89    if return_urls:
90        span = soup.find('span', {'class': 'next-games-date'})
91        a_tags = span.findAll('a')
92        urls = ['https://www.oddsportal.com' + a_tag['href'] for a_tag in a_tags]
93        return game_data, urls
94    return game_data
95
96
97if __name__ == '__main__':
98    results = None
99    pool = ThreadPool(5)  # We will be getting, however, 7 URLs
100    # Get today's data and the Urls for the other days:
101    game_data_today, urls = pool.apply(parse_data, args=('https://www.oddsportal.com/matches/soccer', True))
102    urls.pop(1)  # Remove url for today: We already have the data for that
103    game_data_results = pool.imap(parse_data, urls)
104    for i in range(8):
105        try:
106            game_data = game_data_today if i == 1 else next(game_data_results)
107            result = pd.DataFrame(game_data.__dict__)
108            if results is None:
109                results = result
110            else:
111                results = results.append(result, ignore_index=True)
112    print(results)
113    # ensure all the drivers are "quitted":
114    del threadLocal
115    import gc
116
117    gc.collect()  # a little extra insurance
118h1 = soup.find('h1').text
119print(h1)
120
121Next Soccer Matches: Today, 18 Dec 2021
122Next Soccer Matches: Wednesday, 22 Dec 2021
123Next Soccer Matches: Thursday, 23 Dec 2021
124Next Soccer Matches: Friday, 24 Dec 2021
125Next Soccer Matches: Tuesday, 21 Dec 2021
126Next Soccer Matches: Monday, 20 Dec 2021
127Next Soccer Matches: Yesterday, 17 Dec 2021
128Next Soccer Matches: Tomorrow, 19 Dec 2021
129

While I checked, for the argument

When I explore further

1import os
2import re
3import threading
4from multiprocessing.pool import ThreadPool
5import pandas as pd
6from bs4 import BeautifulSoup as bs
7from selenium import webdriver
8
9
10
11class Driver:
12    def __init__(self):
13        options = webdriver.ChromeOptions()
14        options.add_argument("--headless")
15        # Un-comment next line to supress logging:
16        options.add_experimental_option('excludeSwitches', ['enable-logging'])
17        self.driver = webdriver.Chrome(options=options)
18
19    def __del__(self):
20        self.driver.quit()  # clean up driver when we are cleaned up
21        # print('The driver has been "quitted".')
22
23
24threadLocal = threading.local()
25
26
27def create_driver():
28    the_driver = getattr(threadLocal, 'the_driver', None)
29    if the_driver is None:
30        the_driver = Driver()
31        setattr(threadLocal, 'the_driver', the_driver)
32    return the_driver.driver
33
34
35class GameData:
36    def __init__(self):
37        self.date = []
38        self.time = []
39        self.game = []
40        self.score = []
41        self.home_odds = []
42        self.draw_odds = []
43        self.away_odds = []
44        self.country = []
45        self.league = []
46
47
48def generate_matches(table):
49    tr_tags = table.findAll('tr')
50    for tr_tag in tr_tags:
51        if 'class' in tr_tag.attrs and 'dark' in tr_tag['class']:
52            th_tag = tr_tag.find('th', {'class': 'first2 tl'})
53            a_tags = th_tag.findAll('a')
54            country = a_tags[0].text
55            league = a_tags[1].text
56        else:
57            td_tags = tr_tag.findAll('td')
58            if len(td_tags) > 0:  # or just if td_tags
59                yield [td_tags[0].text, td_tags[1].text, td_tags[2].text, td_tags[3].text,
60                       td_tags[4].text, td_tags[5].text, country, league]
61
62
63def parse_data(url, return_urls=False):
64    browser = create_driver()
65    browser.get(url)
66    soup = bs(browser.page_source, "lxml")
67    div = soup.find('div', {'id': 'col-content'})
68    table = div.find('table', {'class': 'table-main'})
69    h1 = soup.find('h1').text
70    print(h1)
71    m = re.search(r'\d+ \w+ \d{4}$', h1)
72    game_date = m[0]
73    game_data = GameData()
74    for row in generate_matches(table):
75        game_data.date.append(game_date)
76        game_data.time.append(row[0])
77        game_data.game.append(row[1])
78        # Score present?
79        if ':' not in row[2]:
80            # No, shift a few columns right:
81            row[5], row[4], row[3], row[2] = row[4], row[3], row[2], nan
82        game_data.score.append(row[2])
83        game_data.home_odds.append(nan if row[3] == '-' else row[3])
84        game_data.draw_odds.append(nan if row[4] == '-' else row[4])
85        game_data.away_odds.append(nan if row[5] == '-' else row[5])
86        game_data.country.append(row[6])
87        game_data.league.append(row[7])
88
89    if return_urls:
90        span = soup.find('span', {'class': 'next-games-date'})
91        a_tags = span.findAll('a')
92        urls = ['https://www.oddsportal.com' + a_tag['href'] for a_tag in a_tags]
93        return game_data, urls
94    return game_data
95
96
97if __name__ == '__main__':
98    results = None
99    pool = ThreadPool(5)  # We will be getting, however, 7 URLs
100    # Get today's data and the Urls for the other days:
101    game_data_today, urls = pool.apply(parse_data, args=('https://www.oddsportal.com/matches/soccer', True))
102    urls.pop(1)  # Remove url for today: We already have the data for that
103    game_data_results = pool.imap(parse_data, urls)
104    for i in range(8):
105        try:
106            game_data = game_data_today if i == 1 else next(game_data_results)
107            result = pd.DataFrame(game_data.__dict__)
108            if results is None:
109                results = result
110            else:
111                results = results.append(result, ignore_index=True)
112    print(results)
113    # ensure all the drivers are "quitted":
114    del threadLocal
115    import gc
116
117    gc.collect()  # a little extra insurance
118h1 = soup.find('h1').text
119print(h1)
120
121Next Soccer Matches: Today, 18 Dec 2021
122Next Soccer Matches: Wednesday, 22 Dec 2021
123Next Soccer Matches: Thursday, 23 Dec 2021
124Next Soccer Matches: Friday, 24 Dec 2021
125Next Soccer Matches: Tuesday, 21 Dec 2021
126Next Soccer Matches: Monday, 20 Dec 2021
127Next Soccer Matches: Yesterday, 17 Dec 2021
128Next Soccer Matches: Tomorrow, 19 Dec 2021
129        td_tags = tr_tag.findAll('td')
130        print(td_tags)
131        if len(td_tags) > 0:  # or just if td_tags
132            print(len(td_tags))
133

I get 2 cases

1import os
2import re
3import threading
4from multiprocessing.pool import ThreadPool
5import pandas as pd
6from bs4 import BeautifulSoup as bs
7from selenium import webdriver
8
9
10
11class Driver:
12    def __init__(self):
13        options = webdriver.ChromeOptions()
14        options.add_argument("--headless")
15        # Un-comment next line to supress logging:
16        options.add_experimental_option('excludeSwitches', ['enable-logging'])
17        self.driver = webdriver.Chrome(options=options)
18
19    def __del__(self):
20        self.driver.quit()  # clean up driver when we are cleaned up
21        # print('The driver has been "quitted".')
22
23
24threadLocal = threading.local()
25
26
27def create_driver():
28    the_driver = getattr(threadLocal, 'the_driver', None)
29    if the_driver is None:
30        the_driver = Driver()
31        setattr(threadLocal, 'the_driver', the_driver)
32    return the_driver.driver
33
34
35class GameData:
36    def __init__(self):
37        self.date = []
38        self.time = []
39        self.game = []
40        self.score = []
41        self.home_odds = []
42        self.draw_odds = []
43        self.away_odds = []
44        self.country = []
45        self.league = []
46
47
48def generate_matches(table):
49    tr_tags = table.findAll('tr')
50    for tr_tag in tr_tags:
51        if 'class' in tr_tag.attrs and 'dark' in tr_tag['class']:
52            th_tag = tr_tag.find('th', {'class': 'first2 tl'})
53            a_tags = th_tag.findAll('a')
54            country = a_tags[0].text
55            league = a_tags[1].text
56        else:
57            td_tags = tr_tag.findAll('td')
58            if len(td_tags) > 0:  # or just if td_tags
59                yield [td_tags[0].text, td_tags[1].text, td_tags[2].text, td_tags[3].text,
60                       td_tags[4].text, td_tags[5].text, country, league]
61
62
63def parse_data(url, return_urls=False):
64    browser = create_driver()
65    browser.get(url)
66    soup = bs(browser.page_source, "lxml")
67    div = soup.find('div', {'id': 'col-content'})
68    table = div.find('table', {'class': 'table-main'})
69    h1 = soup.find('h1').text
70    print(h1)
71    m = re.search(r'\d+ \w+ \d{4}$', h1)
72    game_date = m[0]
73    game_data = GameData()
74    for row in generate_matches(table):
75        game_data.date.append(game_date)
76        game_data.time.append(row[0])
77        game_data.game.append(row[1])
78        # Score present?
79        if ':' not in row[2]:
80            # No, shift a few columns right:
81            row[5], row[4], row[3], row[2] = row[4], row[3], row[2], nan
82        game_data.score.append(row[2])
83        game_data.home_odds.append(nan if row[3] == '-' else row[3])
84        game_data.draw_odds.append(nan if row[4] == '-' else row[4])
85        game_data.away_odds.append(nan if row[5] == '-' else row[5])
86        game_data.country.append(row[6])
87        game_data.league.append(row[7])
88
89    if return_urls:
90        span = soup.find('span', {'class': 'next-games-date'})
91        a_tags = span.findAll('a')
92        urls = ['https://www.oddsportal.com' + a_tag['href'] for a_tag in a_tags]
93        return game_data, urls
94    return game_data
95
96
97if __name__ == '__main__':
98    results = None
99    pool = ThreadPool(5)  # We will be getting, however, 7 URLs
100    # Get today's data and the Urls for the other days:
101    game_data_today, urls = pool.apply(parse_data, args=('https://www.oddsportal.com/matches/soccer', True))
102    urls.pop(1)  # Remove url for today: We already have the data for that
103    game_data_results = pool.imap(parse_data, urls)
104    for i in range(8):
105        try:
106            game_data = game_data_today if i == 1 else next(game_data_results)
107            result = pd.DataFrame(game_data.__dict__)
108            if results is None:
109                results = result
110            else:
111                results = results.append(result, ignore_index=True)
112    print(results)
113    # ensure all the drivers are "quitted":
114    del threadLocal
115    import gc
116
117    gc.collect()  # a little extra insurance
118h1 = soup.find('h1').text
119print(h1)
120
121Next Soccer Matches: Today, 18 Dec 2021
122Next Soccer Matches: Wednesday, 22 Dec 2021
123Next Soccer Matches: Thursday, 23 Dec 2021
124Next Soccer Matches: Friday, 24 Dec 2021
125Next Soccer Matches: Tuesday, 21 Dec 2021
126Next Soccer Matches: Monday, 20 Dec 2021
127Next Soccer Matches: Yesterday, 17 Dec 2021
128Next Soccer Matches: Tomorrow, 19 Dec 2021
129        td_tags = tr_tag.findAll('td')
130        print(td_tags)
131        if len(td_tags) > 0:  # or just if td_tags
132            print(len(td_tags))
133[<td class="table-time datet t1639839600-1-1-0-0">15:00</td>, <td class="name table-participant"><a href="/soccer/england/npl-premier-division/south-shields-witton-albion-COXLb3wr/">South Shields - Witton</a><span class="ico-event-info" onmouseout="allowHideTootip(true);delayHideTip(200);" onmouseover="toolTip('Postponed due to Covid-19.', this, event, '4');allowHideTootip(false);delayHideTip(200);return false;"> </span></td>, <td class="center bold table-odds table-score">postp.</td>, <td class="odds-nowrp" xodd="1.36" xoid="E-4v5i6xv464x0xd4ur7"><a href="" onclick="globals.ch.togle(this , 'E-4v5i6xv464x0xd4ur7');return false;" xparam="odds_text">1.36</a></td>, <td class="odds-nowrp" xodd="4.84" xoid="E-4v5i6xv498x0x0"><a href="" onclick="globals.ch.togle(this , 'E-4v5i6xv498x0x0');return false;" xparam="odds_text">4.84</a></td>, <td class="odds-nowrp" xodd="7.05" xoid="E-4v5i6xv464x0xd4ur8"><a href="" onclick="globals.ch.togle(this , 'E-4v5i6xv464x0xd4ur8');return false;" xparam="odds_text">7.05</a></td>, <td class="center info-value">11</td>]
1347
135[<td class="table-time datet t1639839600-1-1-0-0">15:00</td>, <td class="name table-participant" colspan="2"><a href="/soccer/england/npl-premier-division/warrington-town-scarborough-athletic-ObyQcNhl/">Warrington - Scarborough</a></td>, <td class="odds-nowrp" xodd="1.8" xoid="E-4v5i7xv464x0xd4ur9"><a href="" onclick="globals.ch.togle(this , 'E-4v5i7xv464x0xd4ur9');return false;" xparam="odds_text">1.80</a></td>, <td class="odds-nowrp" xodd="3.59" xoid="E-4v5i7xv498x0x0"><a href="" onclick="globals.ch.togle(this , 'E-4v5i7xv498x0x0');return false;" xparam="odds_text">3.59</a></td>, <td class="odds-nowrp" xodd="3.91" xoid="E-4v5i7xv464x0xd4ura"><a href="" onclick="globals.ch.togle(this , 'E-4v5i7xv464x0xd4ura');return false;" xparam="odds_text">3.91</a></td>, <td class="center info-value">12</td>]
1366
137

I am getting an IndexError at:

1import os
2import re
3import threading
4from multiprocessing.pool import ThreadPool
5import pandas as pd
6from bs4 import BeautifulSoup as bs
7from selenium import webdriver
8
9
10
11class Driver:
12    def __init__(self):
13        options = webdriver.ChromeOptions()
14        options.add_argument("--headless")
15        # Un-comment next line to supress logging:
16        options.add_experimental_option('excludeSwitches', ['enable-logging'])
17        self.driver = webdriver.Chrome(options=options)
18
19    def __del__(self):
20        self.driver.quit()  # clean up driver when we are cleaned up
21        # print('The driver has been "quitted".')
22
23
24threadLocal = threading.local()
25
26
27def create_driver():
28    the_driver = getattr(threadLocal, 'the_driver', None)
29    if the_driver is None:
30        the_driver = Driver()
31        setattr(threadLocal, 'the_driver', the_driver)
32    return the_driver.driver
33
34
35class GameData:
36    def __init__(self):
37        self.date = []
38        self.time = []
39        self.game = []
40        self.score = []
41        self.home_odds = []
42        self.draw_odds = []
43        self.away_odds = []
44        self.country = []
45        self.league = []
46
47
48def generate_matches(table):
49    tr_tags = table.findAll('tr')
50    for tr_tag in tr_tags:
51        if 'class' in tr_tag.attrs and 'dark' in tr_tag['class']:
52            th_tag = tr_tag.find('th', {'class': 'first2 tl'})
53            a_tags = th_tag.findAll('a')
54            country = a_tags[0].text
55            league = a_tags[1].text
56        else:
57            td_tags = tr_tag.findAll('td')
58            if len(td_tags) > 0:  # or just if td_tags
59                yield [td_tags[0].text, td_tags[1].text, td_tags[2].text, td_tags[3].text,
60                       td_tags[4].text, td_tags[5].text, country, league]
61
62
63def parse_data(url, return_urls=False):
64    browser = create_driver()
65    browser.get(url)
66    soup = bs(browser.page_source, "lxml")
67    div = soup.find('div', {'id': 'col-content'})
68    table = div.find('table', {'class': 'table-main'})
69    h1 = soup.find('h1').text
70    print(h1)
71    m = re.search(r'\d+ \w+ \d{4}$', h1)
72    game_date = m[0]
73    game_data = GameData()
74    for row in generate_matches(table):
75        game_data.date.append(game_date)
76        game_data.time.append(row[0])
77        game_data.game.append(row[1])
78        # Score present?
79        if ':' not in row[2]:
80            # No, shift a few columns right:
81            row[5], row[4], row[3], row[2] = row[4], row[3], row[2], nan
82        game_data.score.append(row[2])
83        game_data.home_odds.append(nan if row[3] == '-' else row[3])
84        game_data.draw_odds.append(nan if row[4] == '-' else row[4])
85        game_data.away_odds.append(nan if row[5] == '-' else row[5])
86        game_data.country.append(row[6])
87        game_data.league.append(row[7])
88
89    if return_urls:
90        span = soup.find('span', {'class': 'next-games-date'})
91        a_tags = span.findAll('a')
92        urls = ['https://www.oddsportal.com' + a_tag['href'] for a_tag in a_tags]
93        return game_data, urls
94    return game_data
95
96
97if __name__ == '__main__':
98    results = None
99    pool = ThreadPool(5)  # We will be getting, however, 7 URLs
100    # Get today's data and the Urls for the other days:
101    game_data_today, urls = pool.apply(parse_data, args=('https://www.oddsportal.com/matches/soccer', True))
102    urls.pop(1)  # Remove url for today: We already have the data for that
103    game_data_results = pool.imap(parse_data, urls)
104    for i in range(8):
105        try:
106            game_data = game_data_today if i == 1 else next(game_data_results)
107            result = pd.DataFrame(game_data.__dict__)
108            if results is None:
109                results = result
110            else:
111                results = results.append(result, ignore_index=True)
112    print(results)
113    # ensure all the drivers are "quitted":
114    del threadLocal
115    import gc
116
117    gc.collect()  # a little extra insurance
118h1 = soup.find('h1').text
119print(h1)
120
121Next Soccer Matches: Today, 18 Dec 2021
122Next Soccer Matches: Wednesday, 22 Dec 2021
123Next Soccer Matches: Thursday, 23 Dec 2021
124Next Soccer Matches: Friday, 24 Dec 2021
125Next Soccer Matches: Tuesday, 21 Dec 2021
126Next Soccer Matches: Monday, 20 Dec 2021
127Next Soccer Matches: Yesterday, 17 Dec 2021
128Next Soccer Matches: Tomorrow, 19 Dec 2021
129        td_tags = tr_tag.findAll('td')
130        print(td_tags)
131        if len(td_tags) > 0:  # or just if td_tags
132            print(len(td_tags))
133[<td class="table-time datet t1639839600-1-1-0-0">15:00</td>, <td class="name table-participant"><a href="/soccer/england/npl-premier-division/south-shields-witton-albion-COXLb3wr/">South Shields - Witton</a><span class="ico-event-info" onmouseout="allowHideTootip(true);delayHideTip(200);" onmouseover="toolTip('Postponed due to Covid-19.', this, event, '4');allowHideTootip(false);delayHideTip(200);return false;"> </span></td>, <td class="center bold table-odds table-score">postp.</td>, <td class="odds-nowrp" xodd="1.36" xoid="E-4v5i6xv464x0xd4ur7"><a href="" onclick="globals.ch.togle(this , 'E-4v5i6xv464x0xd4ur7');return false;" xparam="odds_text">1.36</a></td>, <td class="odds-nowrp" xodd="4.84" xoid="E-4v5i6xv498x0x0"><a href="" onclick="globals.ch.togle(this , 'E-4v5i6xv498x0x0');return false;" xparam="odds_text">4.84</a></td>, <td class="odds-nowrp" xodd="7.05" xoid="E-4v5i6xv464x0xd4ur8"><a href="" onclick="globals.ch.togle(this , 'E-4v5i6xv464x0xd4ur8');return false;" xparam="odds_text">7.05</a></td>, <td class="center info-value">11</td>]
1347
135[<td class="table-time datet t1639839600-1-1-0-0">15:00</td>, <td class="name table-participant" colspan="2"><a href="/soccer/england/npl-premier-division/warrington-town-scarborough-athletic-ObyQcNhl/">Warrington - Scarborough</a></td>, <td class="odds-nowrp" xodd="1.8" xoid="E-4v5i7xv464x0xd4ur9"><a href="" onclick="globals.ch.togle(this , 'E-4v5i7xv464x0xd4ur9');return false;" xparam="odds_text">1.80</a></td>, <td class="odds-nowrp" xodd="3.59" xoid="E-4v5i7xv498x0x0"><a href="" onclick="globals.ch.togle(this , 'E-4v5i7xv498x0x0');return false;" xparam="odds_text">3.59</a></td>, <td class="odds-nowrp" xodd="3.91" xoid="E-4v5i7xv464x0xd4ura"><a href="" onclick="globals.ch.togle(this , 'E-4v5i7xv464x0xd4ura');return false;" xparam="odds_text">3.91</a></td>, <td class="center info-value">12</td>]
1366
137line 67, in generate_matches
138    yield [td_tags[0].text, td_tags[1].text, td_tags[2].text, td_tags[3].text,
139IndexError: list index out of range
140

How can I resolve this?

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque pellentesque, ipsum vel tempor suscipit, turpis mauris venenatis leo, nec vestibulum arcu urna et quam

ANSWER

Answered 2021-Dec-18 at 07:42

The fact that you get an IndexError when accessing td_tags implies that len(td_tags) is not always 6 or 7. It is <6 at least once.

Look at this bit of code:

1import os
2import re
3import threading
4from multiprocessing.pool import ThreadPool
5import pandas as pd
6from bs4 import BeautifulSoup as bs
7from selenium import webdriver
8
9
10
11class Driver:
12    def __init__(self):
13        options = webdriver.ChromeOptions()
14        options.add_argument(&quot;--headless&quot;)
15        # Un-comment next line to supress logging:
16        options.add_experimental_option('excludeSwitches', ['enable-logging'])
17        self.driver = webdriver.Chrome(options=options)
18
19    def __del__(self):
20        self.driver.quit()  # clean up driver when we are cleaned up
21        # print('The driver has been &quot;quitted&quot;.')
22
23
24threadLocal = threading.local()
25
26
27def create_driver():
28    the_driver = getattr(threadLocal, 'the_driver', None)
29    if the_driver is None:
30        the_driver = Driver()
31        setattr(threadLocal, 'the_driver', the_driver)
32    return the_driver.driver
33
34
35class GameData:
36    def __init__(self):
37        self.date = []
38        self.time = []
39        self.game = []
40        self.score = []
41        self.home_odds = []
42        self.draw_odds = []
43        self.away_odds = []
44        self.country = []
45        self.league = []
46
47
48def generate_matches(table):
49    tr_tags = table.findAll('tr')
50    for tr_tag in tr_tags:
51        if 'class' in tr_tag.attrs and 'dark' in tr_tag['class']:
52            th_tag = tr_tag.find('th', {'class': 'first2 tl'})
53            a_tags = th_tag.findAll('a')
54            country = a_tags[0].text
55            league = a_tags[1].text
56        else:
57            td_tags = tr_tag.findAll('td')
58            if len(td_tags) &gt; 0:  # or just if td_tags
59                yield [td_tags[0].text, td_tags[1].text, td_tags[2].text, td_tags[3].text,
60                       td_tags[4].text, td_tags[5].text, country, league]
61
62
63def parse_data(url, return_urls=False):
64    browser = create_driver()
65    browser.get(url)
66    soup = bs(browser.page_source, &quot;lxml&quot;)
67    div = soup.find('div', {'id': 'col-content'})
68    table = div.find('table', {'class': 'table-main'})
69    h1 = soup.find('h1').text
70    print(h1)
71    m = re.search(r'\d+ \w+ \d{4}$', h1)
72    game_date = m[0]
73    game_data = GameData()
74    for row in generate_matches(table):
75        game_data.date.append(game_date)
76        game_data.time.append(row[0])
77        game_data.game.append(row[1])
78        # Score present?
79        if ':' not in row[2]:
80            # No, shift a few columns right:
81            row[5], row[4], row[3], row[2] = row[4], row[3], row[2], nan
82        game_data.score.append(row[2])
83        game_data.home_odds.append(nan if row[3] == '-' else row[3])
84        game_data.draw_odds.append(nan if row[4] == '-' else row[4])
85        game_data.away_odds.append(nan if row[5] == '-' else row[5])
86        game_data.country.append(row[6])
87        game_data.league.append(row[7])
88
89    if return_urls:
90        span = soup.find('span', {'class': 'next-games-date'})
91        a_tags = span.findAll('a')
92        urls = ['https://www.oddsportal.com' + a_tag['href'] for a_tag in a_tags]
93        return game_data, urls
94    return game_data
95
96
97if __name__ == '__main__':
98    results = None
99    pool = ThreadPool(5)  # We will be getting, however, 7 URLs
100    # Get today's data and the Urls for the other days:
101    game_data_today, urls = pool.apply(parse_data, args=('https://www.oddsportal.com/matches/soccer', True))
102    urls.pop(1)  # Remove url for today: We already have the data for that
103    game_data_results = pool.imap(parse_data, urls)
104    for i in range(8):
105        try:
106            game_data = game_data_today if i == 1 else next(game_data_results)
107            result = pd.DataFrame(game_data.__dict__)
108            if results is None:
109                results = result
110            else:
111                results = results.append(result, ignore_index=True)
112    print(results)
113    # ensure all the drivers are &quot;quitted&quot;:
114    del threadLocal
115    import gc
116
117    gc.collect()  # a little extra insurance
118h1 = soup.find('h1').text
119print(h1)
120
121Next Soccer Matches: Today, 18 Dec 2021
122Next Soccer Matches: Wednesday, 22 Dec 2021
123Next Soccer Matches: Thursday, 23 Dec 2021
124Next Soccer Matches: Friday, 24 Dec 2021
125Next Soccer Matches: Tuesday, 21 Dec 2021
126Next Soccer Matches: Monday, 20 Dec 2021
127Next Soccer Matches: Yesterday, 17 Dec 2021
128Next Soccer Matches: Tomorrow, 19 Dec 2021
129        td_tags = tr_tag.findAll('td')
130        print(td_tags)
131        if len(td_tags) &gt; 0:  # or just if td_tags
132            print(len(td_tags))
133[&lt;td class=&quot;table-time datet t1639839600-1-1-0-0&quot;&gt;15:00&lt;/td&gt;, &lt;td class=&quot;name table-participant&quot;&gt;&lt;a href=&quot;/soccer/england/npl-premier-division/south-shields-witton-albion-COXLb3wr/&quot;&gt;South Shields - Witton&lt;/a&gt;&lt;span class=&quot;ico-event-info&quot; onmouseout=&quot;allowHideTootip(true);delayHideTip(200);&quot; onmouseover=&quot;toolTip('Postponed due to Covid-19.', this, event, '4');allowHideTootip(false);delayHideTip(200);return false;&quot;&gt; &lt;/span&gt;&lt;/td&gt;, &lt;td class=&quot;center bold table-odds table-score&quot;&gt;postp.&lt;/td&gt;, &lt;td class=&quot;odds-nowrp&quot; xodd=&quot;1.36&quot; xoid=&quot;E-4v5i6xv464x0xd4ur7&quot;&gt;&lt;a href=&quot;&quot; onclick=&quot;globals.ch.togle(this , 'E-4v5i6xv464x0xd4ur7');return false;&quot; xparam=&quot;odds_text&quot;&gt;1.36&lt;/a&gt;&lt;/td&gt;, &lt;td class=&quot;odds-nowrp&quot; xodd=&quot;4.84&quot; xoid=&quot;E-4v5i6xv498x0x0&quot;&gt;&lt;a href=&quot;&quot; onclick=&quot;globals.ch.togle(this , 'E-4v5i6xv498x0x0');return false;&quot; xparam=&quot;odds_text&quot;&gt;4.84&lt;/a&gt;&lt;/td&gt;, &lt;td class=&quot;odds-nowrp&quot; xodd=&quot;7.05&quot; xoid=&quot;E-4v5i6xv464x0xd4ur8&quot;&gt;&lt;a href=&quot;&quot; onclick=&quot;globals.ch.togle(this , 'E-4v5i6xv464x0xd4ur8');return false;&quot; xparam=&quot;odds_text&quot;&gt;7.05&lt;/a&gt;&lt;/td&gt;, &lt;td class=&quot;center info-value&quot;&gt;11&lt;/td&gt;]
1347
135[&lt;td class=&quot;table-time datet t1639839600-1-1-0-0&quot;&gt;15:00&lt;/td&gt;, &lt;td class=&quot;name table-participant&quot; colspan=&quot;2&quot;&gt;&lt;a href=&quot;/soccer/england/npl-premier-division/warrington-town-scarborough-athletic-ObyQcNhl/&quot;&gt;Warrington - Scarborough&lt;/a&gt;&lt;/td&gt;, &lt;td class=&quot;odds-nowrp&quot; xodd=&quot;1.8&quot; xoid=&quot;E-4v5i7xv464x0xd4ur9&quot;&gt;&lt;a href=&quot;&quot; onclick=&quot;globals.ch.togle(this , 'E-4v5i7xv464x0xd4ur9');return false;&quot; xparam=&quot;odds_text&quot;&gt;1.80&lt;/a&gt;&lt;/td&gt;, &lt;td class=&quot;odds-nowrp&quot; xodd=&quot;3.59&quot; xoid=&quot;E-4v5i7xv498x0x0&quot;&gt;&lt;a href=&quot;&quot; onclick=&quot;globals.ch.togle(this , 'E-4v5i7xv498x0x0');return false;&quot; xparam=&quot;odds_text&quot;&gt;3.59&lt;/a&gt;&lt;/td&gt;, &lt;td class=&quot;odds-nowrp&quot; xodd=&quot;3.91&quot; xoid=&quot;E-4v5i7xv464x0xd4ura&quot;&gt;&lt;a href=&quot;&quot; onclick=&quot;globals.ch.togle(this , 'E-4v5i7xv464x0xd4ura');return false;&quot; xparam=&quot;odds_text&quot;&gt;3.91&lt;/a&gt;&lt;/td&gt;, &lt;td class=&quot;center info-value&quot;&gt;12&lt;/td&gt;]
1366
137line 67, in generate_matches
138    yield [td_tags[0].text, td_tags[1].text, td_tags[2].text, td_tags[3].text,
139IndexError: list index out of range
140if len(td_tags) &gt; 0:  # or just if td_tags
141    yield [td_tags[0].text, td_tags[1].text, td_tags[2].text, td_tags[3].text,
142           td_tags[4].text, td_tags[5].text, country, league]
143

You are using list indices 0 up to and including 5. To not get an IndexError requires len(td_tags) of at least 6! So change the first line to:

1import os
2import re
3import threading
4from multiprocessing.pool import ThreadPool
5import pandas as pd
6from bs4 import BeautifulSoup as bs
7from selenium import webdriver
8
9
10
11class Driver:
12    def __init__(self):
13        options = webdriver.ChromeOptions()
14        options.add_argument(&quot;--headless&quot;)
15        # Un-comment next line to supress logging:
16        options.add_experimental_option('excludeSwitches', ['enable-logging'])
17        self.driver = webdriver.Chrome(options=options)
18
19    def __del__(self):
20        self.driver.quit()  # clean up driver when we are cleaned up
21        # print('The driver has been &quot;quitted&quot;.')
22
23
24threadLocal = threading.local()
25
26
27def create_driver():
28    the_driver = getattr(threadLocal, 'the_driver', None)
29    if the_driver is None:
30        the_driver = Driver()
31        setattr(threadLocal, 'the_driver', the_driver)
32    return the_driver.driver
33
34
35class GameData:
36    def __init__(self):
37        self.date = []
38        self.time = []
39        self.game = []
40        self.score = []
41        self.home_odds = []
42        self.draw_odds = []
43        self.away_odds = []
44        self.country = []
45        self.league = []
46
47
48def generate_matches(table):
49    tr_tags = table.findAll('tr')
50    for tr_tag in tr_tags:
51        if 'class' in tr_tag.attrs and 'dark' in tr_tag['class']:
52            th_tag = tr_tag.find('th', {'class': 'first2 tl'})
53            a_tags = th_tag.findAll('a')
54            country = a_tags[0].text
55            league = a_tags[1].text
56        else:
57            td_tags = tr_tag.findAll('td')
58            if len(td_tags) &gt; 0:  # or just if td_tags
59                yield [td_tags[0].text, td_tags[1].text, td_tags[2].text, td_tags[3].text,
60                       td_tags[4].text, td_tags[5].text, country, league]
61
62
63def parse_data(url, return_urls=False):
64    browser = create_driver()
65    browser.get(url)
66    soup = bs(browser.page_source, &quot;lxml&quot;)
67    div = soup.find('div', {'id': 'col-content'})
68    table = div.find('table', {'class': 'table-main'})
69    h1 = soup.find('h1').text
70    print(h1)
71    m = re.search(r'\d+ \w+ \d{4}$', h1)
72    game_date = m[0]
73    game_data = GameData()
74    for row in generate_matches(table):
75        game_data.date.append(game_date)
76        game_data.time.append(row[0])
77        game_data.game.append(row[1])
78        # Score present?
79        if ':' not in row[2]:
80            # No, shift a few columns right:
81            row[5], row[4], row[3], row[2] = row[4], row[3], row[2], nan
82        game_data.score.append(row[2])
83        game_data.home_odds.append(nan if row[3] == '-' else row[3])
84        game_data.draw_odds.append(nan if row[4] == '-' else row[4])
85        game_data.away_odds.append(nan if row[5] == '-' else row[5])
86        game_data.country.append(row[6])
87        game_data.league.append(row[7])
88
89    if return_urls:
90        span = soup.find('span', {'class': 'next-games-date'})
91        a_tags = span.findAll('a')
92        urls = ['https://www.oddsportal.com' + a_tag['href'] for a_tag in a_tags]
93        return game_data, urls
94    return game_data
95
96
97if __name__ == '__main__':
98    results = None
99    pool = ThreadPool(5)  # We will be getting, however, 7 URLs
100    # Get today's data and the Urls for the other days:
101    game_data_today, urls = pool.apply(parse_data, args=('https://www.oddsportal.com/matches/soccer', True))
102    urls.pop(1)  # Remove url for today: We already have the data for that
103    game_data_results = pool.imap(parse_data, urls)
104    for i in range(8):
105        try:
106            game_data = game_data_today if i == 1 else next(game_data_results)
107            result = pd.DataFrame(game_data.__dict__)
108            if results is None:
109                results = result
110            else:
111                results = results.append(result, ignore_index=True)
112    print(results)
113    # ensure all the drivers are &quot;quitted&quot;:
114    del threadLocal
115    import gc
116
117    gc.collect()  # a little extra insurance
118h1 = soup.find('h1').text
119print(h1)
120
121Next Soccer Matches: Today, 18 Dec 2021
122Next Soccer Matches: Wednesday, 22 Dec 2021
123Next Soccer Matches: Thursday, 23 Dec 2021
124Next Soccer Matches: Friday, 24 Dec 2021
125Next Soccer Matches: Tuesday, 21 Dec 2021
126Next Soccer Matches: Monday, 20 Dec 2021
127Next Soccer Matches: Yesterday, 17 Dec 2021
128Next Soccer Matches: Tomorrow, 19 Dec 2021
129        td_tags = tr_tag.findAll('td')
130        print(td_tags)
131        if len(td_tags) &gt; 0:  # or just if td_tags
132            print(len(td_tags))
133[&lt;td class=&quot;table-time datet t1639839600-1-1-0-0&quot;&gt;15:00&lt;/td&gt;, &lt;td class=&quot;name table-participant&quot;&gt;&lt;a href=&quot;/soccer/england/npl-premier-division/south-shields-witton-albion-COXLb3wr/&quot;&gt;South Shields - Witton&lt;/a&gt;&lt;span class=&quot;ico-event-info&quot; onmouseout=&quot;allowHideTootip(true);delayHideTip(200);&quot; onmouseover=&quot;toolTip('Postponed due to Covid-19.', this, event, '4');allowHideTootip(false);delayHideTip(200);return false;&quot;&gt; &lt;/span&gt;&lt;/td&gt;, &lt;td class=&quot;center bold table-odds table-score&quot;&gt;postp.&lt;/td&gt;, &lt;td class=&quot;odds-nowrp&quot; xodd=&quot;1.36&quot; xoid=&quot;E-4v5i6xv464x0xd4ur7&quot;&gt;&lt;a href=&quot;&quot; onclick=&quot;globals.ch.togle(this , 'E-4v5i6xv464x0xd4ur7');return false;&quot; xparam=&quot;odds_text&quot;&gt;1.36&lt;/a&gt;&lt;/td&gt;, &lt;td class=&quot;odds-nowrp&quot; xodd=&quot;4.84&quot; xoid=&quot;E-4v5i6xv498x0x0&quot;&gt;&lt;a href=&quot;&quot; onclick=&quot;globals.ch.togle(this , 'E-4v5i6xv498x0x0');return false;&quot; xparam=&quot;odds_text&quot;&gt;4.84&lt;/a&gt;&lt;/td&gt;, &lt;td class=&quot;odds-nowrp&quot; xodd=&quot;7.05&quot; xoid=&quot;E-4v5i6xv464x0xd4ur8&quot;&gt;&lt;a href=&quot;&quot; onclick=&quot;globals.ch.togle(this , 'E-4v5i6xv464x0xd4ur8');return false;&quot; xparam=&quot;odds_text&quot;&gt;7.05&lt;/a&gt;&lt;/td&gt;, &lt;td class=&quot;center info-value&quot;&gt;11&lt;/td&gt;]
1347
135[&lt;td class=&quot;table-time datet t1639839600-1-1-0-0&quot;&gt;15:00&lt;/td&gt;, &lt;td class=&quot;name table-participant&quot; colspan=&quot;2&quot;&gt;&lt;a href=&quot;/soccer/england/npl-premier-division/warrington-town-scarborough-athletic-ObyQcNhl/&quot;&gt;Warrington - Scarborough&lt;/a&gt;&lt;/td&gt;, &lt;td class=&quot;odds-nowrp&quot; xodd=&quot;1.8&quot; xoid=&quot;E-4v5i7xv464x0xd4ur9&quot;&gt;&lt;a href=&quot;&quot; onclick=&quot;globals.ch.togle(this , 'E-4v5i7xv464x0xd4ur9');return false;&quot; xparam=&quot;odds_text&quot;&gt;1.80&lt;/a&gt;&lt;/td&gt;, &lt;td class=&quot;odds-nowrp&quot; xodd=&quot;3.59&quot; xoid=&quot;E-4v5i7xv498x0x0&quot;&gt;&lt;a href=&quot;&quot; onclick=&quot;globals.ch.togle(this , 'E-4v5i7xv498x0x0');return false;&quot; xparam=&quot;odds_text&quot;&gt;3.59&lt;/a&gt;&lt;/td&gt;, &lt;td class=&quot;odds-nowrp&quot; xodd=&quot;3.91&quot; xoid=&quot;E-4v5i7xv464x0xd4ura&quot;&gt;&lt;a href=&quot;&quot; onclick=&quot;globals.ch.togle(this , 'E-4v5i7xv464x0xd4ura');return false;&quot; xparam=&quot;odds_text&quot;&gt;3.91&lt;/a&gt;&lt;/td&gt;, &lt;td class=&quot;center info-value&quot;&gt;12&lt;/td&gt;]
1366
137line 67, in generate_matches
138    yield [td_tags[0].text, td_tags[1].text, td_tags[2].text, td_tags[3].text,
139IndexError: list index out of range
140if len(td_tags) &gt; 0:  # or just if td_tags
141    yield [td_tags[0].text, td_tags[1].text, td_tags[2].text, td_tags[3].text,
142           td_tags[4].text, td_tags[5].text, country, league]
143if len(td_tags) &gt; 5:
144

That should get rid of the IndexError.

Source https://stackoverflow.com/questions/70401284

QUESTION

Write an Effective query for django for a FOR Loop?

Asked 2021-Dec-14 at 17:48

I need to get number of policies bought in each month from these model

1class Insurance(models.Model):
2  #other fields..
3  date_purchased_on = models.DateField(auto_now_add=True)    
4  customer_region = models.CharField(
5    max_length=10, choices=RegionType.choices)
6

so i have written a views to get a response

1class Insurance(models.Model):
2  #other fields..
3  date_purchased_on = models.DateField(auto_now_add=True)    
4  customer_region = models.CharField(
5    max_length=10, choices=RegionType.choices)
6@api_view(['GET'])
7def chart_data(request):
8    # structure of the op should be all the months from 1 to 12
9    #  [{&quot;month&quot;: 1,&quot;count&quot;: 100,}, { &quot;month:2&quot;, &quot;count&quot;:20}]
10
11    filtered_data = []
12    for i in range(1, 13):
13        query = Insurance.objects.filter(date_purchased_on__month=i).count()
14        data = {
15            &quot;month&quot;: i,
16            &quot;count&quot;: query
17        }
18        filtered_data.append(data)
19        
20    return Response(filtered_data)
21

now the problem is this query hits the database every time its like 12 times. is there a way to handle this to get in a single query or to reduce it

and my logger results

1class Insurance(models.Model):
2  #other fields..
3  date_purchased_on = models.DateField(auto_now_add=True)    
4  customer_region = models.CharField(
5    max_length=10, choices=RegionType.choices)
6@api_view(['GET'])
7def chart_data(request):
8    # structure of the op should be all the months from 1 to 12
9    #  [{&quot;month&quot;: 1,&quot;count&quot;: 100,}, { &quot;month:2&quot;, &quot;count&quot;:20}]
10
11    filtered_data = []
12    for i in range(1, 13):
13        query = Insurance.objects.filter(date_purchased_on__month=i).count()
14        data = {
15            &quot;month&quot;: i,
16            &quot;count&quot;: query
17        }
18        filtered_data.append(data)
19        
20    return Response(filtered_data)
21(0.000) SELECT COUNT(*) AS &quot;__count&quot; FROM &quot;insurance_insurance&quot; WHERE EXTRACT('month' FROM &quot;insurance_insurance&quot;.&quot;date_purchased_on&quot;) = 1; args=(1,); alias=default
22(0.000) SELECT COUNT(*) AS &quot;__count&quot; FROM &quot;insurance_insurance&quot; WHERE EXTRACT('month' FROM &quot;insurance_insurance&quot;.&quot;date_purchased_on&quot;) = 2; args=(2,); alias=default
23(0.000) SELECT COUNT(*) AS &quot;__count&quot; FROM &quot;insurance_insurance&quot; WHERE EXTRACT('month' FROM &quot;insurance_insurance&quot;.&quot;date_purchased_on&quot;) = 3; args=(3,); alias=default
24(0.000) SELECT COUNT(*) AS &quot;__count&quot; FROM &quot;insurance_insurance&quot; WHERE EXTRACT('month' FROM &quot;insurance_insurance&quot;.&quot;date_purchased_on&quot;) = 4; args=(4,); alias=default
25(0.000) SELECT COUNT(*) AS &quot;__count&quot; FROM &quot;insurance_insurance&quot; WHERE EXTRACT('month' FROM &quot;insurance_insurance&quot;.&quot;date_purchased_on&quot;) = 5; args=(5,); alias=default
26(0.000) SELECT COUNT(*) AS &quot;__count&quot; FROM &quot;insurance_insurance&quot; WHERE EXTRACT('month' FROM &quot;insurance_insurance&quot;.&quot;date_purchased_on&quot;) = 6; args=(6,); alias=default
27(0.000) SELECT COUNT(*) AS &quot;__count&quot; FROM &quot;insurance_insurance&quot; WHERE EXTRACT('month' FROM &quot;insurance_insurance&quot;.&quot;date_purchased_on&quot;) = 7; args=(7,); alias=default
28(0.000) SELECT COUNT(*) AS &quot;__count&quot; FROM &quot;insurance_insurance&quot; WHERE EXTRACT('month' FROM &quot;insurance_insurance&quot;.&quot;date_purchased_on&quot;) = 8; args=(8,); alias=default
29(0.000) SELECT COUNT(*) AS &quot;__count&quot; FROM &quot;insurance_insurance&quot; WHERE EXTRACT('month' FROM &quot;insurance_insurance&quot;.&quot;date_purchased_on&quot;) = 9; args=(9,); alias=default
30(0.000) SELECT COUNT(*) AS &quot;__count&quot; FROM &quot;insurance_insurance&quot; WHERE EXTRACT('month' FROM &quot;insurance_insurance&quot;.&quot;date_purchased_on&quot;) = 10; args=(10,); alias=default
31(0.000) SELECT COUNT(*) AS &quot;__count&quot; FROM &quot;insurance_insurance&quot; WHERE EXTRACT('month' FROM &quot;insurance_insurance&quot;.&quot;date_purchased_on&quot;) = 11; args=(11,); alias=default
32(0.000) SELECT COUNT(*) AS &quot;__count&quot; FROM &quot;insurance_insurance&quot; WHERE EXTRACT('month' FROM &quot;insurance_insurance&quot;.&quot;date_purchased_on&quot;) = 12; args=(12,); alias=default
33
34

ANSWER

Answered 2021-Dec-14 at 17:48

You can extract the month and order by that month with an ExtractMonth expression [Django-doc]:

1class Insurance(models.Model):
2  #other fields..
3  date_purchased_on = models.DateField(auto_now_add=True)    
4  customer_region = models.CharField(
5    max_length=10, choices=RegionType.choices)
6@api_view(['GET'])
7def chart_data(request):
8    # structure of the op should be all the months from 1 to 12
9    #  [{&quot;month&quot;: 1,&quot;count&quot;: 100,}, { &quot;month:2&quot;, &quot;count&quot;:20}]
10
11    filtered_data = []
12    for i in range(1, 13):
13        query = Insurance.objects.filter(date_purchased_on__month=i).count()
14        data = {
15            &quot;month&quot;: i,
16            &quot;count&quot;: query
17        }
18        filtered_data.append(data)
19        
20    return Response(filtered_data)
21(0.000) SELECT COUNT(*) AS &quot;__count&quot; FROM &quot;insurance_insurance&quot; WHERE EXTRACT('month' FROM &quot;insurance_insurance&quot;.&quot;date_purchased_on&quot;) = 1; args=(1,); alias=default
22(0.000) SELECT COUNT(*) AS &quot;__count&quot; FROM &quot;insurance_insurance&quot; WHERE EXTRACT('month' FROM &quot;insurance_insurance&quot;.&quot;date_purchased_on&quot;) = 2; args=(2,); alias=default
23(0.000) SELECT COUNT(*) AS &quot;__count&quot; FROM &quot;insurance_insurance&quot; WHERE EXTRACT('month' FROM &quot;insurance_insurance&quot;.&quot;date_purchased_on&quot;) = 3; args=(3,); alias=default
24(0.000) SELECT COUNT(*) AS &quot;__count&quot; FROM &quot;insurance_insurance&quot; WHERE EXTRACT('month' FROM &quot;insurance_insurance&quot;.&quot;date_purchased_on&quot;) = 4; args=(4,); alias=default
25(0.000) SELECT COUNT(*) AS &quot;__count&quot; FROM &quot;insurance_insurance&quot; WHERE EXTRACT('month' FROM &quot;insurance_insurance&quot;.&quot;date_purchased_on&quot;) = 5; args=(5,); alias=default
26(0.000) SELECT COUNT(*) AS &quot;__count&quot; FROM &quot;insurance_insurance&quot; WHERE EXTRACT('month' FROM &quot;insurance_insurance&quot;.&quot;date_purchased_on&quot;) = 6; args=(6,); alias=default
27(0.000) SELECT COUNT(*) AS &quot;__count&quot; FROM &quot;insurance_insurance&quot; WHERE EXTRACT('month' FROM &quot;insurance_insurance&quot;.&quot;date_purchased_on&quot;) = 7; args=(7,); alias=default
28(0.000) SELECT COUNT(*) AS &quot;__count&quot; FROM &quot;insurance_insurance&quot; WHERE EXTRACT('month' FROM &quot;insurance_insurance&quot;.&quot;date_purchased_on&quot;) = 8; args=(8,); alias=default
29(0.000) SELECT COUNT(*) AS &quot;__count&quot; FROM &quot;insurance_insurance&quot; WHERE EXTRACT('month' FROM &quot;insurance_insurance&quot;.&quot;date_purchased_on&quot;) = 9; args=(9,); alias=default
30(0.000) SELECT COUNT(*) AS &quot;__count&quot; FROM &quot;insurance_insurance&quot; WHERE EXTRACT('month' FROM &quot;insurance_insurance&quot;.&quot;date_purchased_on&quot;) = 10; args=(10,); alias=default
31(0.000) SELECT COUNT(*) AS &quot;__count&quot; FROM &quot;insurance_insurance&quot; WHERE EXTRACT('month' FROM &quot;insurance_insurance&quot;.&quot;date_purchased_on&quot;) = 11; args=(11,); alias=default
32(0.000) SELECT COUNT(*) AS &quot;__count&quot; FROM &quot;insurance_insurance&quot; WHERE EXTRACT('month' FROM &quot;insurance_insurance&quot;.&quot;date_purchased_on&quot;) = 12; args=(12,); alias=default
33
34from django.db.models import <strong>Count</strong>
35from django.db.models.functions import <strong>ExtractMonth</strong>
36
37Insurance.objects.values(month=<strong>ExtractMonth('date_purchased_on')</strong>).annotate(
38    <strong>count=Count('pk')</strong>
39).order_by('month')

This will product a QuerySet of dictionaries that look like:

1class Insurance(models.Model):
2  #other fields..
3  date_purchased_on = models.DateField(auto_now_add=True)    
4  customer_region = models.CharField(
5    max_length=10, choices=RegionType.choices)
6@api_view(['GET'])
7def chart_data(request):
8    # structure of the op should be all the months from 1 to 12
9    #  [{&quot;month&quot;: 1,&quot;count&quot;: 100,}, { &quot;month:2&quot;, &quot;count&quot;:20}]
10
11    filtered_data = []
12    for i in range(1, 13):
13        query = Insurance.objects.filter(date_purchased_on__month=i).count()
14        data = {
15            &quot;month&quot;: i,
16            &quot;count&quot;: query
17        }
18        filtered_data.append(data)
19        
20    return Response(filtered_data)
21(0.000) SELECT COUNT(*) AS &quot;__count&quot; FROM &quot;insurance_insurance&quot; WHERE EXTRACT('month' FROM &quot;insurance_insurance&quot;.&quot;date_purchased_on&quot;) = 1; args=(1,); alias=default
22(0.000) SELECT COUNT(*) AS &quot;__count&quot; FROM &quot;insurance_insurance&quot; WHERE EXTRACT('month' FROM &quot;insurance_insurance&quot;.&quot;date_purchased_on&quot;) = 2; args=(2,); alias=default
23(0.000) SELECT COUNT(*) AS &quot;__count&quot; FROM &quot;insurance_insurance&quot; WHERE EXTRACT('month' FROM &quot;insurance_insurance&quot;.&quot;date_purchased_on&quot;) = 3; args=(3,); alias=default
24(0.000) SELECT COUNT(*) AS &quot;__count&quot; FROM &quot;insurance_insurance&quot; WHERE EXTRACT('month' FROM &quot;insurance_insurance&quot;.&quot;date_purchased_on&quot;) = 4; args=(4,); alias=default
25(0.000) SELECT COUNT(*) AS &quot;__count&quot; FROM &quot;insurance_insurance&quot; WHERE EXTRACT('month' FROM &quot;insurance_insurance&quot;.&quot;date_purchased_on&quot;) = 5; args=(5,); alias=default
26(0.000) SELECT COUNT(*) AS &quot;__count&quot; FROM &quot;insurance_insurance&quot; WHERE EXTRACT('month' FROM &quot;insurance_insurance&quot;.&quot;date_purchased_on&quot;) = 6; args=(6,); alias=default
27(0.000) SELECT COUNT(*) AS &quot;__count&quot; FROM &quot;insurance_insurance&quot; WHERE EXTRACT('month' FROM &quot;insurance_insurance&quot;.&quot;date_purchased_on&quot;) = 7; args=(7,); alias=default
28(0.000) SELECT COUNT(*) AS &quot;__count&quot; FROM &quot;insurance_insurance&quot; WHERE EXTRACT('month' FROM &quot;insurance_insurance&quot;.&quot;date_purchased_on&quot;) = 8; args=(8,); alias=default
29(0.000) SELECT COUNT(*) AS &quot;__count&quot; FROM &quot;insurance_insurance&quot; WHERE EXTRACT('month' FROM &quot;insurance_insurance&quot;.&quot;date_purchased_on&quot;) = 9; args=(9,); alias=default
30(0.000) SELECT COUNT(*) AS &quot;__count&quot; FROM &quot;insurance_insurance&quot; WHERE EXTRACT('month' FROM &quot;insurance_insurance&quot;.&quot;date_purchased_on&quot;) = 10; args=(10,); alias=default
31(0.000) SELECT COUNT(*) AS &quot;__count&quot; FROM &quot;insurance_insurance&quot; WHERE EXTRACT('month' FROM &quot;insurance_insurance&quot;.&quot;date_purchased_on&quot;) = 11; args=(11,); alias=default
32(0.000) SELECT COUNT(*) AS &quot;__count&quot; FROM &quot;insurance_insurance&quot; WHERE EXTRACT('month' FROM &quot;insurance_insurance&quot;.&quot;date_purchased_on&quot;) = 12; args=(12,); alias=default
33
34from django.db.models import <strong>Count</strong>
35from django.db.models.functions import <strong>ExtractMonth</strong>
36
37Insurance.objects.values(month=<strong>ExtractMonth('date_purchased_on')</strong>).annotate(
38    <strong>count=Count('pk')</strong>
39).order_by('month')&lt;QuerySet [
40    {'month': 6, 'count': 100 },
41    {'month': 7, 'count': 20 }
42]&gt;
43

Months for which there is no Insurance object will not be included in the QuerySet. You thus might have to post-process it and include records with 'count': 0 for these.

Source https://stackoverflow.com/questions/70353341

QUESTION

Combining values in a tuple that contain both string and integers values and storing as a dataframe

Asked 2021-Nov-08 at 12:41

I'm working on building a function that takes in a list of company descriptions and then outputs the most common 3-word phrases found in the list. I've been able to get it to the point where it outputs a dictionary of tuples constructed like this:

1{('technology', 'company', 'provides'): 2,
2 ('various', 'industries.', 'company'): 2,
3 ('provides', 'software', 'solutions'): 2,
4 ('life', 'health', 'insurance'): 2,...}
5

I'd like to convert this to a table/dataframe that concatenates the strings into a single value and then creates a separate column that would store the number of instances of the phrase.

The ideal output would be:

Phrase Occurrence
technology company provides 2
various industries company 2
provides software solutions 2
life health insurance 2

I've tried using the following which combines the tuple into a string but it drops the number of occurrences:

1{('technology', 'company', 'provides'): 2,
2 ('various', 'industries.', 'company'): 2,
3 ('provides', 'software', 'solutions'): 2,
4 ('life', 'health', 'insurance'): 2,...}
5# function that converts tuple to string
6def join_tuple_string(descriptions) -&gt; str:
7   return ' '.join(descriptions)
8
9# joining all the tuples
10result = map(join_tuple_string, descriptions)
11
12# converting and printing the result
13print(list(result))
14

Here is the output:

1{('technology', 'company', 'provides'): 2,
2 ('various', 'industries.', 'company'): 2,
3 ('provides', 'software', 'solutions'): 2,
4 ('life', 'health', 'insurance'): 2,...}
5# function that converts tuple to string
6def join_tuple_string(descriptions) -&gt; str:
7   return ' '.join(descriptions)
8
9# joining all the tuples
10result = map(join_tuple_string, descriptions)
11
12# converting and printing the result
13print(list(result))
14['technology company provides', 
15'provides software solutions', 
16'product suite includes', 'life health insurance',...]
17

How can I concatenate these values without losing the number of occurrences? I'd like to be able to export this to a CSV to review the full list.

ANSWER

Answered 2021-Nov-06 at 17:37

given:

1{('technology', 'company', 'provides'): 2,
2 ('various', 'industries.', 'company'): 2,
3 ('provides', 'software', 'solutions'): 2,
4 ('life', 'health', 'insurance'): 2,...}
5# function that converts tuple to string
6def join_tuple_string(descriptions) -&gt; str:
7   return ' '.join(descriptions)
8
9# joining all the tuples
10result = map(join_tuple_string, descriptions)
11
12# converting and printing the result
13print(list(result))
14['technology company provides', 
15'provides software solutions', 
16'product suite includes', 'life health insurance',...]
17din = {('technology', 'company', 'provides'): 2,
18 ('various', 'industries.', 'company'): 2,
19 ('provides', 'software', 'solutions'): 2,
20 ('life', 'health', 'insurance'): 2}  
21

In would proceed as follows:

1{('technology', 'company', 'provides'): 2,
2 ('various', 'industries.', 'company'): 2,
3 ('provides', 'software', 'solutions'): 2,
4 ('life', 'health', 'insurance'): 2,...}
5# function that converts tuple to string
6def join_tuple_string(descriptions) -&gt; str:
7   return ' '.join(descriptions)
8
9# joining all the tuples
10result = map(join_tuple_string, descriptions)
11
12# converting and printing the result
13print(list(result))
14['technology company provides', 
15'provides software solutions', 
16'product suite includes', 'life health insurance',...]
17din = {('technology', 'company', 'provides'): 2,
18 ('various', 'industries.', 'company'): 2,
19 ('provides', 'software', 'solutions'): 2,
20 ('life', 'health', 'insurance'): 2}  
21def reportValues(d):
22    result = []
23    for ky, v in d.items():
24        result.append([' '.join(ky), v])
25    return result 
26
27result = reportValues(din)
28for r in result:
29    print(f'{r[0]:25}\t{r[1]}')   
30

which produces:

1{('technology', 'company', 'provides'): 2,
2 ('various', 'industries.', 'company'): 2,
3 ('provides', 'software', 'solutions'): 2,
4 ('life', 'health', 'insurance'): 2,...}
5# function that converts tuple to string
6def join_tuple_string(descriptions) -&gt; str:
7   return ' '.join(descriptions)
8
9# joining all the tuples
10result = map(join_tuple_string, descriptions)
11
12# converting and printing the result
13print(list(result))
14['technology company provides', 
15'provides software solutions', 
16'product suite includes', 'life health insurance',...]
17din = {('technology', 'company', 'provides'): 2,
18 ('various', 'industries.', 'company'): 2,
19 ('provides', 'software', 'solutions'): 2,
20 ('life', 'health', 'insurance'): 2}  
21def reportValues(d):
22    result = []
23    for ky, v in d.items():
24        result.append([' '.join(ky), v])
25    return result 
26
27result = reportValues(din)
28for r in result:
29    print(f'{r[0]:25}\t{r[1]}')   
30technology company provides 2
31various industries. company 2
32provides software solutions 2
33life health insurance       2
34

Source https://stackoverflow.com/questions/69866261

QUESTION

Error when using case_when with mutate and functions: getting closest number to zero with NA

Asked 2021-Nov-05 at 15:55

I am trying to flag the closest number of days from an accident to when someone got insurance. 0 represents the same day, a negative number represents insurance prior to accident and a positive number represents insurance post accident.

Data

1df &lt;- data.frame(id=1:4, accident1=c(-1,3,4, NA), accident2=c(100, -2, NA, NA), accident3=c(-3,1,2, NA))
2    
3&gt; df
4  id accident1 accident2 accident3
51  1        -1       100        -3
62  2         3        -2         1
73  3         4        NA         2
84  4        NA        NA        NA
9

Code:

1df &lt;- data.frame(id=1:4, accident1=c(-1,3,4, NA), accident2=c(100, -2, NA, NA), accident3=c(-3,1,2, NA))
2    
3&gt; df
4  id accident1 accident2 accident3
51  1        -1       100        -3
62  2         3        -2         1
73  3         4        NA         2
84  4        NA        NA        NA
9library(DescTools)
10library(dplyr)
11
12df %&gt;%
13  rowwise() %&gt;%
14  mutate(magic=
15           case_when(
16             accident1 &lt;0 |accident2&lt;0 |accident3&lt;0 ~ as.numeric(pmax(accident1, accident2, accident3, na.rm=T)),
17             accident1 &gt;0 |accident2&gt;0 | accident3&gt;0 ~ as.numeric(pmin(accident1, accident2, accident3, na.rm=T)),
18             accident1 &gt; 0 &amp; accident2 &gt; 0 &amp; accident3&gt;0 ~ as.numeric(pmin(accident1, accident2, accident3, na.rm=T)),
19             accident1 &lt; 0 &amp; accident2 &lt; 0 &amp; accident3 &lt; 0 ~ as.numeric(pmax(accident1, accident2, accident3, na.rm=T)),
20             TRUE ~ NA_real_)) %&gt;%
21         rowwise() %&gt;% 
22  # not working
23  mutate(magic= case_when(
24             (accident1 &gt;0 |accident2&lt;0 |accident3&lt;0) &amp; (accident1 &gt;0 |accident2&gt;0 | accident3&gt;0) ~ 
25               Closest(as.numeric(unlist(c(accident1, accident2, accident3))), 0, na.rm=T), TRUE~magic))
26

Result without last row in data (all NAs):

1df &lt;- data.frame(id=1:4, accident1=c(-1,3,4, NA), accident2=c(100, -2, NA, NA), accident3=c(-3,1,2, NA))
2    
3&gt; df
4  id accident1 accident2 accident3
51  1        -1       100        -3
62  2         3        -2         1
73  3         4        NA         2
84  4        NA        NA        NA
9library(DescTools)
10library(dplyr)
11
12df %&gt;%
13  rowwise() %&gt;%
14  mutate(magic=
15           case_when(
16             accident1 &lt;0 |accident2&lt;0 |accident3&lt;0 ~ as.numeric(pmax(accident1, accident2, accident3, na.rm=T)),
17             accident1 &gt;0 |accident2&gt;0 | accident3&gt;0 ~ as.numeric(pmin(accident1, accident2, accident3, na.rm=T)),
18             accident1 &gt; 0 &amp; accident2 &gt; 0 &amp; accident3&gt;0 ~ as.numeric(pmin(accident1, accident2, accident3, na.rm=T)),
19             accident1 &lt; 0 &amp; accident2 &lt; 0 &amp; accident3 &lt; 0 ~ as.numeric(pmax(accident1, accident2, accident3, na.rm=T)),
20             TRUE ~ NA_real_)) %&gt;%
21         rowwise() %&gt;% 
22  # not working
23  mutate(magic= case_when(
24             (accident1 &gt;0 |accident2&lt;0 |accident3&lt;0) &amp; (accident1 &gt;0 |accident2&gt;0 | accident3&gt;0) ~ 
25               Closest(as.numeric(unlist(c(accident1, accident2, accident3))), 0, na.rm=T), TRUE~magic))
26# A tibble: 3 × 5
27# Rowwise: 
28     id accident1 accident2 accident3 magic
29  &lt;int&gt;     &lt;dbl&gt;     &lt;dbl&gt;     &lt;dbl&gt; &lt;dbl&gt;
301     1        -1       100        -3    -1
312     2         3        -2         1     1
323     3         4        NA         2     2
33

However, when I try this with NA in last row:

1df &lt;- data.frame(id=1:4, accident1=c(-1,3,4, NA), accident2=c(100, -2, NA, NA), accident3=c(-3,1,2, NA))
2    
3&gt; df
4  id accident1 accident2 accident3
51  1        -1       100        -3
62  2         3        -2         1
73  3         4        NA         2
84  4        NA        NA        NA
9library(DescTools)
10library(dplyr)
11
12df %&gt;%
13  rowwise() %&gt;%
14  mutate(magic=
15           case_when(
16             accident1 &lt;0 |accident2&lt;0 |accident3&lt;0 ~ as.numeric(pmax(accident1, accident2, accident3, na.rm=T)),
17             accident1 &gt;0 |accident2&gt;0 | accident3&gt;0 ~ as.numeric(pmin(accident1, accident2, accident3, na.rm=T)),
18             accident1 &gt; 0 &amp; accident2 &gt; 0 &amp; accident3&gt;0 ~ as.numeric(pmin(accident1, accident2, accident3, na.rm=T)),
19             accident1 &lt; 0 &amp; accident2 &lt; 0 &amp; accident3 &lt; 0 ~ as.numeric(pmax(accident1, accident2, accident3, na.rm=T)),
20             TRUE ~ NA_real_)) %&gt;%
21         rowwise() %&gt;% 
22  # not working
23  mutate(magic= case_when(
24             (accident1 &gt;0 |accident2&lt;0 |accident3&lt;0) &amp; (accident1 &gt;0 |accident2&gt;0 | accident3&gt;0) ~ 
25               Closest(as.numeric(unlist(c(accident1, accident2, accident3))), 0, na.rm=T), TRUE~magic))
26# A tibble: 3 × 5
27# Rowwise: 
28     id accident1 accident2 accident3 magic
29  &lt;int&gt;     &lt;dbl&gt;     &lt;dbl&gt;     &lt;dbl&gt; &lt;dbl&gt;
301     1        -1       100        -3    -1
312     2         3        -2         1     1
323     3         4        NA         2     2
33Error: Problem with `mutate()` column `magic`.
34ℹ `magic = case_when(...)`.
35ℹ `magic` must be size 1, not 0.
36ℹ Did you mean: `magic = list(case_when(...))` ?
37ℹ The error occurred in row 4.
38Run `rlang::last_error()` to see where the error occurred.
39In addition: Warning message:
40Problem with `mutate()` column `magic`.
41ℹ `magic = case_when(...)`.
42ℹ no non-missing arguments to min; returning Inf
43ℹ The warning occurred in row 4. 
44

Any advice on how to get the code to work on row 4 with NAs?

ANSWER

Answered 2021-Nov-05 at 15:27

The main reason is the last element which returns numeric(0) because all the elements are NA and we used na.rm = TRUE for Closest. We can prevent that by indexing to return first element and this changes to NA

1df &lt;- data.frame(id=1:4, accident1=c(-1,3,4, NA), accident2=c(100, -2, NA, NA), accident3=c(-3,1,2, NA))
2    
3&gt; df
4  id accident1 accident2 accident3
51  1        -1       100        -3
62  2         3        -2         1
73  3         4        NA         2
84  4        NA        NA        NA
9library(DescTools)
10library(dplyr)
11
12df %&gt;%
13  rowwise() %&gt;%
14  mutate(magic=
15           case_when(
16             accident1 &lt;0 |accident2&lt;0 |accident3&lt;0 ~ as.numeric(pmax(accident1, accident2, accident3, na.rm=T)),
17             accident1 &gt;0 |accident2&gt;0 | accident3&gt;0 ~ as.numeric(pmin(accident1, accident2, accident3, na.rm=T)),
18             accident1 &gt; 0 &amp; accident2 &gt; 0 &amp; accident3&gt;0 ~ as.numeric(pmin(accident1, accident2, accident3, na.rm=T)),
19             accident1 &lt; 0 &amp; accident2 &lt; 0 &amp; accident3 &lt; 0 ~ as.numeric(pmax(accident1, accident2, accident3, na.rm=T)),
20             TRUE ~ NA_real_)) %&gt;%
21         rowwise() %&gt;% 
22  # not working
23  mutate(magic= case_when(
24             (accident1 &gt;0 |accident2&lt;0 |accident3&lt;0) &amp; (accident1 &gt;0 |accident2&gt;0 | accident3&gt;0) ~ 
25               Closest(as.numeric(unlist(c(accident1, accident2, accident3))), 0, na.rm=T), TRUE~magic))
26# A tibble: 3 × 5
27# Rowwise: 
28     id accident1 accident2 accident3 magic
29  &lt;int&gt;     &lt;dbl&gt;     &lt;dbl&gt;     &lt;dbl&gt; &lt;dbl&gt;
301     1        -1       100        -3    -1
312     2         3        -2         1     1
323     3         4        NA         2     2
33Error: Problem with `mutate()` column `magic`.
34ℹ `magic = case_when(...)`.
35ℹ `magic` must be size 1, not 0.
36ℹ Did you mean: `magic = list(case_when(...))` ?
37ℹ The error occurred in row 4.
38Run `rlang::last_error()` to see where the error occurred.
39In addition: Warning message:
40Problem with `mutate()` column `magic`.
41ℹ `magic = case_when(...)`.
42ℹ no non-missing arguments to min; returning Inf
43ℹ The warning occurred in row 4. 
44library(dplyr)
45df %&gt;% 
46  mutate(magic=
47           case_when(
48             accident1 &lt;0 |accident2&lt;0 |accident3&lt;0 ~ as.numeric(pmax(accident1, accident2, accident3, na.rm=T)),
49             accident1 &gt;0 |accident2&gt;0 | accident3&gt;0 ~ as.numeric(pmin(accident1, accident2, accident3, na.rm=T)),
50             accident1 &gt; 0 &amp; accident2 &gt; 0 &amp; accident3&gt;0 ~ as.numeric(pmin(accident1, accident2, accident3, na.rm=T)),
51             accident1 &lt; 0 &amp; accident2 &lt; 0 &amp; accident3 &lt; 0 ~ as.numeric(pmax(accident1, accident2, accident3, na.rm=T)),
52             TRUE ~ NA_real_)) %&gt;%
53         rowwise() %&gt;%  
54  mutate(magic= case_when(
55             (accident1 &gt;0 |accident2&lt;0 |accident3&lt;0) &amp; (accident1 &gt;0 |accident2&gt;0 | accident3&gt;0) ~ 
56               Closest(as.numeric(unlist(c(accident1, accident2, accident3))), 0, na.rm=TRUE)[1], TRUE~magic))
57

-output

1df &lt;- data.frame(id=1:4, accident1=c(-1,3,4, NA), accident2=c(100, -2, NA, NA), accident3=c(-3,1,2, NA))
2    
3&gt; df
4  id accident1 accident2 accident3
51  1        -1       100        -3
62  2         3        -2         1
73  3         4        NA         2
84  4        NA        NA        NA
9library(DescTools)
10library(dplyr)
11
12df %&gt;%
13  rowwise() %&gt;%
14  mutate(magic=
15           case_when(
16             accident1 &lt;0 |accident2&lt;0 |accident3&lt;0 ~ as.numeric(pmax(accident1, accident2, accident3, na.rm=T)),
17             accident1 &gt;0 |accident2&gt;0 | accident3&gt;0 ~ as.numeric(pmin(accident1, accident2, accident3, na.rm=T)),
18             accident1 &gt; 0 &amp; accident2 &gt; 0 &amp; accident3&gt;0 ~ as.numeric(pmin(accident1, accident2, accident3, na.rm=T)),
19             accident1 &lt; 0 &amp; accident2 &lt; 0 &amp; accident3 &lt; 0 ~ as.numeric(pmax(accident1, accident2, accident3, na.rm=T)),
20             TRUE ~ NA_real_)) %&gt;%
21         rowwise() %&gt;% 
22  # not working
23  mutate(magic= case_when(
24             (accident1 &gt;0 |accident2&lt;0 |accident3&lt;0) &amp; (accident1 &gt;0 |accident2&gt;0 | accident3&gt;0) ~ 
25               Closest(as.numeric(unlist(c(accident1, accident2, accident3))), 0, na.rm=T), TRUE~magic))
26# A tibble: 3 × 5
27# Rowwise: 
28     id accident1 accident2 accident3 magic
29  &lt;int&gt;     &lt;dbl&gt;     &lt;dbl&gt;     &lt;dbl&gt; &lt;dbl&gt;
301     1        -1       100        -3    -1
312     2         3        -2         1     1
323     3         4        NA         2     2
33Error: Problem with `mutate()` column `magic`.
34ℹ `magic = case_when(...)`.
35ℹ `magic` must be size 1, not 0.
36ℹ Did you mean: `magic = list(case_when(...))` ?
37ℹ The error occurred in row 4.
38Run `rlang::last_error()` to see where the error occurred.
39In addition: Warning message:
40Problem with `mutate()` column `magic`.
41ℹ `magic = case_when(...)`.
42ℹ no non-missing arguments to min; returning Inf
43ℹ The warning occurred in row 4. 
44library(dplyr)
45df %&gt;% 
46  mutate(magic=
47           case_when(
48             accident1 &lt;0 |accident2&lt;0 |accident3&lt;0 ~ as.numeric(pmax(accident1, accident2, accident3, na.rm=T)),
49             accident1 &gt;0 |accident2&gt;0 | accident3&gt;0 ~ as.numeric(pmin(accident1, accident2, accident3, na.rm=T)),
50             accident1 &gt; 0 &amp; accident2 &gt; 0 &amp; accident3&gt;0 ~ as.numeric(pmin(accident1, accident2, accident3, na.rm=T)),
51             accident1 &lt; 0 &amp; accident2 &lt; 0 &amp; accident3 &lt; 0 ~ as.numeric(pmax(accident1, accident2, accident3, na.rm=T)),
52             TRUE ~ NA_real_)) %&gt;%
53         rowwise() %&gt;%  
54  mutate(magic= case_when(
55             (accident1 &gt;0 |accident2&lt;0 |accident3&lt;0) &amp; (accident1 &gt;0 |accident2&gt;0 | accident3&gt;0) ~ 
56               Closest(as.numeric(unlist(c(accident1, accident2, accident3))), 0, na.rm=TRUE)[1], TRUE~magic))
57# A tibble: 4 × 5
58# Rowwise: 
59     id accident1 accident2 accident3 magic
60  &lt;int&gt;     &lt;dbl&gt;     &lt;dbl&gt;     &lt;dbl&gt; &lt;dbl&gt;
611     1        -1       100        -3    -1
622     2         3        -2         1     1
633     3         4        NA         2     2
644     4        NA        NA        NA    NA
65

It is more easier to understand if we do this only on Closest

1df &lt;- data.frame(id=1:4, accident1=c(-1,3,4, NA), accident2=c(100, -2, NA, NA), accident3=c(-3,1,2, NA))
2    
3&gt; df
4  id accident1 accident2 accident3
51  1        -1       100        -3
62  2         3        -2         1
73  3         4        NA         2
84  4        NA        NA        NA
9library(DescTools)
10library(dplyr)
11
12df %&gt;%
13  rowwise() %&gt;%
14  mutate(magic=
15           case_when(
16             accident1 &lt;0 |accident2&lt;0 |accident3&lt;0 ~ as.numeric(pmax(accident1, accident2, accident3, na.rm=T)),
17             accident1 &gt;0 |accident2&gt;0 | accident3&gt;0 ~ as.numeric(pmin(accident1, accident2, accident3, na.rm=T)),
18             accident1 &gt; 0 &amp; accident2 &gt; 0 &amp; accident3&gt;0 ~ as.numeric(pmin(accident1, accident2, accident3, na.rm=T)),
19             accident1 &lt; 0 &amp; accident2 &lt; 0 &amp; accident3 &lt; 0 ~ as.numeric(pmax(accident1, accident2, accident3, na.rm=T)),
20             TRUE ~ NA_real_)) %&gt;%
21         rowwise() %&gt;% 
22  # not working
23  mutate(magic= case_when(
24             (accident1 &gt;0 |accident2&lt;0 |accident3&lt;0) &amp; (accident1 &gt;0 |accident2&gt;0 | accident3&gt;0) ~ 
25               Closest(as.numeric(unlist(c(accident1, accident2, accident3))), 0, na.rm=T), TRUE~magic))
26# A tibble: 3 × 5
27# Rowwise: 
28     id accident1 accident2 accident3 magic
29  &lt;int&gt;     &lt;dbl&gt;     &lt;dbl&gt;     &lt;dbl&gt; &lt;dbl&gt;
301     1        -1       100        -3    -1
312     2         3        -2         1     1
323     3         4        NA         2     2
33Error: Problem with `mutate()` column `magic`.
34ℹ `magic = case_when(...)`.
35ℹ `magic` must be size 1, not 0.
36ℹ Did you mean: `magic = list(case_when(...))` ?
37ℹ The error occurred in row 4.
38Run `rlang::last_error()` to see where the error occurred.
39In addition: Warning message:
40Problem with `mutate()` column `magic`.
41ℹ `magic = case_when(...)`.
42ℹ no non-missing arguments to min; returning Inf
43ℹ The warning occurred in row 4. 
44library(dplyr)
45df %&gt;% 
46  mutate(magic=
47           case_when(
48             accident1 &lt;0 |accident2&lt;0 |accident3&lt;0 ~ as.numeric(pmax(accident1, accident2, accident3, na.rm=T)),
49             accident1 &gt;0 |accident2&gt;0 | accident3&gt;0 ~ as.numeric(pmin(accident1, accident2, accident3, na.rm=T)),
50             accident1 &gt; 0 &amp; accident2 &gt; 0 &amp; accident3&gt;0 ~ as.numeric(pmin(accident1, accident2, accident3, na.rm=T)),
51             accident1 &lt; 0 &amp; accident2 &lt; 0 &amp; accident3 &lt; 0 ~ as.numeric(pmax(accident1, accident2, accident3, na.rm=T)),
52             TRUE ~ NA_real_)) %&gt;%
53         rowwise() %&gt;%  
54  mutate(magic= case_when(
55             (accident1 &gt;0 |accident2&lt;0 |accident3&lt;0) &amp; (accident1 &gt;0 |accident2&gt;0 | accident3&gt;0) ~ 
56               Closest(as.numeric(unlist(c(accident1, accident2, accident3))), 0, na.rm=TRUE)[1], TRUE~magic))
57# A tibble: 4 × 5
58# Rowwise: 
59     id accident1 accident2 accident3 magic
60  &lt;int&gt;     &lt;dbl&gt;     &lt;dbl&gt;     &lt;dbl&gt; &lt;dbl&gt;
611     1        -1       100        -3    -1
622     2         3        -2         1     1
633     3         4        NA         2     2
644     4        NA        NA        NA    NA
65&gt; apply(df[-1], 1, function(x) Closest(x, 0, na.rm = TRUE))
66[[1]]
67accident1 
68       -1 
69
70[[2]]
71accident3 
72        1 
73
74[[3]]
75accident3 
76        2 
77
78[[4]]
79named numeric(0)  ####
80

The solution does index, so that numeric(0) becomes NA

1df &lt;- data.frame(id=1:4, accident1=c(-1,3,4, NA), accident2=c(100, -2, NA, NA), accident3=c(-3,1,2, NA))
2    
3&gt; df
4  id accident1 accident2 accident3
51  1        -1       100        -3
62  2         3        -2         1
73  3         4        NA         2
84  4        NA        NA        NA
9library(DescTools)
10library(dplyr)
11
12df %&gt;%
13  rowwise() %&gt;%
14  mutate(magic=
15           case_when(
16             accident1 &lt;0 |accident2&lt;0 |accident3&lt;0 ~ as.numeric(pmax(accident1, accident2, accident3, na.rm=T)),
17             accident1 &gt;0 |accident2&gt;0 | accident3&gt;0 ~ as.numeric(pmin(accident1, accident2, accident3, na.rm=T)),
18             accident1 &gt; 0 &amp; accident2 &gt; 0 &amp; accident3&gt;0 ~ as.numeric(pmin(accident1, accident2, accident3, na.rm=T)),
19             accident1 &lt; 0 &amp; accident2 &lt; 0 &amp; accident3 &lt; 0 ~ as.numeric(pmax(accident1, accident2, accident3, na.rm=T)),
20             TRUE ~ NA_real_)) %&gt;%
21         rowwise() %&gt;% 
22  # not working
23  mutate(magic= case_when(
24             (accident1 &gt;0 |accident2&lt;0 |accident3&lt;0) &amp; (accident1 &gt;0 |accident2&gt;0 | accident3&gt;0) ~ 
25               Closest(as.numeric(unlist(c(accident1, accident2, accident3))), 0, na.rm=T), TRUE~magic))
26# A tibble: 3 × 5
27# Rowwise: 
28     id accident1 accident2 accident3 magic
29  &lt;int&gt;     &lt;dbl&gt;     &lt;dbl&gt;     &lt;dbl&gt; &lt;dbl&gt;
301     1        -1       100        -3    -1
312     2         3        -2         1     1
323     3         4        NA         2     2
33Error: Problem with `mutate()` column `magic`.
34ℹ `magic = case_when(...)`.
35ℹ `magic` must be size 1, not 0.
36ℹ Did you mean: `magic = list(case_when(...))` ?
37ℹ The error occurred in row 4.
38Run `rlang::last_error()` to see where the error occurred.
39In addition: Warning message:
40Problem with `mutate()` column `magic`.
41ℹ `magic = case_when(...)`.
42ℹ no non-missing arguments to min; returning Inf
43ℹ The warning occurred in row 4. 
44library(dplyr)
45df %&gt;% 
46  mutate(magic=
47           case_when(
48             accident1 &lt;0 |accident2&lt;0 |accident3&lt;0 ~ as.numeric(pmax(accident1, accident2, accident3, na.rm=T)),
49             accident1 &gt;0 |accident2&gt;0 | accident3&gt;0 ~ as.numeric(pmin(accident1, accident2, accident3, na.rm=T)),
50             accident1 &gt; 0 &amp; accident2 &gt; 0 &amp; accident3&gt;0 ~ as.numeric(pmin(accident1, accident2, accident3, na.rm=T)),
51             accident1 &lt; 0 &amp; accident2 &lt; 0 &amp; accident3 &lt; 0 ~ as.numeric(pmax(accident1, accident2, accident3, na.rm=T)),
52             TRUE ~ NA_real_)) %&gt;%
53         rowwise() %&gt;%  
54  mutate(magic= case_when(
55             (accident1 &gt;0 |accident2&lt;0 |accident3&lt;0) &amp; (accident1 &gt;0 |accident2&gt;0 | accident3&gt;0) ~ 
56               Closest(as.numeric(unlist(c(accident1, accident2, accident3))), 0, na.rm=TRUE)[1], TRUE~magic))
57# A tibble: 4 × 5
58# Rowwise: 
59     id accident1 accident2 accident3 magic
60  &lt;int&gt;     &lt;dbl&gt;     &lt;dbl&gt;     &lt;dbl&gt; &lt;dbl&gt;
611     1        -1       100        -3    -1
622     2         3        -2         1     1
633     3         4        NA         2     2
644     4        NA        NA        NA    NA
65&gt; apply(df[-1], 1, function(x) Closest(x, 0, na.rm = TRUE))
66[[1]]
67accident1 
68       -1 
69
70[[2]]
71accident3 
72        1 
73
74[[3]]
75accident3 
76        2 
77
78[[4]]
79named numeric(0)  ####
80numeric(0)[1]
81[1] NA
82

Source https://stackoverflow.com/questions/69855458

QUESTION

How to remove parent json element in python3 if child is object is empty

Asked 2021-Oct-27 at 15:51

I'm trying to move data from SQL to Mongo. Here is a challenge I'm facing, if any child object is empty I want to remove parent element. I want till insurance field to be removed.

Here is what I tried:

1def remove_empty_elements(jsonData):
2    if(isinstance(jsonData, list) or isinstance(jsonData,dict)):
3
4        for elem in list(jsonData):
5            if not isinstance(elem, dict) and isinstance(jsonData[elem], list) and elem:
6                jsonData[elem] = [x for x in jsonData[elem] if x]
7                if(len(jsonData[elem])==0):
8                    del jsonData[elem]
9            elif not isinstance(elem, dict) and isinstance(jsonData[elem], dict) and not jsonData[elem]:
10                del jsonData[elem]
11    else:
12        pass
13    return jsonData
14
15
16

sample data

1def remove_empty_elements(jsonData):
2    if(isinstance(jsonData, list) or isinstance(jsonData,dict)):
3
4        for elem in list(jsonData):
5            if not isinstance(elem, dict) and isinstance(jsonData[elem], list) and elem:
6                jsonData[elem] = [x for x in jsonData[elem] if x]
7                if(len(jsonData[elem])==0):
8                    del jsonData[elem]
9            elif not isinstance(elem, dict) and isinstance(jsonData[elem], dict) and not jsonData[elem]:
10                del jsonData[elem]
11    else:
12        pass
13    return jsonData
14
15
16{
17  &quot;_id&quot;: &quot;30546c62-8ea0-4f1a-a239-cc7508041a7b&quot;,
18  &quot;IsActive&quot;: &quot;True&quot;,
19  &quot;name&quot;: &quot;Pixel 3&quot;,
20  &quot;phone&quot;: [
21    {
22      &quot;Bill&quot;: 145,
23      &quot;phonetype&quot;: &quot;xyz&quot;,
24      &quot;insurance&quot;: [
25        {
26          &quot;year_one_claims&quot;: [
27            {
28             &quot;2020&quot;: 200 
29            },
30            {
31              
32            },
33            {
34              
35            },
36            {
37              
38            },
39            {
40              
41            }
42          ]
43        },
44        {
45          &quot;year_two_claims&quot;: [
46            {
47              
48            },
49            {
50              
51            },
52            {
53              
54            },
55            {
56              
57            },
58            {
59              
60            }
61          ]
62        },
63        
64      ]
65    }
66  ],
67  &quot;Provider&quot;: {
68    &quot;agent&quot;: &quot;aaadd&quot;,
69    
70  }
71}
72

Results should look like that

1def remove_empty_elements(jsonData):
2    if(isinstance(jsonData, list) or isinstance(jsonData,dict)):
3
4        for elem in list(jsonData):
5            if not isinstance(elem, dict) and isinstance(jsonData[elem], list) and elem:
6                jsonData[elem] = [x for x in jsonData[elem] if x]
7                if(len(jsonData[elem])==0):
8                    del jsonData[elem]
9            elif not isinstance(elem, dict) and isinstance(jsonData[elem], dict) and not jsonData[elem]:
10                del jsonData[elem]
11    else:
12        pass
13    return jsonData
14
15
16{
17  &quot;_id&quot;: &quot;30546c62-8ea0-4f1a-a239-cc7508041a7b&quot;,
18  &quot;IsActive&quot;: &quot;True&quot;,
19  &quot;name&quot;: &quot;Pixel 3&quot;,
20  &quot;phone&quot;: [
21    {
22      &quot;Bill&quot;: 145,
23      &quot;phonetype&quot;: &quot;xyz&quot;,
24      &quot;insurance&quot;: [
25        {
26          &quot;year_one_claims&quot;: [
27            {
28             &quot;2020&quot;: 200 
29            },
30            {
31              
32            },
33            {
34              
35            },
36            {
37              
38            },
39            {
40              
41            }
42          ]
43        },
44        {
45          &quot;year_two_claims&quot;: [
46            {
47              
48            },
49            {
50              
51            },
52            {
53              
54            },
55            {
56              
57            },
58            {
59              
60            }
61          ]
62        },
63        
64      ]
65    }
66  ],
67  &quot;Provider&quot;: {
68    &quot;agent&quot;: &quot;aaadd&quot;,
69    
70  }
71}
72
73{
74  &quot;_id&quot;: &quot;30546c62-8ea0-4f1a-a239-cc7508041a7b&quot;,
75  &quot;IsActive&quot;: &quot;True&quot;,
76  &quot;name&quot;: &quot;Pixel 3&quot;,
77  &quot;phone&quot;: [
78    {
79      &quot;Bill&quot;: 145,
80      &quot;phonetype&quot;: &quot;xyz&quot;,
81      &quot;insurance&quot;: [
82        {
83          &quot;year_one_claims&quot;: [
84            {
85             &quot;2020&quot;: 200 
86            },
87           
88          ]
89        },
90       
91        
92      ]
93    }
94  ],
95  &quot;Provider&quot;: {
96    &quot;agent&quot;: &quot;aaadd&quot;,
97    
98  }
99}
100

ANSWER

Answered 2021-Oct-07 at 08:13

Try out this:

1def remove_empty_elements(jsonData):
2    if(isinstance(jsonData, list) or isinstance(jsonData,dict)):
3
4        for elem in list(jsonData):
5            if not isinstance(elem, dict) and isinstance(jsonData[elem], list) and elem:
6                jsonData[elem] = [x for x in jsonData[elem] if x]
7                if(len(jsonData[elem])==0):
8                    del jsonData[elem]
9            elif not isinstance(elem, dict) and isinstance(jsonData[elem], dict) and not jsonData[elem]:
10                del jsonData[elem]
11    else:
12        pass
13    return jsonData
14
15
16{
17  &quot;_id&quot;: &quot;30546c62-8ea0-4f1a-a239-cc7508041a7b&quot;,
18  &quot;IsActive&quot;: &quot;True&quot;,
19  &quot;name&quot;: &quot;Pixel 3&quot;,
20  &quot;phone&quot;: [
21    {
22      &quot;Bill&quot;: 145,
23      &quot;phonetype&quot;: &quot;xyz&quot;,
24      &quot;insurance&quot;: [
25        {
26          &quot;year_one_claims&quot;: [
27            {
28             &quot;2020&quot;: 200 
29            },
30            {
31              
32            },
33            {
34              
35            },
36            {
37              
38            },
39            {
40              
41            }
42          ]
43        },
44        {
45          &quot;year_two_claims&quot;: [
46            {
47              
48            },
49            {
50              
51            },
52            {
53              
54            },
55            {
56              
57            },
58            {
59              
60            }
61          ]
62        },
63        
64      ]
65    }
66  ],
67  &quot;Provider&quot;: {
68    &quot;agent&quot;: &quot;aaadd&quot;,
69    
70  }
71}
72
73{
74  &quot;_id&quot;: &quot;30546c62-8ea0-4f1a-a239-cc7508041a7b&quot;,
75  &quot;IsActive&quot;: &quot;True&quot;,
76  &quot;name&quot;: &quot;Pixel 3&quot;,
77  &quot;phone&quot;: [
78    {
79      &quot;Bill&quot;: 145,
80      &quot;phonetype&quot;: &quot;xyz&quot;,
81      &quot;insurance&quot;: [
82        {
83          &quot;year_one_claims&quot;: [
84            {
85             &quot;2020&quot;: 200 
86            },
87           
88          ]
89        },
90       
91        
92      ]
93    }
94  ],
95  &quot;Provider&quot;: {
96    &quot;agent&quot;: &quot;aaadd&quot;,
97    
98  }
99}
100data = {
101  &quot;_id&quot;: &quot;30546c62-8ea0-4f1a-a239-cc7508041a7b&quot;,
102  &quot;IsActive&quot;: &quot;True&quot;,
103  &quot;name&quot;: &quot;Pixel 3&quot;,
104  &quot;phone&quot;: [
105    {
106      &quot;Bill&quot;: 145,
107      &quot;phonetype&quot;: &quot;xyz&quot;,
108      &quot;insurance&quot;: [
109        {
110          &quot;year_one_claims&quot;: [
111            {
112             &quot;2020&quot;: 200 
113            },
114            {
115              
116            },
117            {
118              
119            },
120            {
121              
122            },
123            {
124              
125            }
126          ]
127        },
128        {
129          &quot;year_two_claims&quot;: [
130            {
131              
132            },
133            {
134              
135            },
136            {
137              
138            },
139            {
140              
141            },
142            {
143              
144            }
145          ]
146        },
147        
148      ]
149    }
150  ],
151  &quot;Provider&quot;: {
152    &quot;agent&quot;: &quot;aaadd&quot;,
153    
154  }
155}
156for phn_data in data['phone']:
157    for ins in phn_data['insurance']:
158        for key, val in list(ins.items()):
159            for ins_data in list(val):
160                if not ins_data:
161                    val.remove(ins_data)
162            if not val:
163                del ins[key]
164                phn_data['insurance'].remove(ins)
165                    
166print (data)
167

Output:

1def remove_empty_elements(jsonData):
2    if(isinstance(jsonData, list) or isinstance(jsonData,dict)):
3
4        for elem in list(jsonData):
5            if not isinstance(elem, dict) and isinstance(jsonData[elem], list) and elem:
6                jsonData[elem] = [x for x in jsonData[elem] if x]
7                if(len(jsonData[elem])==0):
8                    del jsonData[elem]
9            elif not isinstance(elem, dict) and isinstance(jsonData[elem], dict) and not jsonData[elem]:
10                del jsonData[elem]
11    else:
12        pass
13    return jsonData
14
15
16{
17  &quot;_id&quot;: &quot;30546c62-8ea0-4f1a-a239-cc7508041a7b&quot;,
18  &quot;IsActive&quot;: &quot;True&quot;,
19  &quot;name&quot;: &quot;Pixel 3&quot;,
20  &quot;phone&quot;: [
21    {
22      &quot;Bill&quot;: 145,
23      &quot;phonetype&quot;: &quot;xyz&quot;,
24      &quot;insurance&quot;: [
25        {
26          &quot;year_one_claims&quot;: [
27            {
28             &quot;2020&quot;: 200 
29            },
30            {
31              
32            },
33            {
34              
35            },
36            {
37              
38            },
39            {
40              
41            }
42          ]
43        },
44        {
45          &quot;year_two_claims&quot;: [
46            {
47              
48            },
49            {
50              
51            },
52            {
53              
54            },
55            {
56              
57            },
58            {
59              
60            }
61          ]
62        },
63        
64      ]
65    }
66  ],
67  &quot;Provider&quot;: {
68    &quot;agent&quot;: &quot;aaadd&quot;,
69    
70  }
71}
72
73{
74  &quot;_id&quot;: &quot;30546c62-8ea0-4f1a-a239-cc7508041a7b&quot;,
75  &quot;IsActive&quot;: &quot;True&quot;,
76  &quot;name&quot;: &quot;Pixel 3&quot;,
77  &quot;phone&quot;: [
78    {
79      &quot;Bill&quot;: 145,
80      &quot;phonetype&quot;: &quot;xyz&quot;,
81      &quot;insurance&quot;: [
82        {
83          &quot;year_one_claims&quot;: [
84            {
85             &quot;2020&quot;: 200 
86            },
87           
88          ]
89        },
90       
91        
92      ]
93    }
94  ],
95  &quot;Provider&quot;: {
96    &quot;agent&quot;: &quot;aaadd&quot;,
97    
98  }
99}
100data = {
101  &quot;_id&quot;: &quot;30546c62-8ea0-4f1a-a239-cc7508041a7b&quot;,
102  &quot;IsActive&quot;: &quot;True&quot;,
103  &quot;name&quot;: &quot;Pixel 3&quot;,
104  &quot;phone&quot;: [
105    {
106      &quot;Bill&quot;: 145,
107      &quot;phonetype&quot;: &quot;xyz&quot;,
108      &quot;insurance&quot;: [
109        {
110          &quot;year_one_claims&quot;: [
111            {
112             &quot;2020&quot;: 200 
113            },
114            {
115              
116            },
117            {
118              
119            },
120            {
121              
122            },
123            {
124              
125            }
126          ]
127        },
128        {
129          &quot;year_two_claims&quot;: [
130            {
131              
132            },
133            {
134              
135            },
136            {
137              
138            },
139            {
140              
141            },
142            {
143              
144            }
145          ]
146        },
147        
148      ]
149    }
150  ],
151  &quot;Provider&quot;: {
152    &quot;agent&quot;: &quot;aaadd&quot;,
153    
154  }
155}
156for phn_data in data['phone']:
157    for ins in phn_data['insurance']:
158        for key, val in list(ins.items()):
159            for ins_data in list(val):
160                if not ins_data:
161                    val.remove(ins_data)
162            if not val:
163                del ins[key]
164                phn_data['insurance'].remove(ins)
165                    
166print (data)
167{
168    '_id': '30546c62-8ea0-4f1a-a239-cc7508041a7b',
169    'IsActive': 'True',
170    'name': 'Pixel 3',
171    'phone': [{
172        'Bill': 145,
173        'phonetype': 'xyz',
174        'insurance': [{
175            'year_one_claims': [{
176                '2020': 200
177            }]
178        }]
179    }],
180    'Provider': {
181        'agent': 'aaadd'
182    }
183}
184

Source https://stackoverflow.com/questions/69476935

Community Discussions contain sources that include Stack Exchange Network

Tutorials and Learning Resources in Insurance

Tutorials and Learning Resources are not available at this moment for Insurance

Share this Page

share link

Get latest updates on Insurance