1

I have a dataframe like this:

df=pd.DataFrame([[1,"10/1/2019","I",2879],
                 [1,"10/1/2019","O",196],
                 [1,"10/2/2019","I",2840],
                 [1,"10/2/2019","O",189],
                 [2,"10/1/2019","I",2907],
                 [2,"10/1/2019","O",195]],
                columns=["A","B","C","D"])
df.set_index(["A","B","C"],inplace=True)

when I display the contents they look more or less like this:

                    D
A   B           C   
1   10/1/2019   I   2879
                O   196
    10/2/2019   I   2840
                O   189
2   10/1/2019   I   2907
                O   195

So my question is how to generate a csv file which contents look like it shows on the notebook where "A" and "B" values become empty on subsequent rows. In other words, this is how I need the csv file output to look like:

A,B,C,D
1,10/1/2019,I,2879
,,O,196
,10/2/2019,I,2840
,,O,189
2,10/1/2019,I,2907
,,O,195
0

1 Answer 1

2

Don't set the index, and create some series that return True or False and where True make the values blank:

df=pd.DataFrame([[1,"10/1/2019","I",2879],
                 [1,"10/1/2019","O",196],
                 [1,"10/2/2019","I",2840],
                 [1,"10/2/2019","O",189],
                 [2,"10/1/2019","I",2907],
                 [2,"10/1/2019","O",195]],
                columns=["A","B","C","D"])
s1 = df.duplicated(subset=['A'])
s2 = df.duplicated(subset=['A','B'])
df['A'] = df['A'].where(~s1,'')
df['B'] = df['B'].where(~s2,'')
df
Out[1]: 
   A          B  C     D
0  1  10/1/2019  I  2879
1                O   196
2     10/2/2019  I  2840
3                O   189
4  2  10/1/2019  I  2907
5                O   195
Sign up to request clarification or add additional context in comments.

1 Comment

@codeninja2c4u no problem also kindly feel free to upvot if helpful. Thanks for accepting!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.