从dict创建python系列,分配索引的问题

2024-09-24 06:26:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在练习Python技能,希望创建一个系列。 我想使用字典,但当我想为该系列建立索引时,它会显示给我。 我现在不知道是什么原因。 初始字典具有键和值的字符串值,仅包含8个元素

country_capital_dict = {'Germany':'Berlin', 'US':'Washington',
                         'Italy':'Rome', 'France':'Paris',
                         'Russia':'Moscow','Spain':'Madrid',
                         'Austria':'Vienna','Greece':'Athens'}
country_capital_series = pd.Series(country_capital_dict, index = ['a','b','c','d','e','f','g','h'])
print(country_capital_series)

Tags: 字符串元素字典技能原因countrydictseries
2条回答

如果默认情况下创建了从键向Series传递字典的索引:

country_capital_dict = {'Germany':'Berlin', 'US':'Washington',
                         'Italy':'Rome', 'France':'Paris',
                         'Russia':'Moscow','Spain':'Madrid',
                         'Austria':'Vienna','Greece':'Athens'}
country_capital_series = pd.Series(country_capital_dict)
print(country_capital_series)
Germany        Berlin
US         Washington
Italy            Rome
France          Paris
Russia         Moscow
Spain          Madrid
Austria        Vienna
Greece         Athens
dtype: object

如果需要更改索引,您可以指定它:

country_capital_series.index = ['a','b','c','d','e','f','g','h']

print(country_capital_series)
a        Berlin
b    Washington
c          Rome
d         Paris
e        Moscow
f        Madrid
g        Vienna
h        Athens
dtype: object

或仅将字典的值传递给Series

country_capital_series = pd.Series(country_capital_dict.values(), 
                                   index = ['a','b','c','d','e','f','g','h'])
print(country_capital_series)
a        Berlin
b    Washington
c          Rome
d         Paris
e        Moscow
f        Madrid
g        Vienna
h        Athens
dtype: object

获取所有缺失值的原因是列表中的索引和字典键中的索引不匹配-因为不同的熊猫尝试用列表中的“新”更改原始索引,但不知道新值,所以分配了所有“N”:

country_capital_series = pd.Series(country_capital_dict, 
                                   index = ['a','b','c','d','e','f','g','h'])
print(country_capital_series)
a    NaN
b    NaN
c    NaN
d    NaN
e    NaN
f    NaN
g    NaN
h    NaN
dtype: object

如果仅为某些匹配的值分配了NAN(仅针对不匹配的值):

country_capital_series = pd.Series(country_capital_dict, 
                                   index = ['a','Germany','c','d','e','Austria','g','h'])
print(country_capital_series)
a             NaN
Germany    Berlin
c             NaN
d             NaN
e             NaN
Austria    Vienna
g             NaN
h             NaN
dtype: object

谢谢,成功了

此外,我还创建了一个数据框,并根据您的建议更改了索引。 而且还可以。 汽车详细信息={'Honda':['Civic','Accord','Acura','Pilot','Odyssey','Fit','Ringline','Civic掀背车','HR-V',], ‘年份’:[‘2005’、‘2012’、‘2018’、‘2020’、‘2013’、‘2019’、‘2020’、‘2019’、‘2014’], ‘里程’:[98600550001600016000150000170005010000110000], ‘价格’:[6500123001500018000770001660034000190012000], ‘类别’:[‘轿车’、‘轿车’、‘轿车’、‘suv’、‘小型货车’、‘掀背车’、‘卡车’、‘掀背车’、‘suv’]

             }

df_cars_details=pd.数据帧(cars_details)

从0到9的索引是

df_cars_details.index=['1'、'2'、'3'、'4'、'5'、'6'、'7'、'8'、'9'] 打印('df\U车辆\U带有新索引的详细信息') 打印(df\U车辆\U详细信息)

索引从1到9

我没有任何具体的目标来改变指数,只是把手放在熊猫身上 实践 但我很感激你的建议,它很有用

相关问题 更多 >