[英]Convert consecutive columns to respective rows in Pandas DataFrame
Trying to convert consecutive columns to rows in pandas.尝试将连续的列转换为熊猫中的行。 Ex: Consecutive column names are sequential numbers along with some strings ie Key1
, Val1
,...., KeyN
, ValN
in DataFrame
.例如:连续的列名是序列号以及一些字符串,即ValN
中的Key1
, Val1
,...., KeyN
, DataFrame
。 You can use below code to generate the dataframe.您可以使用以下代码生成数据框。
df = pd.DataFrame({'City': ['Houston', 'Austin', 'Hoover'],'State': ['Texas', 'Texas', 'Alabama'],'Name':['Aria', 'Penelope', 'Niko'],'Key1':["test1", "test2", "test3"],'Val1':[28, 4, 7],'Key2':["test4", "test5", "test6"],
'Val2':[82, 45, 76],'Key3':["test7", "test8", "test9"],'Val3':[4, 76, 9],'Key4':["test10", "test11", "test12"],'Val4':[97, 66, 10],'Key5':["test13", "test14", "test15"],'Val5':[4, 10, '']},columns=['City', 'State', 'Name', 'Key1', 'Val1', 'Key2', 'Val2', 'Key3', 'Val3', 'Key4', 'Val4', 'Key5', 'Val5'])
I tried melt
function as below:我尝试如下melt
功能:
df.melt(id_vars=['City', 'State'], var_name='Column', value_name='Key')
But I got the below output:但我得到了以下输出:
The problem is for every key, val column has different rows.问题是对于每个键,val 列都有不同的行。 The expected output is below:预期输出如下:
Use pd.wide_to_long
:使用pd.wide_to_long
:
pd.wide_to_long(df,['Key', 'Val'],['City', 'State', 'Name'],'No').reset_index()
Output:输出:
City State Name No Key Val
0 Houston Texas Aria 1 test1 28
1 Houston Texas Aria 2 test4 82
2 Houston Texas Aria 3 test7 4
3 Houston Texas Aria 4 test10 97
4 Houston Texas Aria 5 test13 4
5 Austin Texas Penelope 1 test2 4
6 Austin Texas Penelope 2 test5 45
7 Austin Texas Penelope 3 test8 76
8 Austin Texas Penelope 4 test11 66
9 Austin Texas Penelope 5 test14 10
10 Hoover Alabama Niko 1 test3 7
11 Hoover Alabama Niko 2 test6 76
12 Hoover Alabama Niko 3 test9 9
13 Hoover Alabama Niko 4 test12 10
14 Hoover Alabama Niko 5 test15
You are trying to simultaneously melt two columns.您正在尝试同时熔化两列。 pd.wide_to_long handles this situation. pd.wide_to_long 处理这种情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.