Last active
November 15, 2022 15:05
-
-
Save Cheers3985/10e113ffce1d5657450c32ce52430386 to your computer and use it in GitHub Desktop.
[pandas.concat] 纵向合并多个dataframe #pandas.concat #pandas
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# pandas.concat 实现竖向的拼接 | |
import pandas as pd | |
df1=pd.DataFrame([10,12,13]) | |
df2=pd.DataFrame([22,33,44,55]) | |
df3=pd.DataFrame([90,94]) | |
pd.concat([df1,df2,df3],ignore_index=True) # 纵向组合,注意此时没有设定列名。此时拼接只有一列 | |
# --------concat 多列进行拼接--------- | |
import pandas as pd | |
df1=pd.DataFrame([10,12,13],columns=['1']) | |
df2=pd.DataFrame([22,33,44,55],columns=['2']) | |
df3=pd.DataFrame([90,94],columns=['3']) | |
df = pd.concat([df1,df2,df3],ignore_index=True,) | |
df | |
# 此时按照有3列,结果如下 | |
1 2 3 | |
0 10.0 NaN NaN | |
1 12.0 NaN NaN | |
2 13.0 NaN NaN | |
3 NaN 22.0 NaN | |
4 NaN 33.0 NaN | |
5 NaN 44.0 NaN | |
6 NaN 55.0 NaN | |
7 NaN NaN 90.0 | |
8 NaN NaN 94.0 | |
参考资料: | |
https://notebook.community/xlbaojun/Note-jupyter/05%E5%85%B6%E4%BB%96/pandas%E6%96%87%E6%A1%A3-zh-master/%E6%95%B0%E6%8D%AE%E5%90%88%E5%B9%B6%E3%80%81%E8%BF%9E%E6%8E%A5%E5%92%8C%E6%8B%BC%E6%8E%A5-Merge,%20join,%20and%20concat |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 重命名为一个列名,并且忽略index | |
df = pd.Dataframe(columns=['datetime','datetime_nano',f'{self.code}.last_price]) | |
df_file.columns = df.columns | |
df = pd.concat([df,df_file],ignore_index=True) | |
# 这是股票高频数据本地获取的逻辑 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment