The Pandas HDFStore function used to append data to Table in file.
1 Pandas.HDFStore.append(key, value, format = None, axes = None, index = True, append = True,
2 complib = None, complevel = None, columns = None, min_itemsize = None, nan_rep = None,
3 chunksize = None, expectedrows = None, dropna = None, data_columns = None, encoding = None, errors = 'strict')
key : It is string that specifies the key for the specified object in the HDFStore object.
value : It is actual value that will be mapped with key in the HDFStore object.
format : It is string that specifies the format to use when storing object in HDFStore. If not specified, the default value will be table.
1. | table : It specifies Table format. Write as a PyTable Table structure which may perform worse but allow more flexible operations like searching and selecting subsets of the data. |
index : It is boolean that write DataFrame index as a column. If not specified, the default value will be True.
append : It is boolean value that will force Table format, append the input data to the existing. If not specified, the default value will be False.
data_columns : It is list of columns or True.
1. | list : it specifies the list of columns to create as data columns. |
2. | True : It specified, it includes all columns. |
min_itemsize : It is dictionary of columns that specifies the minimum str sizes.
nan_rep : It is string that specifies the string to use for nan value.
chunksize : It is string that specifies the size to chunk while writing.
expectedrows : It is expected TOTAL rows size of this table.
encoding : It is string that provide an encoding for strings. If not specified, the default value will be None.
dropna : It is boolean that specifies whether to remove missing value. If not specified, the default value will be False.
1 import pandas as pd
2
3 store = pd.HDFStore('data1.h5')
4
5 df = pd.DataFrame([['a', 'b'], ['c', 'd']],
6 index = ['R1', 'R2'],
7 columns = ['C1', 'C2'])
8
9 store.append('df', df)
10
11 res = store.select('df')
12 print('The selected key value is :')
13 print(res)
14
15
16 store.close()
In the above example, a HDFStore object is created by passing a name. An append() function is called by passing an key and DataFrame object. A select() function is called by passing a key that retrieves the mapped object and assign to the variable that will be printed on console.
1 The selected key value is :
2 C1 C2
3 R1 a b
4 R2 c d
Note : If you execute above example multiple time, it will append new rows each time.
Example 2
1 import pandas as pd
2
3 store = pd.HDFStore('data1.h5')
4
5 df = pd.DataFrame([[1, 2], [3, 4]],
6 index = ['x1', 'x2'],
7 columns = ['A1', 'A2'])
8
9 store.append('df', df)
10
11 res = store.select('df')
12 print('The selected key value is :')
13 print(res)
14
15
16 store.close()
In the above example, a HDFStore object is created by passing a name. An append() function is called by passing an key and DataFrame object. A select() function is called by passing a key that retrieves the mapped object and assign to the variable that will be print.
1 The selected key value is :
2 A1 A2
3 x1 1 2
4 x2 3 4
Related options for your search