type(x)
<class 'pandas.core.frame.DataFrame'>
x.shape
(18, 12)
要用表达式引用第一行和3:5列:
type(x.iloc[0,3:5])
<class 'pandas.core.series.Series'>
x.iloc[0,3:5]
total_operating_revenue NaN
net_profit 3.43019e+07
Name: 2001-12-31, dtype: object
使用表达式引用第一行和8:10列:
type(x.iloc[0,8:10])
<class 'pandas.core.series.Series'>
x.iloc[0,8:10]
total_operating_revenue_parent 5.05e+8
net_profit_parent 4.4e+07
Name: 2001-12-31, dtype: object
我想获得合并的新系列(假设为y
),如下所示:
type(y)
<class 'pandas.core.series.Series'>
y.shape
(4,)
y
包含:
total_operating_revenue NaN
net_profit 3.43019e+07
total_operating_revenue_parent 5.05e+8
net_profit_parent 4.4e+07
Name: 2001-12-31, dtype: object
我失败的尝试:
x.iloc[0,[3:5,8:10]]
x.iloc[0,3:5].combine(x.iloc[0,8:10])
pd.concat([x.iloc[0,3:5],x.iloc[0,8:10]],axis=1)
不是我的期望,与y
完全不同。
z = pd.concat([x.iloc[0,3:5],x.iloc[0,8:10]],axis=1)
type(z)
<class 'pandas.core.frame.DataFrame'>
z.shape
(4, 2)
答案 0 :(得分:0)
我之前建议您在各列中进行合并的错误。
相反,您应该沿着以下行进行合并:
y = pd.concat([x.iloc[0,3:5],x.iloc[0,8:10]])
示例:
import numpy as np
x = pd.DataFrame(np.random.randint(0,100,size=(18, 12)),
columns=list('ABCDEFGHIJKL'))
然后:
In [392]: y = pd.concat([x.iloc[0,3:5],x.iloc[0,8:10]])
In [393]: y.shape
Out[393]: (4,)