大佬教程收集整理的这篇文章主要介绍了Python Pandas 附加所有行的列,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个 python 脚本,旨在将嵌套的 Json 转换为 csv.. 到目前为止,我认为我的脚本运行良好,但我发现了一个我无法解决的大错误.. 我附上了代码:
def generate_csv(filename,keys,desc,GMT,day):
global dire
#Open Json
with open(filename + ".Json") as f_input:
data = Json.load(f_input)
filename = filename + ".csv"
picker = CherryPicker(data)
pos = 0
not_found = []
for colum in keys:
try:
flat = picker[colum].flatten().get()
df = pd.DataFrame(flat)
df.columns = ['timestamp',colum] #Rename
print(df) #I will attach how the data looks like..
if(pos == 0):
df.timestamp = pd.to_datetiR_519_11845@e(df.timestamp,unit='ms',utc=GMT) #Unix time to timestamp
fin = df
#Add ID
fin['ID']=desc
#Reorder ts - ID - data
new_order = ['timestamp','ID',colum]
fin = fin.reindex(new_order,axis=1)
pos = 1
else:
del df['timestamp'] #Remove timestamp because it is repeated
fin[colum] = df
except:
not_found.append(colum)
if(pos != 0):
fin.to_csv(filename,enCoding='utf-8',index=falsE)
else:
#Error message
d = {'Sin datos': ['','']}
fin = pd.DataFrame(d)
fin.to_csv(filename,index=falsE)
我所做的小总结: 我打开 Json 作为阅读。 我使用 CherryPicker 库来选择一个子 Json 并将其添加到最终数据帧中。 我添加了设备的 ID,避免重复时间戳。
这是在cherrypicker处理和名称更改后数据框的外观。
timestamp dias
0 1615690800248 23
timestamp maximoH_2
0 1615690800000 71.52
[118 rows x 2 columns]
timestamp minimoH_2
0 1615690800000 28.23
timestamp pesoObjetivo
0 1615690800000 644.5
timestamp deltaSilo
0 1615741040148 0.0
1 1615740435876 42.8
2 1615739227568 0.0
3 1615738623379 0.0
4 1615738019633 0.0
.. ... ...
113 1615658408107 0.0
114 1615657802881 0.0
115 1615657198866 0.0
116 1615656594517 0.0
117 1615655990194 0.0
[118 rows x 2 columns]
timestamp Deltavolumen
0 1615741040148 7.809999999997672
1 1615740435876 0
2 1615739831618 6
3 1615739227568 12.75
4 1615738623379 14.160000000032596
.. ... ...
138 1615657802881 15.190000000002328
139 1615657198866 6.470000000030268
140 1615656594517 16.429999999993015
141 1615655990194 18.05999999999767
142 1615655385756 12.910000000032596
[143 rows x 2 columns]
timestamp tabla
0 1615690800000 1111
timestamp FreeHeap
0 1615741040148 220124
1 1615740435876 220124
2 1615739831618 219332
3 1615739227568 220124
4 1615738623379 220124
.. ... ...
138 1615657802881 220120
139 1615657198866 220120
140 1615656594517 220120
141 1615655990194 220124
142 1615655385756 219328
如您所见.. 数据框具有不同的行数.. 它们出现的顺序因 API 而异,这似乎很重要。我发现我处理的第一个 DataFrame 决定了我在 CSV 中获得的最终行数。 在对应于上面附加数据帧的执行中,CSV 只有一行,即使某些数据帧有 142 个或更多......
我怀疑解决方案在以下几行中......我尝试了合并和追加,但没有积极的结果:
else:
del df['timestamp'] #Remove timestamp because it is repeated
fin[colum] = df
有什么想法吗?我需要能够可视化所有行,没有那个数量的列在这些空间中应该有一个空值。 谢谢! 请原谅我的拼写和写作!我的英语水平不是最好的。
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)
以上是大佬教程为你收集整理的Python Pandas 附加所有行的列全部内容,希望文章能够帮你解决Python Pandas 附加所有行的列所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。