0

I have a pandas dataframe, for example:

import sys
if sys.version_info[0] < 3: 
    from StringIO import StringIO
else:
    from io import StringIO

import pandas as pd

TESTDATA = StringIO("""col1;col2;col3
    (1,2);(3,4);(5,6)
    (7,8);(9,10);(11,12)
    (13,14);(15,16);(17,18)
    (19,20);(21,22);(23,24)
    """)

df = pd.read_csv(TESTDATA, sep=";")

dataframe looks like this:

          col1     col2     col3
0        (1,2)    (3,4)    (5,6)
1        (7,8)   (9,10)  (11,12)
2      (13,14)  (15,16)  (17,18)
3      (19,20)  (21,22)  (23,24)

Now, let's say there's 2 rows 0 and 2 that I wish to replace with a numpy array of zeros such as:

np.zeros((1,3,2))
"""array([[[ 0.,  0.],
        [ 0.,  0.],
        [ 0.,  0.]]])"""

So the final result is that for the rows 0 and 2, I have tuples (0,0), (0,0), and (0,0). Could someone propose an approach?

1
  • How do you connect 6 values in np.zeros((1,3,2)) with 6 values in a row? There's more than one way you can align the 2 structures (flatten by row, by column, etc). Commented Mar 22, 2018 at 11:09

1 Answer 1

1

This would work:

arr = np.zeros((1, 3, 2))
row = [tuple(t) for t in arr[0]]
df.loc[0] = row
df.loc[2] = row
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.