3

I have this kind of dataframe :

    Variable   Date     Value
0  Variable1  Date1  Valeur 1
1  Variable1  Date2  Valeur 2
2  Variable1  Date3  Valeur 3
3  Variable2  Date4  Valeur 4
4  Variable2  Date5  Valeur 5

I would like to transform it like this :

    Date Variable1 Variable2
0  Date1  Valeur 1      None
1  Date2  Valeur 2      None
2  Date3  Valeur 3      None
3  Date4      None  Valeur 4
4  Date5      None  Valeur 5

How can I do this kind of tranformation in Python with panda or numpy? Thanks for your help

2
  • What are the conversion rules? Commented Apr 26, 2016 at 14:46
  • If values < 3 it's going inside Variable 1 else it's going into variable 2 Commented Apr 26, 2016 at 14:50

2 Answers 2

4

I think you need pivot with rename_axis (new in pandas 0.18.0) and reset_index:

print df.pivot(index='Date', columns='Variable', values='Value')
        .rename_axis(None, axis=1)
        .reset_index()

    Date Variable1 Variable2
0  Date1  Valeur 1      None
1  Date2  Valeur 2      None
2  Date3  Valeur 3      None
3  Date4      None  Valeur 4
4  Date5      None  Valeur 5

Sample:

import pandas as pd

df = pd.DataFrame({'Variable': {0: 'a', 1: 'a', 2: 'a', 3: 'b', 4: 'b'}, 
                    'Date': {0: pd.Timestamp('2016-02-05 00:00:00'), 
                             1: pd.Timestamp('2016-02-06 00:00:00'),
                             2: pd.Timestamp('2016-02-07 00:00:00'), 
                             3: pd.Timestamp('2016-02-08 00:00:00'), 
                             4: pd.Timestamp('2016-02-09 00:00:00')}, 
                    'Value': {0: 1, 1: 2, 2: 3, 3: 4, 4: 5}},
                    columns=['Variable','Date','Value'])

print df
  Variable       Date  Value
0        a 2016-02-05      1
1        a 2016-02-06      2
2        a 2016-02-07      3
3        b 2016-02-08      4
4        b 2016-02-09      5

print df.pivot(index='Date', columns='Variable', values='Value')
        .rename_axis(None, axis=1)
        .reset_index()

        Date    a    b
0 2016-02-05  1.0  NaN
1 2016-02-06  2.0  NaN
2 2016-02-07  3.0  NaN
3 2016-02-08  NaN  4.0
4 2016-02-09  NaN  5.0
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks a lot jezrael. I think it's what I needed!
Super, first I think you need some special conversion rule ;)
Alright like what? can you give me an exemple please?
Thanks al lot jezrael it's exactly what i needed!
0

As a complement, a way to split a column on condition :

df=pd.DataFrame({'Variable':arange(5)},index=pd.date_range('2016/4/26',periods=5))
"""
            Variable
2016-04-26         0
2016-04-27         1
2016-04-28         2
2016-04-29         3
2016-04-30         4
"""

cond=df<3
df[cond].join(df[~cond],lsuffix=1,rsuffix=2)    
"""
            Variable1  Variable2
2016-04-26        0.0        NaN
2016-04-27        1.0        NaN
2016-04-28        2.0        NaN
2016-04-29        NaN        3.0
2016-04-30        NaN        4.0
"""

Comments

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.