怎么把series变为datamate_pandas小技巧将Series转换为DataFrame

在数据处理中,常需要将Series转换为DataFrame,例如在使用seaborn库进行可视化时。通过调用Series的reset_index方法,可以轻松实现这一转换。示例中展示了读取CSV文件,对数据预处理,进行聚合操作后,如何利用groupby和reset_index将Series转换为DataFrame,并用seaborn绘制柱状图。
摘要由CSDN通过智能技术生成

写在前面

在实际工作中,若遇到以下情况,则必须要进行series和dataframe的转换。 - 使用seaborn进行可视化,输入的数据必须为dataframe

sns.catplot(

['x=None', 'y=None', 'hue=None', 'data=None', 'row=None', 'col=None', 'col_wrap=None', 'estimator=', 'ci=95', 'n_boot=1000', 'units=None', 'order=None', 'hue_order=None', 'row_order=None', 'col_order=None', "kind='strip'", 'height=5', 'aspect=1', 'orient=None', 'color=None', 'palette=None', 'legend=True', 'legend_out=True', 'sharex=True', 'sharey=True', 'margin_titles=False', 'facet_kws=None', '**kwargs'],

)

#其中data必须为data : DataFrame

# Long-form (tidy) dataset for plotting. Each column should correspond

# to a variable, and each row should correspond to an observation.在通过pandas读取文件后,然后进行了聚合操作。可以看出聚合后输出为series。

df.groupby(

['by=None', 'axis=0', 'level=None', 'as_index=True', 'sort=True', 'group_keys=True', 'squeeze=False', 'observed=False', '**kwargs'],

)

#Group series using mapper (dict or key function, apply given function

#to group, return result as series) or by a series of columns.对聚合操作后的数据(此时已经是从dataframe转换至series),那么就需要进行series至dataframe的转换。

Series -> DataFrame 转换方法

group.reset_index(level=None, drop=False, name=None, inplace=False)

#Generate a new DataFrame or Series with the index reset.

可以看出,通过上述操作可以完成series向dataframe的转换。 特别是在进行多个列的聚合操作时,此方法非常管用。

示例读取文件

import pandas as pd

df =pd.read_csv('d:/out.csv',encoding = 'gbk',dtype=str)对数据进行预处理

df.dropna(subset=['Lon', 'Lon','ECI','Cell ID'],inplace=True)查看导入数据信息

df.info()

Int64Index: 18333 entries, 153 to 19082

Data columns (total 29 columns):

NO. 18333 non-null object

UETime 18333 non-null object

PCTime 18333 non-null object

Lon 18333 non-null object

Lat 18333 non-null object

ECI 18333 non-null object

Cell ID 18333 non-null object

earfcn_1 18317 non-null object

pci_1 18317 non-null object

rsrp_1 18317 non-null object

earfcn_2 17533 non-null object

pci_2 17533 non-null object

rsrp_2 17533 non-null object

earfcn_3 17433 non-null object

pci_3 17433 non-null object

rsrp_3 17433 non-null object

earfcn_4 15276 non-null object

pci_4 15276 non-null object

rsrp_4 15276 non-null object

earfcn_5 14829 non-null object

pci_5 14829 non-null object

rsrp_5 14829 non-null object

earfcn_6 12166 non-null object

pci_6 12166 non-null object

rsrp_6 12166 non-null object

is_overlap 18333 non-null object

overlap_pci 18333 non-null object

is_mod 18333 non-null object

mod_pci 18333 non-null object

dtypes: object(29)

memory usage: 4.2+ MB

从上图可以看出,pandas读入的数据为dataframe类型。 - 对数据进行聚合

type(df.groupby(['Cell ID','is_overlap'])['is_overlap'].count())

pandas.core.series.Series

从上图中可以看出,聚合后的数据为series类型。 - 利用seaborn进行可视化 先将series转换至dataframe

group.reset_index(name='count')

开始绘图

sns.catplot(x='Cell ID',y='count',data=new_group,kind='bar',hue='is_overlap',aspect=15)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值