Appending dataframes from right column

Question:

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?

Asked By: mahmood

||

Answers:

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.

Answered By: wavetitan
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.