Create a JSON file from multiple dataframes by selecting rows based on keys











up vote
-2
down vote

favorite












I have n dataframes and all have common key columns. I want to take row from these n dataframes for which the key matches and create nested json file by differentiating columns by dataframe.



example: In this example i am using 3 dataframes only



dataframe1:



---------|-----------+-------+
| keycol | df1col1 |df1col2|
+--------|-----------+-------+
| x | a | 1 |
| y | b | 2 |
| z | c | 3 |
| p | d | 4 |
+--------------------+-------+


dataframe 2:



---------|-----------+-------+
| keycol | df2col1 |df2col2|
+--------|-----------+-------+
| x | m | 5 |
| y | n | 6 |
| z | o | 7 |
| p | p | 8 |
+--------------------+-------+


dataframe 3:



---------|-----------+-------+
| keycol | df3col1 |df3col2|
+--------|-----------+-------+
| x | g | 9 |
| y | h | 10 |
| z | i | 11 |
| p | j | 12 |
+--------------------+-------+


I should be able to create separate json files for each key related data from multiple dataframes. Sample json output structure i am looking for is depicted below.This is only for first key record like wise i want to create json files for all the key related data.



   {  
"keycol": "x"
{
"dataframe1"
{
"df1col1": "a"
"df1col2"" "1"
}
"dataframe2"
{
"df2col1": "m"
"df2col2"" "5"
}
"dataframe3"
{
"df3col1": "g"
"df3col2"" "9"
}
}
}


Thank you all for the great help. Thanks in advance.










share|improve this question






















  • Can someone help me out please. I am new to spark.
    – darla
    Nov 14 at 19:57















up vote
-2
down vote

favorite












I have n dataframes and all have common key columns. I want to take row from these n dataframes for which the key matches and create nested json file by differentiating columns by dataframe.



example: In this example i am using 3 dataframes only



dataframe1:



---------|-----------+-------+
| keycol | df1col1 |df1col2|
+--------|-----------+-------+
| x | a | 1 |
| y | b | 2 |
| z | c | 3 |
| p | d | 4 |
+--------------------+-------+


dataframe 2:



---------|-----------+-------+
| keycol | df2col1 |df2col2|
+--------|-----------+-------+
| x | m | 5 |
| y | n | 6 |
| z | o | 7 |
| p | p | 8 |
+--------------------+-------+


dataframe 3:



---------|-----------+-------+
| keycol | df3col1 |df3col2|
+--------|-----------+-------+
| x | g | 9 |
| y | h | 10 |
| z | i | 11 |
| p | j | 12 |
+--------------------+-------+


I should be able to create separate json files for each key related data from multiple dataframes. Sample json output structure i am looking for is depicted below.This is only for first key record like wise i want to create json files for all the key related data.



   {  
"keycol": "x"
{
"dataframe1"
{
"df1col1": "a"
"df1col2"" "1"
}
"dataframe2"
{
"df2col1": "m"
"df2col2"" "5"
}
"dataframe3"
{
"df3col1": "g"
"df3col2"" "9"
}
}
}


Thank you all for the great help. Thanks in advance.










share|improve this question






















  • Can someone help me out please. I am new to spark.
    – darla
    Nov 14 at 19:57













up vote
-2
down vote

favorite









up vote
-2
down vote

favorite











I have n dataframes and all have common key columns. I want to take row from these n dataframes for which the key matches and create nested json file by differentiating columns by dataframe.



example: In this example i am using 3 dataframes only



dataframe1:



---------|-----------+-------+
| keycol | df1col1 |df1col2|
+--------|-----------+-------+
| x | a | 1 |
| y | b | 2 |
| z | c | 3 |
| p | d | 4 |
+--------------------+-------+


dataframe 2:



---------|-----------+-------+
| keycol | df2col1 |df2col2|
+--------|-----------+-------+
| x | m | 5 |
| y | n | 6 |
| z | o | 7 |
| p | p | 8 |
+--------------------+-------+


dataframe 3:



---------|-----------+-------+
| keycol | df3col1 |df3col2|
+--------|-----------+-------+
| x | g | 9 |
| y | h | 10 |
| z | i | 11 |
| p | j | 12 |
+--------------------+-------+


I should be able to create separate json files for each key related data from multiple dataframes. Sample json output structure i am looking for is depicted below.This is only for first key record like wise i want to create json files for all the key related data.



   {  
"keycol": "x"
{
"dataframe1"
{
"df1col1": "a"
"df1col2"" "1"
}
"dataframe2"
{
"df2col1": "m"
"df2col2"" "5"
}
"dataframe3"
{
"df3col1": "g"
"df3col2"" "9"
}
}
}


Thank you all for the great help. Thanks in advance.










share|improve this question













I have n dataframes and all have common key columns. I want to take row from these n dataframes for which the key matches and create nested json file by differentiating columns by dataframe.



example: In this example i am using 3 dataframes only



dataframe1:



---------|-----------+-------+
| keycol | df1col1 |df1col2|
+--------|-----------+-------+
| x | a | 1 |
| y | b | 2 |
| z | c | 3 |
| p | d | 4 |
+--------------------+-------+


dataframe 2:



---------|-----------+-------+
| keycol | df2col1 |df2col2|
+--------|-----------+-------+
| x | m | 5 |
| y | n | 6 |
| z | o | 7 |
| p | p | 8 |
+--------------------+-------+


dataframe 3:



---------|-----------+-------+
| keycol | df3col1 |df3col2|
+--------|-----------+-------+
| x | g | 9 |
| y | h | 10 |
| z | i | 11 |
| p | j | 12 |
+--------------------+-------+


I should be able to create separate json files for each key related data from multiple dataframes. Sample json output structure i am looking for is depicted below.This is only for first key record like wise i want to create json files for all the key related data.



   {  
"keycol": "x"
{
"dataframe1"
{
"df1col1": "a"
"df1col2"" "1"
}
"dataframe2"
{
"df2col1": "m"
"df2col2"" "5"
}
"dataframe3"
{
"df3col1": "g"
"df3col2"" "9"
}
}
}


Thank you all for the great help. Thanks in advance.







apache-spark pyspark apache-spark-sql pyspark-sql






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 at 5:56









darla

43




43












  • Can someone help me out please. I am new to spark.
    – darla
    Nov 14 at 19:57


















  • Can someone help me out please. I am new to spark.
    – darla
    Nov 14 at 19:57
















Can someone help me out please. I am new to spark.
– darla
Nov 14 at 19:57




Can someone help me out please. I am new to spark.
– darla
Nov 14 at 19:57

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53293973%2fcreate-a-json-file-from-multiple-dataframes-by-selecting-rows-based-on-keys%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53293973%2fcreate-a-json-file-from-multiple-dataframes-by-selecting-rows-based-on-keys%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to change which sound is reproduced for terminal bell?

Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

Can I use Tabulator js library in my java Spring + Thymeleaf project?