pandas 如何从for循环输出中将列表连接到单个合并的DataFrame中?

guykilcj  于 2022-12-10  发布在  其他
关注(0)|答案(1)|浏览(193)

我特灵使用API提取数据,我有一个来自csv的ID列表,我使用for循环提取每个ID的请求,输出是列表的形式,我尝试将它们转换为DataFrame,它们变成单独的DataFrame,我无法将它们合并为一个,因为它们在for循环内。

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
``

输出如下所示:

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]
vc9ivgsu

vc9ivgsu1#

您可以先创建一个空列表/篮子,然后放入每次迭代/拉取中收集的 Dataframe ,最后在循环之后使用pandas.concat创建一个完整的单个 Dataframe 。
试试这个:

# 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)
#输出:
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

相关问题