DPdl
DPdl

Reputation: 755

Combining dataframes in python by index

I have multiple dataframes (separated by n, then by s, then by m) like this, call it df1 for n = '239', s = 'male', m = 'is1':

Days    24hU       24hF     ...
1       1e-3       ....     ...
2       8e-4       ....     ...
5       6e-4       ....     ...
...     ....       ....     ...

All dataframes have the same first column 'Days' (which can be set as the index for each dataframe) with same values in the rows. I would like to have a combined dataframe that contains column headers:

Days    24hU_n1s1m1    24hU_n1s1m2    24hU_n1s1m3    ...    24hU_n2s2m6
1       1e-3           ...            ...            ...    ...
2       8e-4           ...
5       6e-4           ...
...     ...            ...

I have the following so far:

for n in ("239", ...):
    for s in ("Male", "Female"):
        for m in ("is1",...):
            df = pandas.read_csv("DF Files//"+n+"//CSVoutputFiles//"+s+"//"+m+".csv", sep=',',skiprows=1, nrows=300).set_index('Days')

Upvotes: 0

Views: 64

Answers (1)

Noah
Noah

Reputation: 22646

Something like this? (This sounds kind of like a data frame merge but that only works in pairs.)

# first set the index
df1 = df1.set_index("Days")
df1.columns = [c+"_suffix1" for c in df1.columns]
... # similarly for other dataframes
combined = pd.concat([df1, df2], axis=1)

Upvotes: 1

Related Questions