Pivot CSV-файл в Python - PullRequest
0 голосов
/ 11 мая 2018

У меня есть следующий формат данных в CSV-файле:

1,01,ABC,This is abc101,This is another abc101
1,01,DEF,This is def101,This is another def101
1,02,ABC,This is abc102,This is another abc102
1,02,DEF,This is def102,This is another def102
1,02,GHI,This is ghi102,This is another ghi102
2,01,ABC,This is abc201,This is another abc201
2,01,DEF,This is def201,This is another def201
2,01,GHI,This is ghi201,This is another ghi201
2,03,GHI,This is ghi203,This is another ghi203
3,02,ABC,This is abc302,This is another abc302
3,02,ABC,This is abc302,This is another abc302
3,02,ABC,This is abc302,This is another abc302
4,01,ABC,This is abc401,This is another abc401
4,01,DEF,This is def401,This is another def401
4,01,ABC,This is abc401,This is another abc401
4,02,DEF,This is def402,This is another def402
4,02,DEF,This is def402,This is another def402

также у меня есть список переменных = ['ABC','ABC_2','GHI','GHI_2'] список заголовков CSV-файлов = ['ID1','ID2','Var_name','var_value1','var_value2']

Мне нужно повернуть вышеуказанные данные, как показано ниже [['ID1','ID2','ABC','ABC_2','GHI','GHI_2'], [1,01,'This is abc101','This is another abc101','',''], [1,02,'This is abc102','This is another abc102','This is ghi102','This is another ghi102']] .. вот так

Если список переменных = ['GHI','GHI_2','ABC','ABC_2'] Выход будет: [['ID1','ID2','GHI','GHI_2','ABC','ABC_2'], [1,01,'','','This is abc101','This is another abc101'], [1,02,'This is ghi102','This is another ghi102','This is abc102','This is another abc102']] .. вот так

Это означает, что список должен:

  • заполнить данные для всех идентификаторов
  • создать пустую строку для тех переменных, которых нет в указанном выше наборе данных.
  • Файл csv не имеет заголовка, у нас есть отдельный список заголовков
  • заполнить вложенный список, сохраняя тот же порядок, что и список заголовков
  • заполняет только те значения заголовка, т.е. список заголовков имеет только значения 'ABC', 'GHI', поэтому вложенный список должен заполнять только значения 'ABC' и 'GHI' и должен игнорировать строки 'DEF' из вышеуказанного набора данных .
  • Для var_value2 он будет заполняться под позицией _2, например «Это другой abc101», будет под «ABC_2»

Я хочу сделать это в Python 2.7, возможно, используя Pandas.

variable_list = ['ABC','DEF']
df = pd.read_csv(csvfile,delimiter='#',engine='python',header=None)
df.columns = ['ID1','ID2','var_name','var_value']
f=df.set_index(['ID1','ID2','var_name'])['var_value'].unstack(fill_value='').fillna('')[variable_list].reset_index()
L1 = [f.columns.tolist()] + f.values.tolist()

Этот код я пробовал для одного var_value, теперь у меня есть два (var_value1, var_value2)

1 Ответ

0 голосов
/ 11 мая 2018

Для меня работает:

#remove duplicates sample data
print (df)
    0   1    2               3                       4
0   1  01  ABC  This is abc101  This is another abc101
1   1  01  DEF  This is def101  This is another def101
2   1  02  ABC  This is abc102  This is another abc102
3   1  02  DEF  This is def102  This is another def102
4   1  02  GHI  This is ghi102  This is another ghi102
5   2  01  ABC  This is abc201  This is another abc201
6   2  01  DEF  This is def201  This is another def201
7   2  01  GHI  This is ghi201  This is another ghi201
8   2  03  GHI  This is ghi203  This is another ghi203
9   3  02  ABC  This is abc302  This is another abc302
10  4  01  ABC  This is abc401  This is another abc401
11  4  02  DEF  This is def402  This is another def402

#change headers to empty string and 2 for 'ABC','ABC_2','GHI','GHI_2'
df.columns = ['ID1','ID2','Var_name','','2']

#unstack all columns
df = df.set_index(['ID1','ID2', 'Var_name']).unstack()

#excepted columns names for ordering
variable_list = ['ABC','ABC_2', 'GHI', 'GHI_2']

#filter second level of MultiIndex
df = df.loc[:, df.columns.get_level_values(1).isin(variable_list)]
#flatten columns names
df.columns = ['{}_{}'.format(j, i).strip('_') for i, j in df.columns]
df = df.reindex(columns=variable_list).fillna('').reset_index()
print (df)
   ID1 ID2             ABC                   ABC_2             GHI  \
0    1  01  This is abc101  This is another abc101                   
1    1  02  This is abc102  This is another abc102  This is ghi102   
2    2  01  This is abc201  This is another abc201  This is ghi201   
3    2  03                                          This is ghi203   
4    3  02  This is abc302  This is another abc302                   
5    4  01  This is abc401  This is another abc401                   
6    4  02                                                           

                    GHI_2  
0                          
1  This is another ghi102  
2  This is another ghi201  
3  This is another ghi203  
4                          
5                          
6     

#convert to lists
L1 = [df.columns.tolist()] + df.values.tolist()
print (L1[:3])
[['ID1', 'ID2', 'ABC', 'ABC_2', 'GHI', 'GHI_2'], 
 [1, '01', 'This is abc101', 'This is another abc101', '', ''], 
 [1, '02', 'This is abc102', 'This is another abc102', 'This is ghi102', 'This is another ghi102']]
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...