Измените конкретные значения столбцов данных, что наиболее эффективно?

Я не думаю, что вам нужна новая лицензия.

По крайней мере, в моем случае у меня есть лицензия, позволяющая загружать и устанавливать на любой ОС (Windows, Linux или Mac).

И пусть у меня будет до 4 одновременных установок , Таким образом, у меня есть один запуск на компьютере с Windows и один на Mac.

0
задан 13 August 2018 в 14:52

3 ответа

Вы можете использовать метод Pandas replace():

energy
                                             Country
0                           United States of America
1                                  Republic of Korea
2  United Kingdom of Great Britain and Northern I...
3     China, Hong Kong Special Administrative Region

energy.replace(rep_map)
          Country
0   United States
1     South Korea
2  United Kingdom
3       Hong Kong

Обратите внимание, что replace() заменит все экземпляры этих строк на кадре данных.

Данные: [ ! d3]

countries = ["United States of America", 
             "Republic of Korea", 
             "United Kingdom of Great Britain and Northern Ireland", 
             "China, Hong Kong Special Administrative Region"]
replacements = ["United States", "South Korea", "United Kingdom", "Hong Kong"]
rep_map = {k:v for k, v in zip(countries, replacements)}
energy = pd.DataFrame({"Country": countries})
0
ответ дан 15 August 2018 в 17:02

Вы можете использовать np.where

energy['Country'] = np.where(energy['Country'] == 'United States of America', 'United States', energy['Country'] )
energy['Country'] = np.where(energy['Country'] == 'Republic of Korea', 'Korea', energy['Country'])

Или:

energy['Country'][energy['Country'] == 'United States of America'] = 'United States'
energy['Country'][energy['Country'] == 'Republic of Korea'] = 'Korea'

df:

                    Country
0  United States of America
1                     Spain
2         Republic of Korea
3                    France

Выход:

         Country
0  United States
1          Spain
2          Korea
3         France
0
ответ дан 15 August 2018 в 17:02

Вы можете объявить словарь с отображением, а затем использовать map

Пример:

import pandas as pd

mapVal = {'United States of America': 'United States', 'Republic of Korea': 'South Korea', 'United Kingdom of Great Britain and Northern Ireland': 'United Kingdom', 'China': 'Hong Kong', 'Hong Kong Special Administrative Region': 'Hong Kong'}    #Sample Mapping

df = pd.DataFrame({'Country': ['United States of America', 'Republic of Korea', 'United Kingdom of Great Britain and Northern Ireland', 'China', 'Hong Kong Special Administrative Region']})
df["newVal"] = df["Country"].map(mapVal)          #df["Country"] = df["Country"].map(mapVal)
print(df)

Пример:

                                             Country          newVal
0                           United States of America   United States
1                                  Republic of Korea     South Korea
2  United Kingdom of Great Britain and Northern I...  United Kingdom
3                                              China       Hong Kong
4            Hong Kong Special Administrative Region       Hong Kong
0
ответ дан 15 August 2018 в 17:02

Другие вопросы по тегам:

Похожие вопросы: