2 回答
TA贡献1909条经验 获得超7个赞
我认为你有重复的列名,所以如果想要 select Seriesget DataFrame:
df = pd.DataFrame([[1,2],[4,5], [7,8]], index=list('aab')).T
print (df)
a a b
0 1 4 7
1 2 5 8
print (df['a'])
a a
0 1 4
1 2 5
print (type(df['a']))
<class 'pandas.core.frame.DataFrame'>
print (df['b'])
0 7
1 8
Name: b, dtype: int64
print (type(df['b']))
<class 'pandas.core.series.Series'>
编辑:
这是另一个问题,一个级别MultiIndex,解决方案是将第一级重新分配回列get_level_values:
mux = pd.MultiIndex([['COST_/CPM', 'CTR', 'ECPM/_ROI', 'Goal_Ratio', 'Hour_of_the_Day',
'IMP./Joins', 'Raw_Clicks_/_Unique_Clicks', 'Raw_Impressions',
'Unique_Goal_/_UniqueGoal_Forecasted_Value']],
labels=[[4, 7, 5, 6, 1, 8, 3, 0, 2]])
df = pd.DataFrame([range(9)], columns=mux)
print (type(df['CTR']))
<class 'pandas.core.frame.DataFrame'>
df.columns = df.columns.get_level_values(0)
print (type(df['CTR']))
<class 'pandas.core.series.Series'>
添加回答
举报