Home > OS >  Appending dataframes from right column
Appending dataframes from right column

Time:12-11

In the following code, in every iteration, a csv file is read as a data frame and it is concatenated to an result data frame (initially empty) from right.

result = pd.DataFrame()
for bench in benchmarks:
    df = read_raw(bench)
    print(df)
    result = pd.concat([result, df], axis=1, join="inner")
print(result.info())

As you can see below, df is not empty, but final result is empty.

$ python3 test.py
                  launch
0                      1
1                    524
2                   3611
3                   3611
4                    169
...                  ...
92515                143
92516                138
92517                169
92518                138
92519               1048

[92520 rows x 1 columns]
<class 'pandas.core.frame.DataFrame'>
Index: 0 entries
Data columns (total 1 columns):
 #   Column  Non-Null Count  Dtype
---  ------  --------------  -----
 0   M1      0 non-null      int64

How can I fix that?

CodePudding user response:

You can remove join="inner".

result = pd.concat([result, df], axis=1)

Setting argument join="inner" means that only indexes appear in both result and df will be concatenated. Since result is an empty dataframe with no index, the result of your pd.concat will always return empty dataframe.

  • Related