Python中创建Dataframe的四种方法

2025-12-01 0 73,617

Pandas主要有两种数据结构,一个是Series,另一个是DataFrame,小编介绍过Pandas中创建Series的三种方法,那DataFrame又如何创建呢?本文介绍创建Dataframe的四种方法:1、通过字典创建DataFrame;2、通过列表创建DataFrame;3、通过numpy ndarray创建dataframe;4、通过一个Series对象创建。

1、通过字典创建DataFrame;

from pandas import DataFrame
# 利用字典
merge_dt_dict = {'date':date_list,
                'update':update_list,
                'serverip':serverip_list}

data_df = DataFrame(merge_dt_dict)

2、通过列表创建DataFrame;

import pandas as pd
data = [[4, 7, 10], 
        [5, 8, 11], 
        [6, 9, 12]]
 
df = pd.DataFrame(data, columns=['A', 'B', 'C'])
df

3、通过numpy ndarray创建dataframe;

arr = np.random.randn(6,4)
index_rows = pd.date_range('today', periods=6)
index_columns = ['A','B','C','D']
dataframe1 =
pd.DataFrame(arr,index=index_rows,columns=index_columns)
dataframe1

4、通过一个Series对象创建。

myDict3 = {'one': s1, 'two': s2}
df4 = pd.DataFrame(data = myDict3)
print(df4)
--------------------------------
[out]:
  one  two
0   a    5
1   b   15
2   c   25
3   d   35

以上就是Python中创建Dataframe的四种方法,大家可以直接套入使用哦·更多python学习推荐:python教程。

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:以上部本文内容由互联网用户自发贡献,本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。投诉邮箱:3758217903@qq.com

ZhiUp资源网 python基础 Python中创建Dataframe的四种方法 https://www.zhiup.top/2346.html

相关