rpy2/pandas: passing a pandas DataFrame with list type column into R





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















I have some R code that takes as input a data.frame where one of the columns holds list instances in its cells, e.g.:



data.frame(x=c("a", "b", "c"), y=I(list(c(1,2,3),c(4,5,6),c(7,8,9))))


Which looks like



  x       y
1 a 1, 2, 3
2 b 4, 5, 6
3 c 7, 8, 9


I would like create a similar pandas DataFrame in Python and pass it into my R code using the tools in the rpy2 library (version 2.9.4). However, when I run the following:



from rpy2.robjects import pandas2ri
pandas2ri.activate()

df = DataFrame({
"x" : ["a", "b", "c"] ,
"y" : [[1,2,3], [4,5,6], [7,8,9] ]
})

r_df = pandas2ri.py2ri(df)

print(r_df)


I get this output:



  x y.0.1L y.0.2L y.0.3L y.1.4L y.1.5L y.1.6L y.2.7L y.2.8L y.2.9L

0 a 1 2 3 4 5 6 7 8 9

1 b 1 2 3 4 5 6 7 8 9

2 c 1 2 3 4 5 6 7 8 9


Of course, the R code was using the "Inhibit" function I() in order to be able to hold the list instances in the data.frame in the first place. Is there some way to express similar intent via the rpy2 interfaces?










share|improve this question

























  • But why are you saving a list in a data frame? What analysis needs this setup? In both pandas and R, save only atomic (not nested) values within columns. Hence why every column in a pandas DataFrame is a pandas Series and every column in a R data.frame is an atomic vector!

    – Parfait
    Nov 16 '18 at 19:14




















1















I have some R code that takes as input a data.frame where one of the columns holds list instances in its cells, e.g.:



data.frame(x=c("a", "b", "c"), y=I(list(c(1,2,3),c(4,5,6),c(7,8,9))))


Which looks like



  x       y
1 a 1, 2, 3
2 b 4, 5, 6
3 c 7, 8, 9


I would like create a similar pandas DataFrame in Python and pass it into my R code using the tools in the rpy2 library (version 2.9.4). However, when I run the following:



from rpy2.robjects import pandas2ri
pandas2ri.activate()

df = DataFrame({
"x" : ["a", "b", "c"] ,
"y" : [[1,2,3], [4,5,6], [7,8,9] ]
})

r_df = pandas2ri.py2ri(df)

print(r_df)


I get this output:



  x y.0.1L y.0.2L y.0.3L y.1.4L y.1.5L y.1.6L y.2.7L y.2.8L y.2.9L

0 a 1 2 3 4 5 6 7 8 9

1 b 1 2 3 4 5 6 7 8 9

2 c 1 2 3 4 5 6 7 8 9


Of course, the R code was using the "Inhibit" function I() in order to be able to hold the list instances in the data.frame in the first place. Is there some way to express similar intent via the rpy2 interfaces?










share|improve this question

























  • But why are you saving a list in a data frame? What analysis needs this setup? In both pandas and R, save only atomic (not nested) values within columns. Hence why every column in a pandas DataFrame is a pandas Series and every column in a R data.frame is an atomic vector!

    – Parfait
    Nov 16 '18 at 19:14
















1












1








1








I have some R code that takes as input a data.frame where one of the columns holds list instances in its cells, e.g.:



data.frame(x=c("a", "b", "c"), y=I(list(c(1,2,3),c(4,5,6),c(7,8,9))))


Which looks like



  x       y
1 a 1, 2, 3
2 b 4, 5, 6
3 c 7, 8, 9


I would like create a similar pandas DataFrame in Python and pass it into my R code using the tools in the rpy2 library (version 2.9.4). However, when I run the following:



from rpy2.robjects import pandas2ri
pandas2ri.activate()

df = DataFrame({
"x" : ["a", "b", "c"] ,
"y" : [[1,2,3], [4,5,6], [7,8,9] ]
})

r_df = pandas2ri.py2ri(df)

print(r_df)


I get this output:



  x y.0.1L y.0.2L y.0.3L y.1.4L y.1.5L y.1.6L y.2.7L y.2.8L y.2.9L

0 a 1 2 3 4 5 6 7 8 9

1 b 1 2 3 4 5 6 7 8 9

2 c 1 2 3 4 5 6 7 8 9


Of course, the R code was using the "Inhibit" function I() in order to be able to hold the list instances in the data.frame in the first place. Is there some way to express similar intent via the rpy2 interfaces?










share|improve this question
















I have some R code that takes as input a data.frame where one of the columns holds list instances in its cells, e.g.:



data.frame(x=c("a", "b", "c"), y=I(list(c(1,2,3),c(4,5,6),c(7,8,9))))


Which looks like



  x       y
1 a 1, 2, 3
2 b 4, 5, 6
3 c 7, 8, 9


I would like create a similar pandas DataFrame in Python and pass it into my R code using the tools in the rpy2 library (version 2.9.4). However, when I run the following:



from rpy2.robjects import pandas2ri
pandas2ri.activate()

df = DataFrame({
"x" : ["a", "b", "c"] ,
"y" : [[1,2,3], [4,5,6], [7,8,9] ]
})

r_df = pandas2ri.py2ri(df)

print(r_df)


I get this output:



  x y.0.1L y.0.2L y.0.3L y.1.4L y.1.5L y.1.6L y.2.7L y.2.8L y.2.9L

0 a 1 2 3 4 5 6 7 8 9

1 b 1 2 3 4 5 6 7 8 9

2 c 1 2 3 4 5 6 7 8 9


Of course, the R code was using the "Inhibit" function I() in order to be able to hold the list instances in the data.frame in the first place. Is there some way to express similar intent via the rpy2 interfaces?







python r pandas rpy2






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 19:07







sparc_spread

















asked Nov 16 '18 at 17:41









sparc_spreadsparc_spread

6,54253244




6,54253244













  • But why are you saving a list in a data frame? What analysis needs this setup? In both pandas and R, save only atomic (not nested) values within columns. Hence why every column in a pandas DataFrame is a pandas Series and every column in a R data.frame is an atomic vector!

    – Parfait
    Nov 16 '18 at 19:14





















  • But why are you saving a list in a data frame? What analysis needs this setup? In both pandas and R, save only atomic (not nested) values within columns. Hence why every column in a pandas DataFrame is a pandas Series and every column in a R data.frame is an atomic vector!

    – Parfait
    Nov 16 '18 at 19:14



















But why are you saving a list in a data frame? What analysis needs this setup? In both pandas and R, save only atomic (not nested) values within columns. Hence why every column in a pandas DataFrame is a pandas Series and every column in a R data.frame is an atomic vector!

– Parfait
Nov 16 '18 at 19:14







But why are you saving a list in a data frame? What analysis needs this setup? In both pandas and R, save only atomic (not nested) values within columns. Hence why every column in a pandas DataFrame is a pandas Series and every column in a R data.frame is an atomic vector!

– Parfait
Nov 16 '18 at 19:14














0






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',
autoActivateHeartbeat: false,
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%2f53342868%2frpy2-pandas-passing-a-pandas-dataframe-with-list-type-column-into-r%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53342868%2frpy2-pandas-passing-a-pandas-dataframe-with-list-type-column-into-r%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

Bressuire

Vorschmack

Quarantine