【问题标题】:Python Pandas Problem Adding Rows with for loopPython Pandas 使用 for 循环添加行的问题
【发布时间】:2020-07-07 08:40:36
【问题描述】:

我想将列添加到具有唯一名称的数据框中。我尝试使用 for 循环自动执行此操作。我在名称后添加了一个索引,但只显示了 i 的最后一个值。我对python比较陌生,需要帮助!谢谢!

我的代码:

from urllib.request import urlopen
import json
import pandas as pd
import sklearn
from matplotlib import pyplot as plt
from sklearn import linear_model
from sklearn.utils import shuffle
import numpy as np
import csv
import time


key = ''

product = 'ENCHANTED_RAW_CHICKEN'
url = 'https://api.hypixel.net/skyblock/bazaar/product?key=' + key + '&productId=' + product
url2 = 'https://api.hypixel.net/gameCounts?key=' + key

for i in range(3):
    json_obj = urlopen(url)
    json_obj2 = urlopen(url2)
    all_data = json.load(json_obj)
    gameCounts = json.load(json_obj2)

    product_info = all_data['product_info']
    week_historic = product_info['week_historic']
    games = gameCounts['games']
    skyblock = (games['SKYBLOCK'])
    skyblock_players = skyblock['players']

    with open('DATA_' + str(product) + '.json', 'w') as outfile:
        json.dump(week_historic, outfile)

    df = pd.read_json (r'DATA_' + product + '.json')
    df.to_csv (r'CSV' + product + '.csv', index = None)

    df = pd.read_csv(r'CSV' + product + '.csv', encoding='utf-8')
    df = df.drop('productId', 1)
    player_count = 'playercount' + str(i)
    df[player_count]= df.shape[0] * [skyblock_players]
    #time.sleep(10)
print(df)
df.to_csv (r'CSV' + product + '.csv', index = None)

输出:

timestamp  nowBuyVolume  ...  sells  playercount2
0    2020-03-19 16:46:45.517         45806  ...    248         33202
1    2020-03-19 17:16:45.517         45576  ...    176         33202
2    2020-03-19 17:46:45.517         47132  ...    121         33202
3    2020-03-19 17:59:31.600         38251  ...      0         33202
4    2020-03-19 18:29:31.579         33466  ...     31         33202
..                       ...           ...  ...    ...           ...
335  2020-03-26 14:55:49.357        159465  ...    168         33202
336  2020-03-26 15:25:49.357        157919  ...     92         33202
337  2020-03-26 15:55:49.357        133332  ...     79         33202
338  2020-03-26 16:25:49.357        130275  ...    139         33202
339  2020-03-26 16:28:29.379        130146  ...     16         33202

我希望输出如何:

timestamp  nowBuyVolume  ...  sells  playercount0  playercount1  playercount2
0    2020-03-19 16:46:45.517         45806  ...    248         33200         33201         33202

我的 csv:

timestamp,nowBuyVolume,nowSellVolume,buyCoins,buyVolume,buys,sellCoins,sellVolume,sells,playercount2
2020-03-19 17:46:45.517,47132,105049,3629664.7,3978,96,3727318.6000000034,3767,121,34537

【问题讨论】:

  • 我猜你的 csv 中有几个分隔符。您可以将df.to_csv (r'CSV' + product + '.csv', sep='\t\s') 中的分隔符设置为正则表达式。用各种分隔符替换 sep 的值。
  • 我只有 ',' 作为分隔符。问题是,只有 playercount2 而不是 playercount0、playercount1、playercount2。此外,分隔符必须是字符串。谢谢你
  • 我明白这一点。我的问题是,例如,第一行的“248”和“33202”之间有什么?它们之间的值应转换为“,”,或视为分隔符并包含在 sep 的值中。您可以在之后管理列名。
  • 我在下面添加了csv文件
  • 那是哪个 csv?输入还是输出?

标签: python pandas for-loop indexing


【解决方案1】:

首先,您的错误是您在循环中实例化了 DataFrame 的第一部分。

我想 DataFrame 的第一部分在url 中,第二部分在url2

首先你应该这样做:

json_obj = urlopen(url)
all_data = json.load(json_obj)
product_info = all_data['product_info']
week_historic = product_info['week_historic']
with open('DATA_' + str(product) + '.json', 'w') as outfile:
        json.dump(week_historic, outfile)

df = pd.read_json (r'DATA_' + product + '.json')
df.to_csv (r'CSV' + product + '.csv', index = None)

df = pd.read_csv(r'CSV' + product + '.csv', encoding='utf-8')

json_obj2 = urlopen(url2)
gameCounts = json.load(json_obj2)
games = gameCounts['games']
skyblock = (games['SKYBLOCK'])
skyblock_players = skyblock['players']

N = 3
for i in range(N):
    player_count = 'playercount' + str(i)
    df[player_count]= # search in skyblock_players or wherever you want the data for each column

【讨论】:

  • 我希望根据我的迭代次数自动添加 playercount0 到 playercount3
  • 更新了答案。
  • 是的,这条线不需要,但没关系。我基本上使用 API 并希望将最新数据附加到 csv 作为 playercount x + 1,因此我可以事后分析 csv。我希望程序每 30 分钟存储一次变量 skyblock_players 并将其添加到 csv 中。这就是 csv 的样子:
  • 常数不会影响我的问题
  • 是的,确实如此,您提到自己想要根据迭代次数自动添加列。如果将其设置为 3,它将不是动态的。您还应该提到您从哪里获取每次迭代中的信息。我想在 url2 中?
猜你喜欢
  • 2021-06-20
  • 2018-12-15
  • 1970-01-01
  • 2011-01-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多