Python 更有效地将json转换为panda数据帧

Python 更有效地将json转换为panda数据帧,python,json,pandas,dataframe,nested,Python,Json,Pandas,Dataframe,Nested,我有一个简单的json文件,我必须将其转换为panda datarame,然后转换为csv。文件中的一些示例记录包括: { '11': ['A', 'fried', 'is', 'a', 'nice', 'companion', '.'], '2': ['Let', 'the', 'things', 'happen', '.'], '33': ['There', 'is', 'always', 'a', 'way', 'out', '.'],

我有一个简单的json文件,我必须将其转换为panda datarame,然后转换为csv。文件中的一些示例记录包括:

    {
      '11': ['A', 'fried', 'is', 'a', 'nice', 'companion', '.'],  
      '2':  ['Let', 'the', 'things', 'happen', '.'], 
      '33': ['There', 'is', 'always', 'a', 'way', 'out', '.'],
      '4':  ['The', 'birds', 'are', 'flying', '.'],
       ... more than 500,000 records
    }
结果数据帧:

    11,    A friend is a nice companion.
     2,    Let the things happen.            
    33,    There is always a way out.
     4,    The birds are flying.    
    ..... upto 500,000 records 
下面给出了转换它的代码,它的工作非常好:

import pandas as pd
import json

df = pd.read_json('my_file.json', orient = 'index')

df = df[df.columns[1:]].apply(lambda x:' '.join(x.dropna().astype(str)),axis=1)

#df = df.apply(lambda x: x.replace(',',' '))
print(df)

df.to_csv('outPutFile1.csv', encoding='utf-8')
我想知道有没有更有效的解决方案?因为熊猫将“,”视为分隔符,所以我必须将所有列合并为一列。是否可以直接将json转换为数据帧,而不将所有列合并为一列

我会感谢你的帮助。
谢谢

将json文件转换为csv文件格式的最快方法如下

# load json file to a dictionary
with open('my_file.json') as f:
    my_file_dictionary = json.load(f)    

# save dictionary keys and value(text separated by space) to a csv
with open('outPutFile1.csv', mode='w', encoding='utf-8') as fp:
    [fp.write('{0},{1}\n'.format(key, ' '.join(value))) for key, value in my_file_dictionary.items()]

为什么要先使用“
”,”.join()
,然后删除逗号。为什么不干脆
'.join()
?使用
'.join()
并删除第二个
应用
明白了,但是不使用怎么办。完全加入并使用其他东西?谢谢你的帮助。我得到一个csv文件与您的代码,但较长的文本分为许多列,而不是只有一列,因为它是与我的代码。我认为文本中有许多单引号、双引号、逗号等,这可能是原因,也可能是多个空格造成的。@codeDB,您可以通过选项卡(
\t
)来分隔csv文件,即
fp.write({0}\t{1}\n.form…
我这样做了,它并没有改变任何东西,而是将第一列和第二列合并在一起,这样第一列中的键就会合并为文本。