熊猫CSV数据框

时间:2019-05-06 07:34:23

标签: python pandas dataframe

我有一个像这样的数据框:

+---+-------+------+-------+-------+
| id| prop1 | prop2|  prop3|prop4  |
+---+-------+------+-------+-------+
|  1| value1|value2| value3|   null|
|  2|value11|  null|value13|value14|
+---+-------+------+-------+-------+

I want to get this in python:
+-------+------------+
|    id |      prop  |
+-------+------------+
|   1   | value1     |
|   1   | value2     |
|   1   | value3     |
|   1   | null       |
|   2   | value11    | 
|   2   | null       |
+-------+------------+


import pandas as pd
import numpy as ny
df1 = pd.read_csv('C:\Python27\programs\DF.csv', delimiter = ',', index_col = 'id')
print(df1)
print('*************************************')
for i,j in df1.iterrows():
    df2 = (i,j)
    print(df2)

1 个答案:

答案 0 :(得分:0)

似乎您需要完全不使用数据框 因此,请使用融化将其取消旋转

pd.melt(df,id_vars=['id'],value_vars=['prop1', 'prop2','prop3','prop4'])