[英]how to write data in the sequential format in the dataframe
import json
import pandas as pd
import numpy as np
class Decoder(json.JSONDecoder):
def decode(self, s):
result = super(Decoder, self).decode(s)
return self._decode(result)
def _decode(self, o):
if isinstance(o, str):
try:
return int(o)
except ValueError:
try:
return float(o)
except ValueError:
return o
elif isinstance(o, dict):
return {k: self._decode(v) for k, v in o.items()}
elif isinstance(o, list):
return [self._decode(v) for v in o]
else:
return o
with open('ouput_data.json') as f:
data = json.load(f, cls=Decoder)
list = []
list = (data['data'])
df = pd.DataFrame(list, columns=['id', 'value'])
new_df = df.set_index('id')
new_df1 = new_df.reindex(np.arange(1, 14)).fillna(' ')
print(new_df1)
给出的输出如下,
id value
1 whats up
2 what
3
4
5 HI
6
7
8
9
10
11
12
13
但我想要按顺序输出,我如何编辑代码以实现此输出
id 1 2 3 4 5 6 7 8 9 10 11 12 13
value whatsup what Hi
让我知道我必须更改现有代码的地方!!!! 请!!!!!
提前致谢
你的三行:
df = pd.DataFrame(list,columns=['id','value'])
new_df = df.set_index('id')
new_df1=new_df.reindex(np.arange(1, 14)).fillna(' ')
可以改写为:
df = (pd.DataFrame(list, columns = ['id','value'])
.set_index('id')
.reindex(np.arange(1, 14)).fillna(' ')
.T
)
而df
就是你所追求的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.