使用适当的列值将pandas.core.series.Series转换为数据框python [英] Converting pandas.core.series.Series to dataframe with appropriate column values python
本文介绍了使用适当的列值将pandas.core.series.Series转换为数据框python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在运行一个函数,其中的变量是pandas.core.series.Series类型.
i'm running a function in which a variable is of pandas.core.series.Series type.
type of the series shown below.
<class 'pandas.core.series.Series'>
product_id_y 1159730
count 1
Name: 6159402, dtype: object
我想将其转换为数据框,这样我得到
i want to convert this into a dataframe,such that, i get
product_id_y count
1159730 1
我尝试这样做:
series1 = series1.to_frame()
但得到错误的结果
转换为数据框后
6159402
product_id_y 1159730
count 1
执行重置索引后,我为series1 = series1.reset_index()
after doing reset index i'e series1 = series1.reset_index()
index 6159402
0 product_id_y 1159730
1 count 1
还有其他其他方法吗?
推荐答案
您非常亲密,首先 T
:
You was very close, first to_frame
and then transpose by T
:
s = pd.Series([1159730, 1], index=['product_id_y','count'], name=6159402)
print (s)
product_id_y 1159730
count 1
Name: 6159402, dtype: int64
df = s.to_frame().T
print (df)
product_id_y count
6159402 1159730 1
df = s.rename(None).to_frame().T
print (df)
product_id_y count
0 1159730 1
使用DataFrame
构造函数的另一种解决方案:
Another solution with DataFrame
constructor:
df = pd.DataFrame([s])
print (df)
product_id_y count
6159402 1159730 1
df = pd.DataFrame([s.rename(None)])
print (df)
product_id_y count
0 1159730 1
这篇关于使用适当的列值将pandas.core.series.Series转换为数据框python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文