0

I am trying to create a tuple from column values of a Dataframe, which looks like:

        0              1            2
0   5005_P1_080  5005_P1_021         None
1   5005_P2_316  5005_P2_298         None
2   5005_P2_317  5005_P2_318         None
3   5013_P2_159  5013_P2_305  5013_P2_304
4   5013_P2_434  5013_P2_437  5013_P2_439
5   5005_P1_538  5006_P2_054  5005_P1_535

I am trying to create a tuple from the 3 columns [0,1,2] for only the column values, but the tuple is looking like this:


(0    5005_P1_080
Name: One, dtype: object, 0    5005_P1_021
Name: Two, dtype: object, 0    None
Name: Three, dtype: object)

whereas it should only be like:


(5005_P1_080, 5005_P1_21, None)

The code I have used is:

B1.columns =['One', 'Two','Three']
Hole1 = B1['One']
Hole2 = B1['Two']
Hole3 = B1['Three']
Hole_Tuple = tuple(Hole1, Hole2, Hole3)
print(Hole_Tuple)

zsh_18
  • 1,012
  • 1
  • 11
  • 29
  • Please share your code so that we can see what went wrong. – Errol Apr 07 '20 at 02:54
  • I think this could be helpful https://stackoverflow.com/questions/9758450/pandas-convert-dataframe-to-array-of-tuples – Jorge Avila Apr 07 '20 at 02:55
  • @Errol- Hi Errol, i have edited the question and added my code. Thanks – zsh_18 Apr 07 '20 at 02:59
  • @JorgeAvila- Hi, I tried this before posting a question but this is not working for me. Thanks – zsh_18 Apr 07 '20 at 03:00
  • 1
    `df.apply(tuple, axis=1).to_list()`? – Quang Hoang Apr 07 '20 at 03:00
  • @QuangHoang- I tried this earlier but it gives me an error that - "series type object does not attribute to_list()." – zsh_18 Apr 07 '20 at 03:06
  • @zsh_18 [`series.tolist()`](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.tolist.html) is what Quang meant – anky Apr 07 '20 at 03:12
  • @anky- when i try this i get this error - AttributeError: 'Series' object has no attribute 'to_list'. – zsh_18 Apr 07 '20 at 03:17
  • @zsh_18 i tagged you to the link , there is no underscore between `to` and `list` , instead of `to_list()` , do `tolist()` – anky Apr 07 '20 at 03:22
  • @anky- oh ...righttt ....it creates a list of a tuple, how do i extract the tuple out of it. – zsh_18 Apr 07 '20 at 03:24
  • @anky- Thanks i got it right. if you can please post this as an answer, i can accept it. – zsh_18 Apr 07 '20 at 03:27
  • @zsh_18 it is actually a duplicate of https://stackoverflow.com/questions/45285244/convert-a-dataframe-to-list-of-tuples , hence I have closed it – anky Apr 07 '20 at 03:30

0 Answers0