首页 > 编程语言 >python pandas DataFrame,Series进一步理解

python pandas DataFrame,Series进一步理解

时间:2023-03-10 17:44:26浏览次数:43  
标签:20230310 python Series NaN DataFrame df serA CS

 

更进一步学习和理解DataFrame,Series

多个Series 拼凑成一个 dict, 然后将dict转为 DataFrame结构

#!/usr/bin/evn python

import numpy as np
import pandas as pd

serA = pd.Series({"000001.CS":1.2, "000002.CS":1.3})
print("serA:", serA)
print("serA.index:", serA.index)
print("serA.values:", serA.values)

serB = pd.Series({"000003.CS":10.2, "000004.CS":10.3, "000005.CS":10.3})
serC = pd.Series({"000006.CS":20.2,"000004.CS":20.3,"000001.CS":20.3})
serD = pd.Series({"000007.CS":30.2, "000008.CS":31.3, "000009.CS":31.5, "000010.CS":40.3, "000011.CS":5.3})
serE = pd.Series(dtype='float64')

results={}
results["serA"] = serA
results["serB"] = serB
results["serC"] = serC
results["serD"] = serD
#results["serE"] = pd.Series(np.nan,index=["EMPTY_CODE"]);
results["serE"] = serE

print("result:", results)
dt = 20230310
df = pd.DataFrame(results)
# 去掉 EMPTY_CODE 这个 index
#df = df.filter(like="CS", axis=0)
df['dt'] = dt
df['symbol'] = df.index.values
print("df:",df)
df.reset_index(drop=True, inplace=True)
print("df.reset_index:",df)
serA: 000001.CS    1.2
000002.CS    1.3
dtype: float64
serA.index: Index(['000001.CS', '000002.CS'], dtype='object')
serA.values: [1.2 1.3]
result: {'serA': 000001.CS    1.2
000002.CS    1.3
dtype: float64, 'serB': 000003.CS    10.2
000004.CS    10.3
000005.CS    10.3
dtype: float64, 'serC': 000006.CS    20.2
000004.CS    20.3
000001.CS    20.3
dtype: float64, 'serD': 000007.CS    30.2
000008.CS    31.3
000009.CS    31.5
000010.CS    40.3
000011.CS     5.3
dtype: float64, 'serE': Series([], dtype: float64)}
df:            serA  serB  serC  serD  serE        dt     symbol
000001.CS   1.2   NaN  20.3   NaN   NaN  20230310  000001.CS
000002.CS   1.3   NaN   NaN   NaN   NaN  20230310  000002.CS
000003.CS   NaN  10.2   NaN   NaN   NaN  20230310  000003.CS
000004.CS   NaN  10.3  20.3   NaN   NaN  20230310  000004.CS
000005.CS   NaN  10.3   NaN   NaN   NaN  20230310  000005.CS
000006.CS   NaN   NaN  20.2   NaN   NaN  20230310  000006.CS
000007.CS   NaN   NaN   NaN  30.2   NaN  20230310  000007.CS
000008.CS   NaN   NaN   NaN  31.3   NaN  20230310  000008.CS
000009.CS   NaN   NaN   NaN  31.5   NaN  20230310  000009.CS
000010.CS   NaN   NaN   NaN  40.3   NaN  20230310  000010.CS
000011.CS   NaN   NaN   NaN   5.3   NaN  20230310  000011.CS
df.reset_index:     serA  serB  serC  serD  serE        dt     symbol
0    1.2   NaN  20.3   NaN   NaN  20230310  000001.CS
1    1.3   NaN   NaN   NaN   NaN  20230310  000002.CS
2    NaN  10.2   NaN   NaN   NaN  20230310  000003.CS
3    NaN  10.3  20.3   NaN   NaN  20230310  000004.CS
4    NaN  10.3   NaN   NaN   NaN  20230310  000005.CS
5    NaN   NaN  20.2   NaN   NaN  20230310  000006.CS
6    NaN   NaN   NaN  30.2   NaN  20230310  000007.CS
7    NaN   NaN   NaN  31.3   NaN  20230310  000008.CS
8    NaN   NaN   NaN  31.5   NaN  20230310  000009.CS
9    NaN   NaN   NaN  40.3   NaN  20230310  000010.CS
10   NaN   NaN   NaN   5.3   NaN  20230310  000011.CS

Process finished with exit code 0

 

标签:20230310,python,Series,NaN,DataFrame,df,serA,CS
From: https://www.cnblogs.com/music-liang/p/17204257.html

相关文章

  • python_2
    importRPi.GPIOasGPIOimporttimeGPIO.setmode(GPIO.BCM)led=21bt=20GPIO.setup(led,GPIO.OUT)GPIO.setup(bt,GPIO.IN,pull_up_down=GPIO.PUD_UP)ledSt......
  • python
    importRPi.GPIOasGPIOimporttimeGPIO.setmode(GPIO.BCM)led=21bt=20GPIO.setup(led,GPIO.OUT)GPIO.setup(bt,GPIO.IN,pull_up_down=GPIO.PUD_UP)ledStatus=Fals......
  • python根据当前时间创建文件夹
    若想在文件夹名称中包含小时、分钟和秒,可以将时间格式化字符串修改为"%Y-%m-%d-%H-%M-%S"。下面是示例代码:importosfromdatetimeimportdatetime#获取当前时间n......
  • Python文件打包exe程序
    文件打包概要脚本打包exe:win/mac【终端】qt5,开发桌面应用打包qt5程序【桌面应用】1.exe打包pipinstallpyinstaller注意事项:支持mac、win(Windows建议使用py......
  • Python:基于词频、TF-IDF 生成词云图
    目录读取数据数据预处理读取文件词频统计jieba分词库函数编写TF-IDF方法TF-IDF函数编写生成词云图wordCloud库函数编写词云图生成效果参考资料现有一份介绍某地点的tx......
  • 【python】pandas 时间序列转换
    1.时间戳-->时间time_stamp=1677895200000#2023-03-0410:00:00pd.to_datetime(time_stamp,unit='ms')#Timestamp('2023-03-0402:00:00')utc时间pd.to_dat......
  • Python - allure 报告使用汇总
    使用pytest做自动化测试过程中,关于allure报告的使用方法汇总pythonallure包使用allure命令行工具生成测试报告不启动服务$alluregenerate{allure_result}......
  • Python - else 语法总结
    else使用汇总。问题阅读别人代码,有点疑惑,精简后如下:defcode_example(arg=None):foriinrange(5):ifarg:breakelse:pr......
  • Python - 连接数据库
    python连接数据库操作pymysqlimportpymysqldefget_connect():connect=pymysql.connect( host="xxx.com",port=3306,user="test",......
  • Python - pandas 数据处理
    数据处理pandas数据读取pd.read_csv:csv/tsv/txt用逗号、tab分隔的纯文本文件pd.read_excel::微软xls或者xlsx文件pd.read_sql:mysql关系型数据库pd.rea......