如何重命名python pandas数据框中的列?

时间:2016-12-26 17:18:53

标签: python pandas pivot

我是大熊猫的新手,想知道是否有人可以帮助我。

  • 我写了下面的代码。
  • 将json文件的内容粘贴到脚本下方。
  • 在json内容下面我们可以看到脚本输出。

我只想将输出数据框中每列的名称设置为国家/地区对象的名称(例如,德国或法国)

而不是获得此输出

                  value    name        value     name
tag                                                  
capital           Paris  France       Berlin  Germany
population  34111000000  France  11233000000  Germany
language         French  France       German  Germany

...我想要这样的东西

                  France             Germany     
tag                                                  
capital           Paris         Berlin  Germany
population  34111000000    11233000000  Germany
language         French         German  Germany

任何帮助将不胜感激: - )

这是我的代码......

import numpy as np
import pandas as pd
import json

class Country(object):
    def __init__(self,name):
        self.name = name
        self.json = name + "_Data.json"

def ImportJson(x):
    ImportedJson = []
    for country in x:
        with open(country.json) as country_json_file:
            country_data = json.load(country_json_file)
            country_data_table = pd.DataFrame(country_data['data'], columns=['tag', 'value']).set_index('tag')
            country_data_table['name'] = country.name
        ImportedJson.append(country_data_table)
    return ImportedJson

France = Country("France")
Germany = Country("Germany")
All_Countries = [France,Germany]

OpenedJson = ImportJson(All_Countries)

Country_Data = pd.concat(OpenedJson,axis=1)
print Country_Data

这里是json文件

Germany_Data.json

{
    "data": [
        {
            "tag": "capital",
            "value": "Berlin"
        },
        {
            "tag": "population",
            "value": 11233000000
        },
        {
            "tag": "language",
            "value": "German"
        }
    ],
    "result_count": 33,
    "page_size": 5000,
    "current_page": 1,
    "total_pages": 1,
    "api_call_credits": 1
}

France_Data.json

{
    "data": [
        {
            "tag": "capital",
            "value": "Paris"
        },
        {
            "tag": "population",
            "value": 34111000000
        },
        {
            "tag": "language",
            "value": "French"
        }
    ],
    "result_count": 33,
    "page_size": 5000,
    "current_page": 1,
    "total_pages": 1,
    "api_call_credits": 1
}

脚本输出

                  value    name        value     name
tag                                                  
capital           Paris  France       Berlin  Germany
population  34111000000  France  11233000000  Germany
language         French  France       German  Germany

2 个答案:

答案 0 :(得分:2)

在您的函数ImportJson中,您有以下两行代码。

country_data_table = pd.DataFrame(country_data['data'], columns=['tag', 'value']).set_index('tag')
country_data_table['name'] = country.name

删除第二行并在其后直接添加

country_data_table.rename(columns={'value':country.name}, inplace=True)

答案 1 :(得分:2)

我重写了您的class

import numpy as np
import pandas as pd
import json

class Country(object):
    def __init__(self,name):
        self.name = name
        self.json = name + "_Data.json"
        with open(self.json, 'r') as fp:
            self.data = json.load(fp)['data']
        self.series = pd.DataFrame.from_records(
            self.data
        ).set_index('tag').value.rename(self.name)

France = Country("France")
Germany = Country("Germany")


pd.concat([c.series for c in [France, Germany]], axis=1)

                 France      Germany
tag                                 
capital           Paris       Berlin
population  34111000000  11233000000
language         French       German

如果您坚持操纵构建的数据框

# take transpose so I can groupby index and add a count column
# for each `name` and `value`.  Once I have a unique index, I can
# do more.
CD1 = Country_Data.T.set_index(
    Country_Data.T.groupby(level=0).cumcount(), append=True).T

# strategy is to filter `value` columns and reassign the columns
CD2 = CD1.filter(like='value')
CD2.columns = Country_Data.loc['capital', 'name'].tolist()

CD2

                 France      Germany
tag                                 
capital           Paris       Berlin
population  34111000000  11233000000
language         French       German

设置json个文件

import json

with open('Germany_Data.json', 'w') as fp:
    json.dump(
        {
            "data": [
                {
                    "tag": "capital",
                    "value": "Berlin"
                },
                {
                    "tag": "population",
                    "value": 11233000000
                },
                {
                    "tag": "language",
                    "value": "German"
                }
            ],
            "result_count": 33,
            "page_size": 5000,
            "current_page": 1,
            "total_pages": 1,
            "api_call_credits": 1
        }
        , fp)

with open('France_Data.json', 'w') as fp:
    json.dump(
        {
            "data": [
                {
                    "tag": "capital",
                    "value": "Paris"
                },
                {
                    "tag": "population",
                    "value": 34111000000
                },
                {
                    "tag": "language",
                    "value": "French"
                }
            ],
            "result_count": 33,
            "page_size": 5000,
            "current_page": 1,
            "total_pages": 1,
            "api_call_credits": 1
        }
        , fp)