Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 熊猫:从宽到长的转换:如何获取行数和列数_Python 3.x_Pandas - Fatal编程技术网

Python 3.x 熊猫:从宽到长的转换:如何获取行数和列数

Python 3.x 熊猫:从宽到长的转换:如何获取行数和列数,python-3.x,pandas,Python 3.x,Pandas,初学者问题: 我有一个矩阵,比如3x3,我想把它转换成长格式,如下所示: 宽: A B C A 0.1 0.2 0.3 B 0.1 0.2 0.3 C 0.1 0.2 0.3 长: Col1 Col2 Row_num Col_num Value 0 A A 1 1 0.1 1 A B 1 2 0.2 2 A C

初学者问题: 我有一个矩阵,比如3x3,我想把它转换成长格式,如下所示:

宽:

    A      B    C
A   0.1    0.2    0.3
B   0.1    0.2    0.3 
C   0.1    0.2    0.3
长:

    Col1  Col2  Row_num Col_num Value

0   A     A     1        1     0.1
1   A     B     1        2     0.2
2   A     C     1        3     0.3
.
.
8   C     C     3        3     0.3
我尝试过各种函数,如melt、unstack()、wide_to_long,但无法获得列数。最好的方法是什么


谢谢

我确信有一种更有效的方法可以做到这一点,因为我的方法涉及两个for循环,但这是一种快速而肮脏的方法,可以像您所寻找的那样转换数据:

# df is your initial dataframe
df = pd.DataFrame({"A": [1,1,1],
                   "B": [2,2,2],
                   "C": [3,3,3]}, 
                   index=["A","B","C"])

#long_rows will store the data we need for the new df
long_rows = []

# loop through each row 
for i in range(len(df)):

    #loop through each column
    for j in range(len(df.columns)):

        ind = list(df.index.values)[i]
        col = list(df.columns.values)[j]
        val = df.iloc[i,j]
        row = [ind, col, i+1, j+1, val]
        long_rows.append(row)

new_df = pd.DataFrame(long_rows, columns=["Col1", "Col2", "Row1", "Row2", "Value"])
结果是:

new_df
    Col1    Col2    Row1    Row2    Value
0   A       A       1       1       1
1   A       B       1       2       2
2   A       C       1       3       3
3   B       A       2       1       1
4   B       B       2       2       2
5   B       C       2       3       3
6   C       A       3       1       1
7   C       B       3       2       2
8   C       C       3       3       3

创建数据并取消堆叠值

df = pd.DataFrame({'A': [0.1, 0.1, 0.1],
                   'B': [0.2, 0.2, 0.2],
                   'C': [0.3, 0.3, 0.3]}, 
                   index=['A', 'B', 'C'])
mapping = {col: idx for idx, col in enumerate(df.columns, 1)}
df = df.unstack().to_frame().reset_index()
df.columns = ['Col1', 'Col2', 'Value']
数据帧

>>> df

    Col1  Col2  Value
0   A     A     0.1
1   A     B     0.1
2   A     C     0.1
3   B     A     0.2
4   B     B     0.2
5   B     C     0.2
6   C     A     0.3
7   C     B     0.3
8   C     C     0.3
映射剩余值

df = pd.DataFrame({'A': [0.1, 0.1, 0.1],
                   'B': [0.2, 0.2, 0.2],
                   'C': [0.3, 0.3, 0.3]}, 
                   index=['A', 'B', 'C'])
mapping = {col: idx for idx, col in enumerate(df.columns, 1)}
df = df.unstack().to_frame().reset_index()
df.columns = ['Col1', 'Col2', 'Value']
>>df.assign(
行数=df['Col1'].map(映射),
Col_num=df['Col2'].map(映射)
)
输出

    Col1  Col2  Value Row_num Col_num
0   A     A     0.1   1    1
1   A     B     0.1   1    2
2   A     C     0.1   1    3
3   B     A     0.2   2    1
4   B     B     0.2   2    2
5   B     C     0.2   2    3
6   C     A     0.3   3    1
7   C     B     0.3   3    2
8   C     C     0.3   3    3

嗨,谢谢你的回答。。我想你误解了问题,所以我更新了不同的值和列名称。请看一看。非常感谢你。这很有效。但正如你所说,可能有一个更圆滑的方法。但我真的很感激你的回答。谢谢