Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

How to put a header title per dataframe after concatenate using pandas in python

dfs = [d1, d2, d3]

df_combined = pd.concat(
    [df.rename(columns=lambda x: x.zfill(4)) for df in dfs],
    keys=['HEADER TITLE{}'.format(i) for i in range(1, len(dfs) + 1)],
    axis=1
)

df_combined
Comment

How to put a header title per dataframe after concatenate using pandas in python

import pandas as pd
from decimal import Decimal, ROUND_HALF_UP

L=['0000','0100','0200','0300','0400','0500','0600'
                                        ,'0700','0800','0900','1000','1100','1200','1300'
                                        ,'1400','1500','1600','1700','1800','1900','2000'
                                        ,'2100','2200','2300']



df1 = pd.read_csv('Dataframe1.csv')
df1.Date = pd.to_datetime(df1.Date, dayfirst=True)
df1 = df1.pivot_table(values='SampleValues',index="SampleIndex",columns='SampleColumns',aggfunc='max',fill_value="ND")
df1.index = df1.index.map(lambda t: t.strftime('%Y-%m-%d'))
df1 = df1.reindex_axis(L, axis=1)
df1.ix[:,pd.isnull(df1).all()] = "ND"


df2 = pd.read_csv('Dataframe2.csv')
df2.Date = pd.to_datetime(df2.Date, dayfirst=True)
df2 = df2.pivot_table(values='SampleValues',index='SampleIndex',columns='SampleColumns',aggfunc='max',fill_value="ND")
df2.index = df2.index.map(lambda t: t.strftime('%Y-%m-%d'))
df2 = df2.reindex_axis(L, axis=1)
df2.ix[:,pd.isnull(df2).all()] = "ND"

df3 = pd.read_csv('Dataframe3.csv')
df3.Date = pd.to_datetime(df4.Date, dayfirst=True)
df3 = df4.pivot_table(values='SampleValues',index='SampleIndex',columns='SampleColumns',aggfunc='max',fill_value="ND")
df3.index = df4.index.map(lambda t: t.strftime('%Y-%m-%d'))
df3 = df4.reindex_axis(L, axis=1)
df3.ix[:,pd.isnull(df4).all()] = "ND"

keys = ['HEADER TITLE1','HEADER TITLE 2', 'HEADER TITLE 3']

df4 = pd.concat([df1,df2,df3], axis = 1,  keys = keys).to_csv("Output.csv", header = True, encoding = 'utf-8')
Comment

PREVIOUS NEXT
Code Example
Python :: Increase "bar width" "px.bar" 
Python :: fix certain parameters during curve fit python lambda 
Python :: comment enleve les chiffre duplice d une liste python 
Python :: train chatterbot using yml 
Python :: return a tuple c++ python 3 
Python :: python difference between multiprocessing pool and threadpool 
Python :: url namespaces for django rest router urls 
Python :: discord.py assign role 
Python :: Faster way to find list of unique elements in a list 
Python :: get the hour of every instance of the date_time 
Python :: if short for python 
Python :: python fibbonacci 
Python :: convert python code to java using jython 
Python :: conventional commits 
Python :: Spansk dansk 
Python :: how to make a password square multicolor square spiral python 
Python :: how to read backslash slash python 
Python :: pandas corr get couple value 
Python :: mechanize python LE #3 
Python :: form list of filename get the filename with highest num pythn 
Python :: "opencv write video" 
Python :: compute difference in dates after groupby 
Python :: uri beecrowd problem 1047 Game Time with Minutes 
Python :: how to use ci variables in python robot 
Python :: number of libraries in python 
Python :: python case sensitive when dealing with identifiers 
Python :: Command to import Required, All, Length, and Range from voluptuous 
Python :: display csv data into flask api 
Python :: code academy magic 8 bal code python 
Python :: docstring return list of tuple 
ADD CONTENT
Topic
Content
Source link
Name
2+4 =