1

I'm currently working with r number of dataframes with identical headers. I would like to select all except one (eg. except 'Apple') and change them by adding the dataframe number next to them.

In this case, all my r dataframes have:

                Apple             Orange            Pear       ...
0                  70                  5               3       ...
1                  15                  1               4       ...
2                  17                  1               6       ...
3                  26                  2               7       ...
4                  12                  1               2       ...
5                  16                  1               0       ...
6                  13                  1               1       ...

I have tried:

for r in range(len(csv_files)):
     dfs[r].columns = [col_name + ' R{}'.format(r+1) for col_name in dfs[r].columns]

For dataframe 1,

             Apple R1          Orange R1         Pear R1       ...
0                  70                  5               3       ...
1                  15                  1               4       ...
2                  17                  1               6       ...
3                  26                  2               7       ...
4                  12                  1               2       ...
5                  16                  1               0       ...
6                  13                  1               1       ...

For dataframe 2,

             Apple R2          Orange R2         Pear R2       ...
0                  70                  5               3       ...
1                  15                  1               4       ...
2                  17                  1               6       ...
3                  26                  2               7       ...
4                  12                  1               2       ...
5                  16                  1               0       ...
6                  13                  1               1       ...

For dataframe 3,

             Apple R3          Orange R3         Pear R3       ...
0                  70                  5               3       ...
1                  15                  1               4       ...
2                  17                  1               6       ...
3                  26                  2               7       ...
4                  12                  1               2       ...
5                  16                  1               0       ...
6                  13                  1               1       ...

Which gives me almost what I want but I can't find how to not include ['Apple'] in this action like for example on dataframe 3:

                Apple          Orange R3         Pear R3       ...
0                  70                  5               3       ...
1                  15                  1               4       ...
2                  17                  1               6       ...
3                  26                  2               7       ...
4                  12                  1               2       ...
5                  16                  1               0       ...
6                  13                  1               1       ...

Thank you :)

2 Answers 2

1

If same positions of columns and Apple is always first:

for r in range(len(csv_files)):
     dfs[r].columns = ['Apple'] + 
                      [col_name + ' R{}'.format(r+1) for col_name in dfs[r].columns[1:]]

More general solution without define position of column:

for r in range(len(csv_files)):
     dfs[r].columns = [c if c == 'Apple' else c + ' R{}'.format(r+1) for c in dfs[r].columns]
Sign up to request clarification or add additional context in comments.

Comments

0

You could use

for r in range(len(csv_files)):
     newcols = dfs[r].columns[1:] + ' R{}'.format(r)
     dfs[r].columns = ['Apple'] + newcols.tolist()

Or, use map like

for r in range(len(csv_files)):
     newcols = dfs[r].columns[1:].map(lambda x: '{} R{}'.format(x, r))
     dfs[r].columns = ['Apple'] + newcols.tolist()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.