Home > Mobile >  How to concatenate lists into single merged DataFrame from for loop output?
How to concatenate lists into single merged DataFrame from for loop output?

Time:12-04

I'm tring to pull data by using API, I have a list of IDs from csv,and I use for loop to pull request for each ID, the output is in the form of lists, and I tried to convert them into DataFrame, they come out into seperate DataFrames and I'm not able to merge them into one since they are inside of a for loop.

The code looks like this:

==================

``
# Read ios id from CSV file
data = pd.read_csv('File.csv')
ios = (data['ios_id'])
ios_data= []

# Convert ios id into a list
for i in ios:
    ios_data.append(i)

for id in ios_data:
        params = {
                "os": "ios",
                "app_id": id,
                "country": "US",
                "search_term": "kid",
                "auth_token": AUTH_TOKEN
        }

        response = requests.get(BASE_URL, params)
        # print(response.status_code)
        raw = response.json()
        feedback = raw['feedback']
        if feedback != []:
                feedback_dict = feedback[0]
                df = pd.DataFrame(feedback_dict)
                print(df)
        else:
                pass
``

And Output looks like this:

                                             content version  ... country     tags
0  So I love tiles of hop it’s fun but I don’t th...   4.4.0  ...      US   Family
1  So I love tiles of hop it’s fun but I don’t th...   4.4.0  ...      US  Love it

[2 rows x 9 columns]
                                             content  ...           tags
0  This game is, well, fantastic and I love how B...  ...            Ads
1  This game is, well, fantastic and I love how B...  ...         Family
2  This game is, well, fantastic and I love how B...  ...        Hate it
3  This game is, well, fantastic and I love how B...  ...  Inappropriate
4  This game is, well, fantastic and I love how B...  ...        Love it
5  This game is, well, fantastic and I love how B...  ...  Strenuousness

[6 rows x 9 columns]

CodePudding user response:

You can start by making an empty list/basket, then put in the dataframes collected in every iteration/pull and finally use pandas.concat to make a whole and single dataframe right after the loop.

Try this :

# Read ios id from CSV file
data = pd.read_csv('File.csv')

ios_data= data['ios_id'].tolist()

list_dfs = []

for id in ios_data:
        params = {
                "os": "ios",
                "app_id": id,
                "country": "US",
                "search_term": "kid",
                "auth_token": AUTH_TOKEN
        }

        response = requests.get(BASE_URL, params)
        # print(response.status_code)
        raw = response.json()
        feedback = raw['feedback']
        if feedback != []:
                feedback_dict = feedback[0]
                df = pd.DataFrame(feedback_dict)
                list_dfs.append(df)
        else:
                pass

df_all= pd.concat(list_dfs, ignore_index=True)

# Output :

print(df_all)
                                             content  ...           tags
0  So I love tiles of hop it’s fun but I don’t th...  ...         Family
1  So I love tiles of hop it’s fun but I don’t th...  ...        Love it
2  This game is, well, fantastic and I love how B...  ...            Ads
3  This game is, well, fantastic and I love how B...  ...         Family
4  This game is, well, fantastic and I love how B...  ...        Hate it
5  This game is, well, fantastic and I love how B...  ...  Inappropriate
6  This game is, well, fantastic and I love how B...  ...        Love it
7  This game is, well, fantastic and I love how B...  ...  Strenuousness
  • Related