0

I have the following dataframe:

  symbol           PSAR
0   AAPL  [nan,100,200]
1   PYPL  [nan,300,400]
2    SPY  [nan,500,600]

I am trying to turn the PSAR list values into rows like the following:

symbol   PSAR

AAPL     nan
AAPL     100
AAPL     200
PYPL     nan
PYPL     300
...      ...
SPY      600

I have been trying to solve it by following the answers in this post(one key difference being that that post has a list of list) but cant get there. How to convert column with list of values into rows in Pandas DataFrame.

df['PSAR'].stack().reset_index(level=1, drop=True).to_frame('PSAR')
.join(df[['symbol']], how='left')
8
  • 3
    df[['symbol']].join(pd.DataFrame(df.PSAR.tolist())).melt(id_vars='symbol').drop('variable', 1) Commented Nov 1, 2018 at 16:28
  • 2
    @user3483203 unnesting again :-(, I will open a topic in git , may the developer can help us build that sweet function . Commented Nov 1, 2018 at 16:28
  • @user3483203 not sure what I am doing wrong, but I literally get the original df back when I run your code. Commented Nov 1, 2018 at 16:34
  • Do you remember to assign that result to a new variable? Commented Nov 1, 2018 at 16:34
  • 1
    repl.it/repls/QuintessentialGivingWatchdog Try it out here, should be working Commented Nov 1, 2018 at 16:39

2 Answers 2

1

Not a slick one but this does the job:

list_of_lists = []
df_as_dict = dict(df.values)
for key,values in df_as_dict.items():
    list_of_lists+=[[key,value] for value in values]
pd.DataFrame(list_of_lists)

returns:

    0      1
0   AAPL    NaN
1   AAPL    100.0
2   AAPL    200.0
3   PYPL    NaN
4   PYPL    300.0
5   PYPL    400.0
6   SPY     NaN
7   SPY    500.0
8   SPY    600.0
Sign up to request clarification or add additional context in comments.

Comments

1

Pandas >= 0.25:

df1 = pd.DataFrame({'symbol':['AAPL', 'PYPL', 'SPY'],
               'PSAR':[[None,100,200], [None,300,400], [None,500,600]]})
print(df1)

symbol  PSAR
0   AAPL    [None, 100, 200]
1   PYPL    [None, 300, 400]
2   SPY [None, 500, 600]

df1.explode('PSAR')

    symbol  PSAR
0   AAPL    None
0   AAPL    100
0   AAPL    200
1   PYPL    None
1   PYPL    300
1   PYPL    400
2   SPY     None
2   SPY     500
2   SPY     600

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.