Reputation:
I have the following table as input:
INPUT
Column1.ab.gz | Column2.ab.gz | Column3.ab.gz | Column4.ab.gz | Column5.ab.gz | Column6.ab.gz |
---|---|---|---|---|---|
1234 | 0 | 22 | 33 | 5 | 7 |
1235 | 1 | 2 | 2 | 0 | 234 |
1236 | 9 | 2 | 82 | 0 | 66 |
1237 | 0 | 0 | 0 | 0 | 0 |
1238 | 7 | 11 | 6 | 66 | 1 |
1239 | 5 | 27 | 5 | 0 | 8 |
1240 | 0 | 0 | 0 | 0 | 0 |
1241 | 15 | 0 | 2 | 13 | 5 |
I would like to rename the column name without 'ab.gz' automatically rather than renaming the column manually (data.rename(columns = 'Column1.ab.gz' :'Column1', 'Column2.ab.gz' :'Column2', 'Column3.ab.gz' :'Column3', 'Column4.ab.gz' :'Column4', 'Column5.ab.gz' :'Column5', 'Column6.ab.gz' :'Column6')
) since the original table contains 50 columns.
I would also like to remove the row which have all column value as 0.
The output should look something like the following:
OUTPUT
Column1 | Column2 | Column3 | Column4 | Column5 | Column6 |
---|---|---|---|---|---|
1234 | 0 | 22 | 33 | 5 | 7 |
1235 | 1 | 2 | 2 | 0 | 234 |
1236 | 9 | 2 | 82 | 0 | 66 |
1238 | 7 | 11 | 6 | 66 | 1 |
1239 | 5 | 27 | 5 | 0 | 8 |
1241 | 15 | 0 | 2 | 13 | 5 |
How do I do that in python?
Thanks in advance!
Upvotes: 1
Views: 515
Reputation: 1413
You can use list comprehension
like this:
df.columns=[i.replace('.ab.gz','') for i in df.columns]
Upvotes: 0
Reputation: 862511
Use str.replace
by empty string:
df.columns = df.columns.str.replace('.ab.gz', '')
print (df)
Column1 Column2 Column3 Column4 Column5 Column6
0 aa 0 22 33 5 7
1 bb 1 2 2 0 234
2 cc 9 2 82 0 66
3 ee 0 0 0 0 0
4 ff 7 11 6 66 1
5 dd 5 27 5 0 8
6 aa 0 0 0 0 0
7 bb 15 0 2 13 5
If need values before first .
:
df.columns = df.columns.str.split('.').str[0]
Upvotes: 3