0

My input data frame df is below

external_id

sw_1
sw_2
sw_3
Sw_55

and my output data frame output_df should be

external_id : Status

sw_1 :Hello Sw_1
sw_2 :Hello sw_2
sw_3 :hello sw_3
Sw_55 :Hello sw_55

Till now I have done this. Able to create new df for for loop output only. I want to store for loop result in existing data frame.

df = pd.read_csv(r'/Users/man/Desktop/input.csv')

output_df = df.copy()
output_df['Status'] = ""

list2 = []

for i in df['external_id']:
    x =  "Hello  " + i
    list2.append(x)

df1 = pd.DataFrame(list2)
print(df1)

Actually I have input data frame which contains external_id and I want to call API for each external_id and then store result of api call (status_code and API response) in existing data frame against of each external_id.

Rabinzel
  • 7,757
  • 3
  • 10
  • 30
  • Does this answer your question? [add a string prefix to each value in a string column using Pandas](https://stackoverflow.com/questions/20025882/add-a-string-prefix-to-each-value-in-a-string-column-using-pandas) – Rabinzel Nov 17 '22 at 08:23
  • No @Rabinzel. Actually I have input data frame which contains external_id and I want to call API for each external_id and then store result of api call (status_code and API response) in existing data frame against of each external_id. – Sudarshan Waman Nov 17 '22 at 08:33
  • I guess, your example isn't well chosen. It looks like you just want to add the string `Hello` to each `example_id`. So instead of `Hello` you have a diffrent status value for each `external_id`? – Rabinzel Nov 17 '22 at 08:42
  • @Rabinzel my requirement is :- Whatever the result of for loop that should be in next column ("Status")of input df("external_id"). Thanks. – Sudarshan Waman Nov 17 '22 at 09:13

1 Answers1

0

It is really still not clear to me what you want to achieve or how you get results of your API, since it just doesn't appear in your code example, but maybe this helps. Your Input is called df. In your code you make a copy and name it output_df but then you never use it. The new created list will be assigned to df1 but you said you want to have a new column in your df. Input:

  external_id
0        sw_1
1        sw_2
2        sw_3
3       Sw_55
result = []
for value in df['external_id']:
    some_value = 'Hello' # here your API should return something for each of your id's
    result.append(some_value + ': ' + value) # be aware that `value` and `some_value`need to be strings, maybe convert them first, then append.

df['Status'] = result
print(df)

Output:

  external_id        Status
0        sw_1   Hello: sw_1
1        sw_2   Hello: sw_2
2        sw_3   Hello: sw_3
3       Sw_55  Hello: Sw_55
Rabinzel
  • 7,757
  • 3
  • 10
  • 30
  • Thanks a lot @Rabinzel. It worked for me. But what if I have more than 1 results of for loop. like some_value and some_value1. some_value = API response. some_value1 = API status code. Please help. – Sudarshan Waman Nov 17 '22 at 09:43
  • Final dataframe should be like this. external_id status_code Response sw_1 200 {'status': 'ok', 'payload': {'user_id':abcd}} – Sudarshan Waman Nov 17 '22 at 09:49
  • As I told you before, this is very vague, please update your question with a specific example and its desired output with decent format. For the first answer I already guessed a lot what your goal could be, that's not how this should work. I'd like to help but I need better explanation. – Rabinzel Nov 17 '22 at 11:04