将两个系列合并为数据框中的新系列?
Combine two series as new one in dataframe?
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)
我之前建议你沿着列进行连接是我的错误。
相反,你应该沿着行连接:
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,)
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)
我之前建议你沿着列进行连接是我的错误。
相反,你应该沿着行连接:
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,)