gpt4 book ai didi

python-3.x - Pandas append 返回带有 NaN 值的 DF

转载 作者:行者123 更新时间:2023-12-04 15:46:47 24 4
gpt4 key购买 nike

我正在将列表中的数据附加到 pandas df。我的条目中不断出现 NaN。

根据我所阅读的内容,我认为我可能必须在我的代码中提及每一列的数据类型。

dumps = [];features_df = pd.DataFrame()
for i in range (int(len(ids)/50)):
dumps = sp.audio_features(ids[i*50:50*(i+1)])
for i in range (len(dumps)):
print(list(dumps[0].values()))
features_df = features_df.append(list(dumps[0].values()), ignore_index = True)

预期的结果,比如-
[0.833, 0.539, 11, -7.399, 0, 0.178, 0.163, 2.1e-06, 0.101, 0.385, 99.947, 'audio_features', '6MWtB6iiXyIwun0YzU6DFP', 'spotify:track:6MWtB6iiXyIwun0YzU6DFP', 7 '6MWtB6iiXyIwun0YzU6Yz'6 ', ' https://api.spotify.com/v1/tracks/6MWtB6iiXyIwun0YzU6DFP ', 149520, 4]一排。实际-
舞蹈能力能量 ... duration_ms time_signature
0 NaN NaN ... NaN NaN
1 NaN NaN ... NaN NaN
2 NaN NaN ... NaN NaN
3 NaN NaN ... NaN NaN
4 NaN NaN ... NaN NaN
5 NaN NaN ... NaN NaN

对于所有行

最佳答案

紧密循环中的

append() 策略不是执行此操作的好方法。相反,您可以构造一个空的 DataFrame,然后使用 loc 指定插入点。应使用 DataFrame 索引。

例如:

import pandas as pd

df = pd.DataFrame(data=[], columns=['n'])
for i in range(100):
df.loc[i] = i
print(df)
time python3 append_df.py 
n
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9

real 0m13.178s
user 0m12.287s
sys 0m0.617s

https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.append.html

Iteratively appending rows to a DataFrame can be more computationally intensive than a single concatenate. A better solution is to append those rows to a list and then concatenate the list with the original DataFrame all at once.

关于python-3.x - Pandas append 返回带有 NaN 值的 DF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55426878/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com