2013-09-28 3 views
1

스프레드 시트를 df이라는 Python DataFrame으로 가져옵니다. 내가 df.columns.is_unique를 실행하면 내가 False열이 자동으로 바뀌어 고유한지 확인하십시오.

내가 자동으로 (같은 또는 일) 열 'A'에 'A_2'

이름을 변경하려는 얻을

df=pd.DataFrame({'a': np.random.rand(10), 'b': np.random.rand(10)}) 
df.columns=['a','a'] 

      a   a 
0 0.973858 0.036459 
1 0.835112 0.947461 
2 0.520322 0.593110 
3 0.480624 0.047711 
4 0.643448 0.104433 
5 0.961639 0.840359 
6 0.848124 0.437380 
7 0.579651 0.257770 
8 0.919173 0.785614 
9 0.505613 0.362737 

:

의이 샘플을 드리겠습니다

나는 해결책을 기대하지 않는다. df.columns=['a','a_2']

나는 여러 칼럼에 사용할 수있는 해결책을 찾고있다. NS!

+1

보기 용액 [팬더의 DataFrame - 개명 다수가 동일한 이름 열 (http://stackoverflow.com/questions/24685012/pandas-dataframe-renaming-multiple-identically-named-columns/24686572#24686572) – Lamakaha

답변

7

수동으로 열을 uniquify 수 있습니다

df_columns = ['a', 'b', 'a', 'a_2', 'a_2', 'a', 'a_2', 'a_2_2'] 

def uniquify(df_columns): 
    seen = set() 

    for item in df_columns: 
     fudge = 1 
     newitem = item 

     while newitem in seen: 
      fudge += 1 
      newitem = "{}_{}".format(item, fudge) 

     yield newitem 
     seen.add(newitem) 

list(uniquify(df_columns)) 
#>>> ['a', 'b', 'a_2', 'a_2_2', 'a_2_3', 'a_3', 'a_2_4', 'a_2_2_2'] 
1

나는 파이썬 DataFrame라는 이름의 DF에 스프레드 시트를 가져 ... 나는 자동으로 [중복] 열 [이름] 이름을 을하고 싶습니다.

판다는 ... 당신은 아무것도 할 필요없이 자동으로 당신을 위해 수행

Test.xls를 : enter image description here

import pandas as pd 
import numpy as np 

df = pd.io.excel.read_excel(
    "./test.xls", 
    "Sheet1", 
    header=0, 
    index_col=0, 
) 
print df 

--output:-- 
     a b c b.1 a.1 a.2 
index        
0  10 100 -10 -100 10 21 
1  20 200 -20 -200 11 22 
2  30 300 -30 -300 12 23 
3  40 400 -40 -400 13 24 
4  50 500 -50 -500 14 25 
5  60 600 -60 -600 15 26 


print df.columns.is_unique 

--output:-- 
True 

어떤 이유로 당신이 제공되는 경우 DataFrame 중복 열을 사용하면 다음과 같이 할 수 있습니다.

import pandas as pd 
import numpy as np 
from collections import defaultdict 

df = pd.DataFrame(
    { 
     'k': np.random.rand(10), 
     'l': np.random.rand(10), 
     'm': np.random.rand(10), 
     'n': np.random.rand(10), 
     'o': np.random.rand(10), 
     'p': np.random.rand(10), 
    } 
) 

print df 

--output:-- 
     k   l   m   n   o   p 
0 0.566150 0.025225 0.744377 0.222350 0.800402 0.449897 
1 0.701286 0.182459 0.661226 0.991143 0.793382 0.980042 
2 0.383213 0.977222 0.404271 0.050061 0.839817 0.779233 
3 0.428601 0.303425 0.144961 0.313716 0.244979 0.487191 
4 0.187289 0.537962 0.669240 0.096126 0.242258 0.645199 
5 0.508956 0.904390 0.838986 0.315681 0.359415 0.830092 
6 0.007256 0.136114 0.775670 0.665000 0.840027 0.991058 
7 0.719344 0.072410 0.378754 0.527760 0.205777 0.870234 
8 0.255007 0.098893 0.079230 0.225225 0.490689 0.554835 
9 0.481340 0.300319 0.649762 0.460897 0.488406 0.16604 


df.columns = ['a', 'b', 'c', 'b', 'a', 'a'] 
print df 

--output:-- 
      a   b   c   b   a   a 
0 0.566150 0.025225 0.744377 0.222350 0.800402 0.449897 
1 0.701286 0.182459 0.661226 0.991143 0.793382 0.980042 
2 0.383213 0.977222 0.404271 0.050061 0.839817 0.779233 
3 0.428601 0.303425 0.144961 0.313716 0.244979 0.487191 
4 0.187289 0.537962 0.669240 0.096126 0.242258 0.645199 
5 0.508956 0.904390 0.838986 0.315681 0.359415 0.830092 
6 0.007256 0.136114 0.775670 0.665000 0.840027 0.991058 
7 0.719344 0.072410 0.378754 0.527760 0.205777 0.870234 
8 0.255007 0.098893 0.079230 0.225225 0.490689 0.554835 
9 0.481340 0.300319 0.649762 0.460897 0.488406 0.166047 


print df.columns.is_unique 

--output:-- 
False 


name_counts = defaultdict(int) 
new_col_names = [] 

for name in df.columns: 
    new_count = name_counts[name] + 1 
    new_col_names.append("{}{}".format(name, new_count)) 
    name_counts[name] = new_count 

print new_col_names 


--output:-- 
['a1', 'b1', 'c1', 'b2', 'a2', 'a3'] 


df.columns = new_col_names 
print df 

--output:-- 
     a1  b1  c1  b2  a2  a3 
0 0.264598 0.321378 0.466370 0.986725 0.580326 0.671168 
1 0.938810 0.179999 0.403530 0.675112 0.279931 0.011046 
2 0.935888 0.167405 0.733762 0.806580 0.392198 0.180401 
3 0.218825 0.295763 0.174213 0.457533 0.234081 0.555525 
4 0.891890 0.196245 0.425918 0.786676 0.791679 0.119826 
5 0.721305 0.496182 0.236912 0.562977 0.249758 0.352434 
6 0.433437 0.501975 0.088516 0.303067 0.916619 0.717283 
7 0.026491 0.412164 0.787552 0.142190 0.665488 0.488059 
8 0.729960 0.037055 0.546328 0.683137 0.134247 0.444709 
9 0.391209 0.765251 0.507668 0.299963 0.348190 0.731980 

print df.columns.is_unique 

--output:-- 
True 
+0

Google 스프레드 시트 ... CSV 또는 Excel 파일이 아닙니다. 그래서, 그런 경우에, 팬더는 당신이 말하는 종류의 행동을하지 않습니다. 또한 나는 분명히''df.columns = [ 'a', 'a_2']'와 같은 해결책을 기대하지 않는다고 말했다. – working4coins

0

오라클 테이블에서 DataFrames를로드 할 때이 문제가 발생했습니다. 7stud는 pd.read_excel()이 * .1을 사용하여 중복 된 열을 자동으로 지정하지만 모든 읽기 기능이이를 수행하는 것은 아닙니다. 한 가지 해결 방법은 DataFrame을 CSV (또는 Excel) 파일에 저장 한 다음 다시로드하여 중복 된 열을 다시 지정하는 것입니다. 제공

data = pd.read_SQL(SQL,connection) 
data.to_csv(r'C:\temp\temp.csv') 
data=read_csv(r'C:\temp\temp.csv') 
관련 문제