PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, May 12, 2022

[FIXED] How to add a list of lists to a df at an specific column on Python? Pandas related

 May 12, 2022     append, dataframe, list, pandas, python-3.x     No comments   

Issue

Suppose that I have the following df with empty strings in the Volatility expected column:

Index Time   Currency   Volatility expected Event                                 Actual    Forecast    Previous
0     02:00    GBP                          U.K. Construction Output (YoY) (Jan)  9.9%      9.2%        7.4%
1     02:00    GBP                          Construction Output (MoM) (Jan)       1.1%      0.5%        2.0%
2     02:00    GBP                          GDP (MoM)                             0.8%      0.2%       -0.2%
3     02:00    GBP                          GDP (YoY)                             10.0%     9.3%        6.0%
                                                                                                 

And the following list of lists called volatility_list:

 volatility_list = [
                   ['Low Volatility Expected'],
                   ['Low Volatility Expected'],
                   ['High Volatility Expected'],
                   ['High Volatility Expected'],
                   ]

How could I add volatility_list values to Volatility expected column from df so it ends up like this?

Index Time   Currency   Volatility expected      Event                                Actual Forecast  Previous
0     02:00    GBP      Low Volatility Expected  U.K. Construction Output (YoY) (Jan)  9.9%     9.2%     7.4%
1     02:00    GBP      Low Volatility Expected  Construction Output (MoM) (Jan)       1.1%     0.5%     2.0%
2     02:00    GBP      High Volatility Expected GDP (MoM)                             0.8%     0.2%    -0.2%
3     02:00    GBP      High Volatility Expected GDP (YoY)                             10.0%    9.3%     6.0%

Solution

You can use a comprehension to extract the first and only element of each item in your list:

df['Volatility expected'] = [v[0] for v in volatility_list]
print(df)

# Output
    Time Currency       Volatility expected                                 Event Actual Forecast Previous
0  02:00      GBP   Low Volatility Expected  U.K. Construction Output (YoY) (Jan)   9.9%     9.2%     7.4%
1  02:00      GBP   Low Volatility Expected       Construction Output (MoM) (Jan)   1.1%     0.5%     2.0%
2  02:00      GBP  High Volatility Expected                             GDP (MoM)   0.8%     0.2%    -0.2%
3  02:00      GBP  High Volatility Expected                             GDP (YoY)  10.0%     9.3%     6.0%


Answered By - Corralien
Answer Checked By - David Goodson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing