简体   繁体   English

无法从数据框熊猫中提取列

[英]Unable to extract columns form the data frame pandas

I'm reading the json file and extracting the required columns into the csv file .Please find the below code and let me know if am wrong 我正在读取json文件并将所需的列提取到csv文件中。请找到以下代码,如果有误请通知我

df=pd.read_json(json_file)
df=df['Person']
data1=pd.DataFrame(df,columns=['cols1','cols2','cols3','title'])

print data1

df.to_csv("ptr1.csv",index = False)

but i'm getting the error empty dataframe output . 但是我得到错误的空数据帧输出。

It seems you need: 看来您需要:

import json
from pandas.io.json import json_normalize

with open('file.json') as data_file:    
    data = json.load(data_file) 

df = json_normalize(data, 'Person')

Sample: 样品:

data = {"Person": [{ "cols1" : 1, "cols2" : "value2", "cols3" : "value1", "title" : "ptr" }]}

df = json_normalize(data, 'Person')
print (df)
   cols1   cols2   cols3 title
0      1  value2  value1   ptr

Still i'm getting the empty dataframe .Please find the below code 我仍然得到空的数据框。请找到以下代码

json_file = open("file.json")

df=pd.read_json(json_file)

data=json_normalize(json_file, 'Person')
print data

Getting output : Empty DataFrame Columns: [] Index: [] 获取输出:空的DataFrame列:[]索引:[]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM