簡體   English   中英

如何重命名python pandas數據框中的列?

[英]How do I rename columns in a python pandas dataframe?

我是熊貓的新手,想知道是否有人可以幫助我。

  • 我寫了下面的代碼。
  • 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

在您的函數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)

我重寫了你的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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM