使用pandas忽略行列索引,纵向拼接多个dataframe
作者:呆萌的代Ma 时间:2022-05-23 08:52:42
从wind上面搞到一批股票数据后发现:本来是一个类型的数据,但是由于季度不同,列名也不同,导致使用pandas合并多个报表的时候总是出现一大堆NaN,所以这里我写了一个函数,专门针对这样的表
它的思路是:
生成一堆单词,然后把这些表的列索引全部替换为这些单词,然后调用 pd.concat() 把这些dataframe全部合并后再把列索引改回来,当然,这里也可以手动指定列索引。
使用方法见代码的最后一行,传入一个dataframe的list就可以了。
import pandas as pd
from random import Random
# 随机生成一堆单词作为公共的列名
def random_list(random_str_count, randomlengtd=6):
result_list = []
random = Random()
chars = "qwertyuiopasdfghjklzxcvbnm"
for str_count in range(random_str_count):
ranstr = ""
lengtd = len(chars) - 1
for str_lengtd in range(randomlengtd):
ranstr += chars[random.randint(0, lengtd)]
result_list.append(ranstr)
return result_list
def combine_as_data_location(pd_list, columns=''):
if not pd_list:
return None
old_columns = pd_list[0].columns
if columns:
new_columns = columns
else:
new_columns = random_list(pd_list[0].shape[1])
for data_df in pd_list:
# data is pandas Dataframe
data_df.columns = new_columns
result_df = pd.concat(pd_list, ignore_index=True)
if columns:
return result_df
else:
result_df.columns = old_columns
return result_df
result_df = combine_as_data_location([df1,df2,df3])
补充:pandas.concat实现竖着拼接、横着拼接DataFrame
1、concat竖着拼接(默认的竖着,axis=0)
话不多说,直接看例子:
import pandas as pd
df1=pd.DataFrame([10,12,13])
df2=pd.DataFrame([22,33,44,55])
df3=pd.DataFrame([90,94])
df1
0 | |
---|---|
0 | 10 |
1 | 12 |
2 | 13 |
df2
0 | |
---|---|
0 | 22 |
1 | 33 |
2 | 44 |
3 | 55 |
df3
0 | |
---|---|
0 | 90 |
1 | 94 |
res= pd.concat([df1,df2,df3])
res
0 | |
---|---|
0 | 10 |
1 | 12 |
2 | 13 |
0 | 22 |
1 | 33 |
2 | 44 |
3 | 55 |
0 | 90 |
1 | 94 |
如果要生成新索引,忽略原来索引怎么办?
默认有个参数ignore_index= False,将其值改为True:
res2= pd.concat([df1,df2,df3], ignore_index=True)
res2
0 | |
---|---|
0 | 10 |
1 | 12 |
2 | 13 |
3 | 22 |
4 | 33 |
5 | 44 |
6 | 55 |
7 | 90 |
8 | 94 |
2、concat横着拼接
用参数axis= 1,看例子:
res_heng= pd.concat([df1,df2,df3], axis=1)
res_heng
0 | 0 | 0 | |
---|---|---|---|
0 | 10.0 | 22 | 90.0 |
1 | 12.0 | 33 | 94.0 |
2 | 13.0 | 44 | NaN |
3 | NaN | 55 | NaN |
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。如有错误或未考虑完全的地方,望不吝赐教。
来源:https://blog.csdn.net/weixin_35757704/article/details/90177680
标签:pandas,索引,纵向,拼接,dataframe
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python grequests模块使用场景及代码实例
2021-08-05 18:30:41
![](https://img.aspxhome.com/file/2023/1/124961_0s.png)
Python机器学习应用之支持向量机的分类预测篇
2023-08-29 20:42:55
![](https://img.aspxhome.com/file/2023/8/75948_0s.png)
ie的javascript失效问题
2009-09-21 12:49:00
利用Python脚本批量生成SQL语句
2023-06-10 10:02:37
![](https://img.aspxhome.com/file/2023/2/103512_0s.jpg)
Python max函数中key的用法及原理解析
2022-09-09 04:00:24
![](https://img.aspxhome.com/file/2023/7/97247_0s.png)
漫谈前端开发中的团队合作
2009-02-05 21:02:00
python机器学习Github已达8.9Kstars模型解释器LIME
2022-06-21 13:38:28
![](https://img.aspxhome.com/file/2023/2/90912_0s.png)
Python编程使用matplotlib绘制动态圆锥曲线示例
2021-08-30 03:38:18
![](https://img.aspxhome.com/file/2023/6/85096_0s.png)
XML轻松学习手册(3)XML的术语
2008-09-05 17:17:00
python-for x in range的用法(注意要点、细节)
2022-11-12 22:40:49
![](https://img.aspxhome.com/file/2023/8/100978_0s.png)
Ubuntu下创建虚拟独立的Python环境全过程
2021-07-27 13:22:29
python3 cookbook中常遇问题解答
2022-09-07 09:07:05
python排序函数sort()与sorted()的区别
2023-09-08 23:38:08
python 内置函数filter
2023-01-07 21:53:37
Pytorch中关于F.normalize计算理解
2021-10-10 12:33:34
![](https://img.aspxhome.com/file/2023/6/115026_0s.png)
pandas 把数据写入txt文件每行固定写入一定数量的值方法
2021-06-13 20:08:14
![](https://img.aspxhome.com/file/2023/4/87734_0s.jpg)
在linux下实现 python 监控usb设备信号
2022-04-18 15:29:24
Python实现的简单计算器功能详解
2023-11-17 09:34:08
![](https://img.aspxhome.com/file/2023/1/64361_0s.jpg)
Python splitlines使用技巧
2023-01-11 01:13:56
Python模拟脉冲星伪信号频率实例代码
2023-02-12 06:01:08
![](https://img.aspxhome.com/file/2023/1/107521_0s.gif)