0

I have a use case where I need to fill a new pandas column with the contents of a specific cell in the same table. There are 60 countries in Europe, so I need to fill a shared currency column with the content's of one country's currency (as an example only)

I need an SQL "Where" clause for Pandas - that:

1. Searches the dataframe rows for the single occurrence of "Britain" in column "country"
2. Returns a single, unique value "pound" from df['currency'].
3. Creates a new column filled with just this value = string "pound"


w['Euro_currency'] = w['Euro_currency'].map(w.loc["country"]=="Britain"["currency"])

# [Britain][currency] - contains the value - "Pound"

When this works correctly, every row in the new column 'Euro_currency' contains the value "pound"

Edward
  • 179
  • 2
  • 12
  • 1
    `w['Euro_currency'] = w.loc["Britain", "currency"]` – cs95 Jul 02 '20 at 06:27
  • Thanks @cs95. I restated the question because I didn't make it clear that "Britain" was a value in one row of a "country" column. – Edward Jul 02 '20 at 12:40

2 Answers2

0

How about you take the value from that cell and just create a new column with it as below:

p = w.loc["Britain"]["currency"]
w['Euro_currency'] = p

Does this work for you?

lytseeker
  • 305
  • 4
  • 10
  • Thanks @lytseeker ... I restated the question to clarify that "Britain" needs to be searched and found in a df['"country"] column first – Edward Jul 02 '20 at 12:42
0

Thanks for help. I found this answer by @anton-protopopov at extract column value based on another column pandas dataframe

currency_value = df.loc[df['country'] == 'Britain', 'currency'].iloc[0]
df['currency_britain'] = currency_value

@anderson-zhu also mentioned that .item() would work as well

currency_value = df.loc[df['country'] == 'Britain', 'currency'].item()
Edward
  • 179
  • 2
  • 12