PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0
Showing posts with label merge. Show all posts
Showing posts with label merge. Show all posts

Monday, November 7, 2022

[FIXED] How to write a python3 function that merges two lists of sets so that the output list has sets of elements that were present in both input sets?

 November 07, 2022     data-structures, merge, python, set, time-complexity     No comments   

Issue

I have two lists of sets, let's say: [{1, 2, 3}, {4, 5}, {6, 7}] and [{1, 2}, {3, 4}, {5, 6, 7}]

No set in the list has the same element and the sum of all sets in both lists is the same. The function should check if the sets in both lists had the same elements. If there were some differences, put them in another set.

So above example should return: [{1, 2}, {3}, {4}, {5}, {6, 7}]

I work on large sets so I would need this function to be as effective as possible.

Here is the example code and how I would like it to work:

def mergeSets(x, y):
    out = set()
    for i in x:
        out = out.union(i)
        # this allows me to get the set of all elements but here where my mind stops working
        # the problem sounds simple but thinking hours I can not think of good algorythm for this       issue :(
        # I found set.intersection() function but it works on single sets only, not lists of sets
    return out


x = mergeSets([{1, 2, 3}, {4, 5}, {6, 7}], [{1, 2}, {3, 4}, {5, 6, 7}])
print(x)
# [{1, 2}, {3}, {4}, {5}, {6, 7}]
x = mergeSets([{1, 2}, {3, 4, 5, 6, 7}, {8}], [{1}, {2, 3, 4}, {5, 6, 7, 8}])
print(x)
# [{1}, {2}, {3, 4}, {5, 6, 7}, {8}]

EDIT: the data doesn't have to be sorted and may be even of different types than integer

EDIT2: the input lists don't have to be sorted so sets may appear in random order


Solution

Given that each value occurs in exactly two sets (one per input list), you could collect the index pairs for each value, where an index pair gives an indication in which two sets (at which indexes in both lists) the value occurs.

Unique pairs indicate unique sets in the output, so a dictionary of such pairs can serve to populate the result:

from collections import defaultdict

def merge_sets(lista, listb):
    index_in_a = {
        val: idx
        for idx, elem in enumerate(lista) for val in elem
    }
    set_by_key = defaultdict(set)
    for idx, elem in enumerate(listb):
        for val in elem:
            set_by_key[(index_in_a[val], idx)].add(val)
    return list(set_by_key.values())

This looks O(n) to me.

NB: since the order of iteration over a set is not defined, the order of the output can look a bit mingled, but I assume the order of where sets appear in the output is not significant.



Answered By - trincot
Answer Checked By - Dawn Plyler (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Saturday, October 29, 2022

[FIXED] How to perform left join using multiple columns where one data frame has missingness in one column?

 October 29, 2022     dplyr, left-join, merge, r     No comments   

Issue

Suppose I have two data frames like the following:

df1 <- data.frame(
    id1  = c(1:4, 4),
    id2  = letters[1:5],
    val1 = c(0, 1, pi, exp(1), 42)
)

df2 <- data.frame(
    id1  = c(1:4, 4),
    id2  = c(NA, letters[2:5]),
    val2 = c("Lorem", "ipsum", "dolor", "sit", "amet")
)

##   df1                   df2
##   id1 id2      val1     id1  id2  val2
## 1   1   a  0.000000       1 <NA> Lorem
## 2   2   b  1.000000       2    b ipsum
## 3   3   c  3.141593       3    c dolor
## 4   4   d  2.718282       4    d   sit
## 5   4   e 42.000000       4    e  amet

This would be my desired result:

desired_result <- data.frame(
    id1  = c(1:4, 4),
    id2  = letters[1:5],
    val1 = c(0, 1, pi, exp(1), 42),
    val2 = c("Lorem", "ipsum", "dolor", "sit", "amet")
)

##   id1 id2      val1  val2
## 1   1   a  0.000000 Lorem
## 2   2   b  1.000000 ipsum
## 3   3   c  3.141593 dolor
## 4   4   d  2.718282   sit
## 5   4   e 42.000000  amet

In my desired result, I'd like to use the information in column id2, when available, to resolve join ambiguities raised by duplicate values in id1. For example, rows 4 and 5 have the same id1, but we can differentiate them by id2. So, if I try to join just on id1, I get too many observations, because I'm not utilizing this information in id2 to match records:

library(dplyr)
left_join(df1, df2, by = "id1")
##   id1 id2.x      val1 id2.y  val2
## 1   1     a  0.000000  <NA> Lorem
## 2   2     b  1.000000     b ipsum
## 3   3     c  3.141593     c dolor
## 4   4     d  2.718282     d   sit
## 5   4     d  2.718282     e  amet
## 6   4     e 42.000000     d   sit
## 7   4     e 42.000000     e  amet

However, if I try to join on both IDs, I lose the information in val2 for row 1, because df2 has NA for id2 on row 1:

left_join(df1, df2, by = c("id1", "id2"))
##  id1 id2      val1  val2
## 1   1   a  0.000000  <NA>
## 2   2   b  1.000000 ipsum
## 3   3   c  3.141593 dolor
## 4   4   d  2.718282   sit
## 5   4   e 42.000000  amet

How can I left_join() (or equivalent) to achieve my desired_result?


Solution

An option using data.table:

library(data.table)
setDT(df1)
setDT(df2)
df1[df2, on=.(id1, id2), mult="first", val2 := val2]
df1[is.na(val2), val2 :=
    df2[.SD, on=.(id1), mult="first", val2]]

I have taken the liberty of using the first value if there are multiple joins (i.e. the combination of id1 and id2 in df2 are not unique).



Answered By - chinsoon12
Answer Checked By - Mildred Charles (PHPFixing Admin)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

[FIXED] How to match elements from data frame with values from an array in R?

 October 29, 2022     arrays, dataframe, left-join, merge, r     No comments   

Issue

I want to match elements from df1 with values from an array1.

df1 <- (c('A','S','E','E','V','G','H','P','K','L','W','N','P','A','A','S','E','N','M','Y','S','G','D','R','H'))


array1

     1    2    3    4    5    6    7    8    9   10   11  12   13  14   15   16   17   18   19   20   21   22   23   24   25
A 0.15 0.00 0.10 0.10 0.05 0.00 0.05 0.00 0.00 0.05 0.00 0.0 0.00 0.0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.05 0.00 0.00 0.00
C 0.00 0.00 0.00 0.00 0.00 0.05 0.00 0.00 0.00 0.00 0.00 0.0 0.00 0.0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
D 0.00 0.00 0.00 0.00 0.00 0.00 0.10 0.00 0.05 0.05 0.00 0.0 0.10 0.0 0.00 0.25 0.10 0.20 0.10 0.00 0.15 0.05 0.00 0.00 0.05
E 0.05 0.10 0.05 0.05 0.00 0.05 0.00 0.10 0.10 0.20 0.00 0.0 0.05 0.0 0.00 0.00 0.05 0.10 0.00 0.20 0.10 0.05 0.15 0.10 0.10
F 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.05 0.00 0.00 0.05 0.0 0.05 0.0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
G 0.00 0.00 0.10 0.00 0.05 0.00 0.00 0.00 0.05 0.00 0.00 0.0 0.00 0.0 0.00 0.00 0.00 0.00 0.05 0.00 0.00 0.05 0.00 0.00 0.00
H 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.0 0.00 0.0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
I 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.05 0.2 0.05 0.1 0.05 0.05 0.00 0.00 0.00 0.00 0.05 0.00 0.00 0.00 0.00
K 0.00 0.10 0.00 0.05 0.00 0.05 0.05 0.05 0.00 0.00 0.00 0.0 0.00 0.0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.10 0.00 0.05
L 0.00 0.00 0.05 0.05 0.05 0.05 0.10 0.00 0.10 0.00 0.00 0.0 0.00 0.2 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.05 0.00
M 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.0 0.00 0.0 0.00 0.00 0.05 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
N 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.0 0.00 0.0 0.00 0.00 0.00 0.05 0.05 0.00 0.00 0.00 0.00 0.05 0.00
P 0.00 0.00 0.00 0.05 0.05 0.00 0.10 0.10 0.00 0.00 0.00 0.0 0.00 0.0 0.00 0.00 0.00 0.00 0.00 0.05 0.00 0.10 0.00 0.05 0.00
Q 0.00 0.05 0.05 0.00 0.10 0.00 0.00 0.00 0.00 0.00 0.05 0.0 0.00 0.1 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.05
R 0.00 0.00 0.05 0.00 0.05 0.15 0.00 0.00 0.00 0.05 0.00 0.0 0.00 0.0 0.00 0.00 0.00 0.00 0.05 0.00 0.00 0.00 0.00 0.00 0.00
S 0.10 0.10 0.00 0.00 0.05 0.00 0.00 0.00 0.00 0.00 0.05 0.0 0.00 0.0 0.15 0.10 0.20 0.05 0.10 0.10 0.05 0.00 0.05 0.05 0.10
T 0.00 0.00 0.00 0.05 0.00 0.05 0.00 0.05 0.05 0.00 0.00 0.0 0.00 0.0 0.05 0.00 0.00 0.00 0.00 0.00 0.00 0.05 0.05 0.05 0.00
V 0.05 0.05 0.00 0.05 0.00 0.00 0.00 0.05 0.05 0.00 0.10 0.2 0.15 0.0 0.15 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
W 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.0 0.00 0.0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
Y 0.05 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.05 0.10 0.0 0.00 0.0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00

The expected outcome can be a list or a df:

0.15, 0.10, 0.05, 0.05, 0.00, 0.00, 0.00, 0.10, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.10, 0.05, 0.05, 0.00, 0.00, 0.05, 0.05, 0.00, 0.00, 0.00

This is what I have tried:

res <- left_join(df1, array1, by = array1[[y]])
view(res)

Solution

You can use matrix subsetting on array1 :

array1[cbind(match(df1, rownames(array1)), 1:ncol(array1))]

#[1] 0.15 0.10 0.05 0.05 0.00 0.00 0.00 0.10 0.00 0.00 0.00 0.00 0.00
#[14] 0.00 0.00 0.10 0.05 0.05 0.00 0.00 0.05 0.05 0.00 0.00 0.00

match(df1, rownames(array1)) creates a row number to subset based on values in df1.



Answered By - Ronak Shah
Answer Checked By - Marilyn (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

[FIXED] How to join two csv files in R and match the data from both tables?

 October 29, 2022     csv, left-join, merge, r     No comments   

Issue

I have two csv files

csv 1 has this data This is the data of csv 1

and csv 2 has this as its data Data of csv 2

I want to add the episode review column from csv 2 to csv 1 with the correct episode name. How could I get this done? Any help is appreciated

EDIT: This is the code that got it work

library(tidyverse)
library(rvest)
library(dplyr)

df1 = read.csv('NewEpisodes.csv', head = T)
df2 = read.csv('episodesReview.csv', head = T, )


dfLeft = df1 %>% left_join(df2, by=c("Episode.Name", "Show.Name"))

Solution

try this:

final_csv = merge(csv_1, csv_2, by = c("Show.Name", "Episode.Name"))


Answered By - aaaaa
Answer Checked By - Pedro (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Friday, October 28, 2022

[FIXED] How to match elements between dataframes based on conditions in base R or dplyr?

 October 28, 2022     dplyr, left-join, match, merge, r     No comments   

Issue

I am trying to match these two data frames, nCode and grpRnk, as illustrated below.

enter image description here

Using the code shown at the bottom, I've been able to get the below output whereby the last column on the right shows the correct values, but I don't need those extra columns from grpRnk$Name through $subGrp (columns 6 -10) and I had specified a name for the last column of grpRnk but I instead get $grpRnk. Am I using this merge() function correctly? Is there a more correct or efficient way to do this sort of multi-factor matching?

  Name  Group nmCnt seqBase subGrp grpRnk$Name $Group $nmCnt $seqBase $subGrp $grpRnk
  <chr> <dbl> <int>   <int>  <int> <chr>        <dbl>  <int>    <int>   <int>   <int>
1 R         0     1       1      0 B                0      1        1       0      NA
2 R         0     2       2      0 R                0      1        1       0      NA
3 B         0     1       1      0 R                0      2        2       0      NA
4 R         0     3       3      0 R                0      3        3       0      NA
5 X         1     1       1      1 X                1      1        1       1       1
6 X         1     2       1      2 X                1      2        1       2       1

The code:

library(dplyr)

myDF1 <- 
  data.frame(
    Name = c("R","R","B","R","X","X"),
    Group = c(0,0,0,0,1,1)
  )

nCode <-  myDF1 %>%
  group_by(Name) %>%
  mutate(nmCnt = row_number()) %>%
  ungroup() %>%
  mutate(seqBase = ifelse(Group == 0 | Group != lag(Group), nmCnt,0)) %>%
  mutate(seqBase = na_if(seqBase, 0)) %>%
  group_by(Name) %>%
  fill(seqBase) %>%
  mutate(seqBase = match(seqBase, unique(seqBase))) %>%
  ungroup() %>%
  mutate(subGrp = as.integer(ifelse(Group > 0, sapply(1:n(), function(x) sum(Name[1:x]==Name[x] & Group[1:x] == Group[x])),0))) 

grpRnk <- nCode %>% select(Name,Group,nmCnt) %>% 
  filter(Group > 0) %>% 
  group_by(Name,Group) %>% 
  slice(which.min(Group)) %>% 
  ungroup() %>%
  arrange(nmCnt) %>%
  mutate(grpRnk = dense_rank(nmCnt)) %>%
  select (-nmCnt)

nCode %>% mutate(grpRnk = merge(nCode,grpRnk, by=c("Name","Group"), all.x=T))

Solution

You need to specify what column you want to extract into your newly created grpRnk column. You can achieve this by adding $grpRnk to the end of your merge() statement.

Here I have first a solution with merge():

library(tidyverse)

myDF1 <- 
  data.frame(
    Name = c("R","R","B","R","X","X"),
    Group = c(0,0,0,0,1,1)
  )

nCode <-  myDF1 %>%
  group_by(Name) %>%
  mutate(nmCnt = row_number()) %>%
  ungroup() %>%
  mutate(seqBase = ifelse(Group == 0 | Group != lag(Group), nmCnt,0)) %>%
  mutate(seqBase = na_if(seqBase, 0)) %>%
  group_by(Name) %>%
  fill(seqBase) %>%
  mutate(seqBase = match(seqBase, unique(seqBase))) %>%
  ungroup() %>%
  mutate(subGrp = as.integer(ifelse(Group > 0, sapply(1:n(), function(x) sum(Name[1:x]==Name[x] & Group[1:x] == Group[x])),0))) 

grpRnk <- nCode %>% select(Name,Group,nmCnt) %>% 
  filter(Group > 0) %>% 
  group_by(Name,Group) %>% 
  slice(which.min(Group)) %>% 
  ungroup() %>%
  arrange(nmCnt) %>%
  mutate(grpRnk = dense_rank(nmCnt)) %>%
  select (-nmCnt)

nCode %>% mutate(grpRnk = merge(nCode,grpRnk, by=c("Name","Group"), all.x = TRUE)$grpRnk)
#> # A tibble: 6 × 6
#>   Name  Group nmCnt seqBase subGrp grpRnk
#>   <chr> <dbl> <int>   <int>  <int>  <int>
#> 1 R         0     1       1      0     NA
#> 2 R         0     2       2      0     NA
#> 3 B         0     1       1      0     NA
#> 4 R         0     3       3      0     NA
#> 5 X         1     1       1      1      1
#> 6 X         1     2       1      2      1

However, you can achieve the same result using only merge() like this:

merge(nCode,grpRnk, by=c("Name","Group"), all.x = TRUE)
#>   Name Group nmCnt seqBase subGrp grpRnk
#> 1    B     0     1       1      0     NA
#> 2    R     0     1       1      0     NA
#> 3    R     0     2       2      0     NA
#> 4    R     0     3       3      0     NA
#> 5    X     1     1       1      1      1
#> 6    X     1     2       1      2      1

And here is another solution using left_join() from the dplyr package:

left_join(nCode, grpRnk, by = c("Name", "Group"))
#> # A tibble: 6 × 6
#>   Name  Group nmCnt seqBase subGrp grpRnk
#>   <chr> <dbl> <int>   <int>  <int>  <int>
#> 1 R         0     1       1      0     NA
#> 2 R         0     2       2      0     NA
#> 3 B         0     1       1      0     NA
#> 4 R         0     3       3      0     NA
#> 5 X         1     1       1      1      1
#> 6 X         1     2       1      2      1

Created on 2022-09-19 with reprex v2.0.2



Answered By - Noah
Answer Checked By - Katrina (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Tuesday, October 11, 2022

[FIXED] How can I merge 3 images into 1 image via PHP?

 October 11, 2022     gd, image, merge, php     No comments   

Issue

I really cannot find a way to successfully do it.. I've searched google for this and it either has black shades around the images or all the images don't overlap. Could you please help?

I am alright at PHP; I'd give myself a 2/5.. I would really appreciate if someone would be willing to help me out.

I'm looking for a simple api that goes something like:

$color=$_GET['color'];
$face=$_GET['face'];
$hat=$_GET['hat'];

echo '<img src="avatar.php?color=$color&face=$face&hat=$hat">';

Thanks for any help in advance. I can understand php from my knowledge of other languages, too, so don't be afraid to talk technical with me; but not too technical.


Solution

there are so many comments on this answer so I'm posting this as an answer.

Got it working on my pc.

use svens code :

    $images = array( $_GET['color'], $_GET['face'], $_GET['hat'] );

    // Allocate new image
    $img = imagecreatetruecolor(58, 75);
    // Make alpha channels work
    imagealphablending($img, true);
    imagesavealpha($img, true);

    foreach($images as $fn) {
        // Load image
        $cur = imagecreatefrompng($fn);
        imagealphablending($cur, true);
        imagesavealpha($cur, true);

        // Copy over image
        imagecopy($img, $cur, 0, 0, 0, 0, 58, 75);

        // Free memory
        imagedestroy($cur);
    }   

    header('Content-Type: image/png');  // Comment out this line to see PHP errors
    imagepng($img);

?>

I renamed your images like this so its easier :


smile : a.png
headset : b.png
blue : c.png

Turns out the problem is with the layering it. Putting one behind the other

after you rename the images, use this url -- it will work(works on my pc).

YOUR_FILE.php?hat=b.png&color=c.png&face=a.png

This will still give you a black background. I am not sure if you have the exact same code as above in your file on the server - because I played around with the image order on your link and it does not help. Try copy-pasting this exact same code on a different file and then trying. Play around with the order and check the results.



Answered By - DMin
Answer Checked By - Willingham (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Monday, August 29, 2022

[FIXED] How to combine two csv files together

 August 29, 2022     csv, merge, pandas, python-3.x     No comments   

Issue

I already looked at: How to combine 2 csv files with common column value, but both files have different number of lines and: Merging two CSV files using Python But both did not give the desired output I needed.

I have two csv files with the below data:

First file is - data1.csv

Name             Dept        Company  
John Smith       candy       lead
Diana Princ      candy       lead
Perry Plat       wood        lead
Jerry Springer   clothes     lead
Calvin Klein     clothes     lead   
Lincoln Tun      warehouse   lead   
Oliver Twist     kitchen     lead

Second file is - data2.csv

Name             Dept        Company  
John Smith       candy       lead
Tyler Perry      candy       lead
Perry Plat       wood        lead
Mary Poppins     clothes     lead
Calvin Klein     clothes     lead   
Lincoln Tun      warehouse   lead   
Herman Sherman   kitchen     lead
Jerry Springer   clothes     lead
Ivan Evans       clothes     lead

I want to merge them as one file, called newdata.csv, sorting the Dept column into groups and dropping the Company column. The final output would look something like this:

Name             Dept        
John Smith       candy       
Diana Princ      candy       
Tyler Perry      candy       
Perry Plat       wood       
Jerry Springer   clothes     
Calvin Klein     clothes     
Mary Poppins     clothes     
Ivan Evans       clothes     
Lincoln Tun      warehouse   
Oliver Twist     kitchen     
Herman Sherman   kitchen   

I tried to use the merge function, but the output wasn't what I needed.

This is my code so far:

import pandas as pd
import os, csv, sys

csvPath1 = 'data1.csv'
csvPath2 = 'data2.csv'
csvDest = 'newdata.csv'

df1 = pd.read_csv(csvPath1)
df2 = pd.read_csv(csvPath2)

df1=df1.drop('Company', 1)
df2=df2.drop('Company', 1)

merged = df1.merge(df2)
merged=merged.sort_values('Dept')

merged.to_csv(csvDest, index=False)

Solution

I ended up finding the answer to my own question. I did some digging and what worked for me was using:

merged=df1.append(df2)
merged=merged.sort_values('Dept')

So my final code output:

import pandas as pd
import os, csv, sys

csvPath1 = 'data1.csv'
csvPath2 = 'data2.csv'
csvDest = 'newdata.csv'

df1 = pd.read_csv(csvPath1)
df2 = pd.read_csv(csvPath2)

df1=df1.drop('Company', 1)
df2=df2.drop('Company', 1)

merged=df1.append(df2)
merged=merged.sort_values('Dept')

merged.to_csv(csvDest, index=False)


Answered By - noobCoder
Answer Checked By - Senaida (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Thursday, August 11, 2022

[FIXED] How to merge two Hex numbers to one number and then convert it into decimal.?

 August 11, 2022     c, decimal, hex, merge, numbers     No comments   

Issue

I am making a C program in which I have two hex numbers, i.e. num1=25 num2=71 which are in hex. I want to make it as num3=2571 and then I have to convert 2571 into a decimal number. How do I do this? Please help, Thanks!


Solution

Just shift the digits and combine

int num1,num2,num3;
num1=0x25;
num2=0x71;
num3=(num1<<8)|(num2);
printf("%x %d",num3,num3);

You need to place 25 (0025) followed by 71 (0071) in a variable, so you have to left shift the first number by 8 bits (0025 to 2500) and combine it with num2. Logical Or is the equivalent for combining, hence the | symbol.



Answered By - Nerdy
Answer Checked By - David Goodson (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Friday, July 29, 2022

[FIXED] How to Merge two pages from a pdf file as one page

 July 29, 2022     image, merge, pdf, python, split     No comments   

Issue

I have a pdf in which there are total 6 pages of images.I want to merge page 1 and 2 as a single pdf and so on for 3 to 6 pages.

I splitted all 6 pages of pdf as individual pdf.

import os from PyPDF2 import PdfFileReader, PdfFileWriter

def pdf_splitter(path): fname = os.path.splitext(os.path.basename(path))[0]

pdf = PdfFileReader(path)
for page in range(pdf.getNumPages()):
    pdf_writer = PdfFileWriter()
    pdf_writer.addPage(pdf.getPage(page))

    output_filename = '{}_page_{}.pdf'.format(
        fname, page+1)

    with open(output_filename, 'wb') as out:
        pdf_writer.write(out)

    print('Created: {}'.format(output_filename))

if name == 'main': path = 'D:\Tasks\Samples\fw9.pdf' pdf_splitter(path)

I want to know how to merge page 1 and 2 of fw9 as single pdf file which contains only 1 page which have half page as page 1 of fw9 pdf file and another half as page 2 of fw9 pdf.I have to do this for all 6 pages as 1-2 as 1 pdf with 1 page ,3-4 page as another pdf which has only 1 page with both on the same page and so on.Kindly help if any one have any idea on how to do so.


Solution

The library pyPDF2 has also a PdfFileMerger object, that should do exactly what you want.

As from the example here you can just create a PdfFileMerger, read two pages and put them into one single file.

I changed your script slightly to create also files with pages 0-1, 2-3, 4-5 ecc.. (of course page 0 is the first page but python numbering starts from 0)

import os
from PyPDF2 import PdfFileReader, PdfFileWriter, PdfFileMerger

def pdf_splitter(path): 

    fname = os.path.splitext(os.path.basename(path))[0]

    pdf = PdfFileReader(path)
    input_paths = []
    for page in range(pdf.getNumPages()):
        pdf_writer = PdfFileWriter()
        pdf_writer.addPage(pdf.getPage(page))
        output_filename = '{}_page_{}.pdf'.format(fname, page+1)
        input_paths.append(output_filename)
        with open(output_filename, 'wb') as out:
            pdf_writer.write(out)

        print('Created: {}'.format(output_filename))

        # every 2 pages! 
        # Change the two if you need every other number of pages!
        if page % 2 == 1:
            pdf_merger = PdfFileMerger() #create pdfilemerger
            for path in input_paths: 
                pdf_merger.append(path) #read the single pages

            # we call it pages_N-1_N, so first would be pages_0_1!
            output_path = '{}_pages_{}_{}.pdf'.format(fname, page-1, page)
            with open(output_path, 'wb') as fileobj:
                pdf_merger.write(fileobj) # write the two pages pdf!

            input_paths = []

if __name__ == '__main__': 

    path = 'D:\Tasks\Samples\fw9.pdf' 
    pdf_splitter(path)

Is this what you wanted?

This will first create single pdf for each page and then combine them 2 to 2. Creating the single pdf could also be skipped, but I was not sure whether you want it or not.



Answered By - freerafiki
Answer Checked By - Mildred Charles (PHPFixing Admin)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Thursday, July 21, 2022

[FIXED] How to write a.dbf file

 July 21, 2022     append, dbf, merge, r     No comments   

Issue

I'm encountering issue using the below script. All are working fine except for the final line which results to the error below.

enter image description here

# read dbf
library(foreign)
setwd("C:/Users/JGGliban/Desktop/Work/ADMIN/Other Stream/PH")

# Combine multiple dbf files
# library('tidyverse')
# List all files ending with dbf in directory
dbf_files <- list.files(pattern = c("*.DBF","*.dbf"), full.names = TRUE)
# Read each dbf file into a list
dbf_list <- lapply(dbf_files, read.dbf, as.is = FALSE)
# Concatenate the data in each dbf file into one combined data frame
data <- do.call(rbind, dbf_list)
# Write dbf file - max-nchar is the maimum number of characters allowed in a character field. After the max, it will be truncated.
x <- write.dbf(data, file, factor2char = TRUE, max_nchar = 254)

Solution

Code modified to:

x <- write.dbf(data, "file.dbf", factor2char = TRUE, max_nchar = 254)


Answered By - Janine
Answer Checked By - Willingham (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Monday, July 11, 2022

[FIXED] How to use the default git commit message after resolving merge conflicts?

 July 11, 2022     commit, conflict, git, merge, message     No comments   

Issue

After doing a merge and resolving conflicts, is there an "easy" way to just accept the default generated commit message from the command line? One of our developers will resolve all the conflicts, and then do a git commit -m"Merge Commit" which replaces the generated commit message that listed all the conflict files. I would like to have a different flag that would just take the current file without modification. I know there is a -F or --file= option, but that requires knowing the file name all the time.

Thank you


Solution

Obviously the "right" answer here is to get your developer to follow correct practice for your team when generating merge commits. Note that the behavior you want used to be the default, and that only recently has git begun demanding "human generated" commit messages for merges. That was for a reason, and it wasn't so that developers would short-circuit the process with a meaningless message.

Maybe the developer is generating merge commits when s/he should be rebasing instead?

That said, the merge commit is the output of git fmt-merge-msg, to which you would have to feed the parents of the merge commit.



Answered By - Andy Ross
Answer Checked By - Timothy Miller (PHPFixing Admin)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Tuesday, May 17, 2022

[FIXED] How to merge two data frames based on part of a character variable?

 May 17, 2022     merge, partial, r     No comments   

Issue

I need to merge two datasets. The first dataset is the original one that I am working with and the second has data that I need to add. I would merge them based on the company name because that character variable is in both datasets, but they're presented differently (e.g. "Apple Inc." in one data frame and "Apple" in the other) and so I can't just use merge() like I otherwise would.

I think the best way is to try merge them based on these variables having the first x number of letters in common, but I don't know how to do this, nor do I know if this is even the best way to go about this.

Can anyone please help me with this? I have only been using R for a few months and don't have a programming background so this stuff doesn't come naturally to me.


Solution

A simple workaround would be to add a column with only the substring and use it for merging:

x$merge.col <- substr(x$company.name, 1, 5)
y$merge.col <- substr(y$company.name, 1, 5)
z <- merge(x, y, by="merge.col")


Answered By - cdalitz
Answer Checked By - Candace Johnson (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

[FIXED] How to merge two data frames with specific string match in columns in R?

 May 17, 2022     dataframe, match, merge, partial, r     No comments   

Issue

I have two dataframes data1 and data2 which have information like below:

dput(data1)

structure(list(ProfName = c("Hua (Christine) Xin", "Dereck Barr-Pulliam", 
"Lisa M. Blum", "Russell  Williamson", "William D. Stout", "Michael F. Wade", 
"Sheila A.  Johnston", "Julie Huang", "Alan Attaway", "Alan Levitan", 
"Benjamin P. Foster", "Carolyn M.  Callahan"), Title = c(" PhD", 
" PhD", " LLM", " PhD", " PhD", " CPA", " MS", " PhD", " PhD", 
" PhD", " PhD", " PhD"), Profession = c("Assistant Professor", 
"Assistant Professor", "Instructor", "Assistant Professor", "Associate Professor and Director", 
"Instructor", "Instructor", "Associate Professor", "Professor", 
"Professor", "Professor", "Brown-Forman Professor of Accountancy"
)), row.names = c(8L, 18L, 25L, 36L, 49L, 50L, 56L, 69L, 71L, 
82L, 88L, 89L), class = "data.frame")

It looks like below:

enter image description here

dput(data2)

structure(list(ProfName = c("Blandford, K     ", "Okafor, A     ", 
"Johnston, S     ", "Rolen, R     ", "Attaway, A     ", "Xin, H     ", 
"Huang, Y     ", "Stout, W     ", "Williamson, R     ", "Callahan, C     ", 
"Foster, B     ", "Blum, L     ", "Levitan, A     ", "Barr-Pulliam, D     ", 
"Wade, M     ")), row.names = c(NA, -15L), class = "data.frame")

data2 looks like below:

enter image description here

I wanted to merge two dataframes, but the names look different. Only a specific string is matching between two dataframes with column ProfName. The data should be merged and if the names don't have any information it should be empty. If they don't have any information in the columns Title and Profession, both ProfName and New columns should have the same name.

I tried using merge, but it didn't give the desired output.

merge(data1, data2, by="ProfName", all.x=TRUE, all.y = TRUE)

The output should look like below:

enter image description here


Solution

Here's a simple solution:

library(stringr)
library(dplyr)
library(tidyr)
library(magrittr)

data1 %<>% mutate(lname = str_extract(ProfName, "[A-Za-z\\-]+$"))
data2 %<>% mutate(lname = str_extract(ProfName, "^[A-Za-z\\-]+"))

df <- merge(data1, data2, all.y = TRUE, by = "lname")

head(df)

#          lname           ProfName.x Title                            Profession           # ProfName.y
# 1      Attaway         Alan Attaway   PhD                             Professor      Attaway, A     
# 2 Barr-Pulliam  Dereck Barr-Pulliam   PhD                   Assistant Professor Barr-Pulliam, D     
# 3    Blandford                 <NA>  <NA>                                  <NA>    Blandford, K     
# 4         Blum         Lisa M. Blum   LLM                            Instructor         Blum, L     
# 5     Callahan Carolyn M.  Callahan   PhD Brown-Forman Professor of Accountancy     Callahan, C     
# 6       Foster   Benjamin P. Foster   PhD                             Professor       Foster, B 


Answered By - Dunois
Answer Checked By - Pedro (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Friday, May 13, 2022

[FIXED] How do I merge and format arrays in Swift? (X and Y coordinate arrays)

 May 13, 2022     append, arrays, ios, merge, swift     No comments   

Issue

I am attempting to merge two arrays in specific formatting in order to create an array of x and y coordinates that will be used to create a graph. I have separate arrays of X-Values and Y-Values in [Double] format, for example:

var xAxis = [1, 2, 3, 4]
var yAxis = [2, 3, 4, 5]

I wish to merge these such that they are presented in the following format:

var chartPoints = [(1,2),(2,3),(3,4),(4,5)]

or more generally:

chartPoints = [(x,y)]

I have tried a few different options, such as the append and extend methods to no luck as this does not sort the arrays or format them in the method required.

How can I merge the two x and y axis arrays to a singular formatted array?


Solution

You can use the zip global function, which, given 2 sequences, returns a sequence of tuples:

let pointsSequence = zip(xAxis, yAxis)

You can then obtain an array of tuples by using the proper init:

let chartPoints = Array(pointsSequence)

Each element of the array is a (x, y) tuple - but values are unnamed, so you can access their individual values by index:

let point = chartPoints[0]
point.0 // This is the 1st element of the tuple
point.1 // This is the 2nd element of the tuple

If you prefer the tuple values to be named, you can make the tuple type explicit:

let chartPoints: [(x: Int, y: Int)] = Array(pointsSequence)

and with that you can access using either the index (as in the example above) or using their explicit names:

let point = chartPoints[0]
point.x
point.y


Answered By - Antonio
Answer Checked By - Terry (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

[FIXED] How to append rows to an R data frame

 May 13, 2022     append, dataframe, merge, r, rows     No comments   

Issue

I have looked around StackOverflow, but I cannot find a solution specific to my problem, which involves appending rows to an R data frame.

I am initializing an empty 2-column data frame, as follows.

df = data.frame(x = numeric(), y = character())

Then, my goal is to iterate through a list of values and, in each iteration, append a value to the end of the list. I started with the following code.

for (i in 1:10) {
    df$x = rbind(df$x, i)
    df$y = rbind(df$y, toString(i))
}

I also attempted the functions c, append, and merge without success. Please let me know if you have any suggestions.

Update from comment: I don't presume to know how R was meant to be used, but I wanted to ignore the additional line of code that would be required to update the indices on every iteration and I cannot easily preallocate the size of the data frame because I don't know how many rows it will ultimately take. Remember that the above is merely a toy example meant to be reproducible. Either way, thanks for your suggestion!


Solution

Update

Not knowing what you are trying to do, I'll share one more suggestion: Preallocate vectors of the type you want for each column, insert values into those vectors, and then, at the end, create your data.frame.

Continuing with Julian's f3 (a preallocated data.frame) as the fastest option so far, defined as:

# pre-allocate space
f3 <- function(n){
  df <- data.frame(x = numeric(n), y = character(n), stringsAsFactors = FALSE)
  for(i in 1:n){
    df$x[i] <- i
    df$y[i] <- toString(i)
  }
  df
}

Here's a similar approach, but one where the data.frame is created as the last step.

# Use preallocated vectors
f4 <- function(n) {
  x <- numeric(n)
  y <- character(n)
  for (i in 1:n) {
    x[i] <- i
    y[i] <- i
  }
  data.frame(x, y, stringsAsFactors=FALSE)
}

microbenchmark from the "microbenchmark" package will give us more comprehensive insight than system.time:

library(microbenchmark)
microbenchmark(f1(1000), f3(1000), f4(1000), times = 5)
# Unit: milliseconds
#      expr         min          lq      median         uq         max neval
#  f1(1000) 1024.539618 1029.693877 1045.972666 1055.25931 1112.769176     5
#  f3(1000)  149.417636  150.529011  150.827393  151.02230  160.637845     5
#  f4(1000)    7.872647    7.892395    7.901151    7.95077    8.049581     5

f1() (the approach below) is incredibly inefficient because of how often it calls data.frame and because growing objects that way is generally slow in R. f3() is much improved due to preallocation, but the data.frame structure itself might be part of the bottleneck here. f4() tries to bypass that bottleneck without compromising the approach you want to take.


Original answer

This is really not a good idea, but if you wanted to do it this way, I guess you can try:

for (i in 1:10) {
  df <- rbind(df, data.frame(x = i, y = toString(i)))
}

Note that in your code, there is one other problem:

  • You should use stringsAsFactors if you want the characters to not get converted to factors. Use: df = data.frame(x = numeric(), y = character(), stringsAsFactors = FALSE)


Answered By - A5C1D2H2I1M1N2O1R2T1
Answer Checked By - Marilyn (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Thursday, May 12, 2022

[FIXED] How to append a new dataset to existing dataset based on index timeseries condition in Python

 May 12, 2022     append, merge, pandas, python, time-series     No comments   

Issue

I'm really new to python. please any one can help my problem on how to append a new dataset to existing dataset based on index timeseries condition. I need to add each of row from df2 to df1 based on its time with tolerance <5min

here is the example of data I have

df1

Time A
01/9/2021 06:50 1
01/9/2021 06:55 2
01/9/2021 07:00 3
01/9/2021 07:05 6
01/9/2021 07:10 3
01/9/2021 07:15 2
01/9/2021 07:20 1
01/9/2021 07:25 2

df2

Time B
01/9/2021 06:51 0.6
01/9/2021 06:55 0.2
01/9/2021 07:12 0.3
01/9/2021 07:16 0.6

Expected outcome it will add each of row from df2 that match time with tolerance (let say 4 min) to the row of df1.

df3

Time A B
01/9/2021 06:50 1 0.6
01/9/2021 06:55 2 0.2
01/9/2021 07:00 3 NAN
01/9/2021 07:05 6 NAN
01/9/2021 07:10 3 0.3
01/9/2021 07:15 2 0.6
01/9/2021 07:20 1 NAN
01/9/2021 07:25 2 NAN

really appreciate you help. thank you


Solution

One way using pandas.to_datetime with pd.Series.dt.round:

df["Time"] = pd.to_datetime(df["Time"])
df2["Time"] = pd.to_datetime(df2["Time"]).dt.round("5min")

new_df = df.merge(df2, on="Time", how="left")
print(new_df)

Output:

                 Time  A    B
0 2021-01-09 06:50:00  1  0.6
1 2021-01-09 06:55:00  2  0.2
2 2021-01-09 07:00:00  3  NaN
3 2021-01-09 07:05:00  6  NaN
4 2021-01-09 07:10:00  3  0.3
5 2021-01-09 07:15:00  2  0.6
6 2021-01-09 07:20:00  1  NaN
7 2021-01-09 07:25:00  2  NaN


Answered By - Chris
Answer Checked By - Mary Flores (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Saturday, March 5, 2022

[FIXED] How to merge two tables that have the same schema into one, whith phpmyadmin

 March 05, 2022     merge, php, phpmyadmin     No comments   

Issue

I have two tables that have the same schema (tab1 and tab2), I would like to know how to merge them into one table, in a way to append tab2 under tab1.

I use phpmyadmin with WAMP.

to sum up what i want:

tab1

+tab2

= tab3 (tab3 = tab1 U tab2)

thank's


Solution

To answer your first question, in phpMyAdmin, open tab2, Operations > Copy table, choose tab1, and select Data only.

If you really want a tab3, open tab1, Operations > Copy table (structure and data) to tab3, then open tab2, Operations > Copy table (data only) to tab3.



Answered By - Marc Delisle
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Wednesday, January 19, 2022

[FIXED] Array Index merge of the same array

 January 19, 2022     arrays, cakephp, merge, php     No comments   

Issue

I have faced a problem. I have an array looks like this

print_r($log_ret_val);
Array ( [0] => Array ( [0] => Array ( [TestingLogDevice] => Array ( [Siteid] => Mirpur_CO ) ) 
                       [1] => Array ( [TestingLogDevice] => Array ( [Siteid] => Mirpur_CO ) ) 
      ) 
        [1] => Array ( [0] => Array ( [TestingLogDevice] => Array ( [Tempin] => 29 ) ) 
                       [1] => Array ( [TestingLogDevice] => Array ( [Tempin] => 29 ) ) 
      ) 
        [2] => Array ( [0] => Array ( [TestingLogDevice] => Array ( [Date_time] => 18.11.2017 11:03:33 ) ) 
                       [1] => Array ( [TestingLogDevice] => Array ( [Date_time] => 18.11.2017 11:00:31 ) ) 
     ) 
)

And this is what I write for that value

$log_val        = array();
foreach ($log_ret_val as $key => $valuee) {
    foreach ($valuee as $key => $val) {
        array_push($log_val,$val);
    }
}
print_r($log_val);

Array ( [0] => Array ( [TestingLogDevice] => Array ( [Siteid] => Mirpur_CO ) ) 
        [1] => Array ( [TestingLogDevice] => Array ( [Siteid] => Mirpur_CO ) ) 
        [2] => Array ( [TestingLogDevice] => Array ( [Tempin] => 29 ) )                   
        [3] => Array ( [TestingLogDevice] => Array ( [Tempin] => 28 ) ) 
        [4] => Array ( [TestingLogDevice] => Array ( [Date_time] => 18.11.2017 11:24:45 ) ) 
        [5] => Array ( [TestingLogDevice] => Array ( [Date_time] => 18.11.2017 11:21:43 ) ) 
)

But my desired output looks like this

Array ( [0] => Array ( [TestingLogDevice] => Array ( [Siteid] => Mirpur_CO [Tempin] => 29 [Date_time] => 18.11.2017 11:24:45 ) ) 
        [1] => Array ( [TestingLogDevice] => Array ( [Siteid] => Mirpur_CO [Tempin] => 28 [Date_time] => 18.11.2017 11:21:43 ) ) 
)

So what can I do meet up my desired output. For your kind suggestion or help please


Solution

Loop through $log_ret_val extract it key and use array_merge_recursive to create new array

$log_ret_val = Array ( "0" => Array ( "0" => Array ( "TestingLogDevice" => Array ( "Siteid" => "Mirpur_CO" ) ) ,
                       "1" => Array ( "TestingLogDevice" => Array ( "Siteid" => "Mirpur_CO" ) ) ,
  ) ,
        "1" => Array ( "0" => Array ( "TestingLogDevice" => Array ( "Tempin" => 29 ) ) ,
                      "1" => Array ( "TestingLogDevice" => Array ( "Tempin" => 29 ) ) ,
  ) ,
        "2" => Array ( "0" => Array ( "TestingLogDevice" => Array ( "Date_time" => "18.11.2017 11:03:33" ) ) ,
                       "1" => Array ( "TestingLogDevice" => Array ( "Date_time" => "18.11.2017 11:00:31" ) ) 
  )
  ); 
$log_val = array();
foreach ($log_ret_val as $key => $value) {
    foreach ($value as $key1 => $value1) {
        if(isset($log_val[$key1]))
            $log_val[$key1] = array_merge_recursive($log_val[$key1],$value1);
        else
            $log_val[$key1] = array_merge_recursive($value1);
    }
}
print_r($log_val);

DEMO



Answered By - B. Desai
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Older Posts Home
View mobile version

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
All Comments
Atom
All Comments

Copyright © PHPFixing