pandas 初识(五)

2019-10-15| 发布者: admin| 查看: |

例:

 class_ timestamp count
0 10 2019-01-20 13:23:00 1
1 10 2019-01-20 13:24:00 2
2 10 2019-01-20 13:25:00 2
3 10 2019-01-20 13:26:00 1
4 10 2019-01-20 13:27:00 2

转为:

class_ 1 2 3 4 10
timestamp
2019-01-20 13:23:01 1.0 NaN NaN NaN NaN
2019-01-20 13:24:02 NaN NaN 2.0 NaN NaN
2019-01-20 13:25:03 NaN 2.0 NaN NaN NaN
2019-01-20 13:26:02 NaN NaN NaN 1.0 NaN
2019-01-20 13:27:05 NaN NaN NaN NaN 2.0







解决:

import pandas as pd
from pandas import Timestamp info = {'class_': {0: 1, 1: 2, 2: 3, 3: 4, 4: 10}, 'timestamp': {0: Timestamp, 1: Timestamp, 2: Timestamp, 3: Timestamp, 4: Timestamp}, 'count': {0: 1, 1: 2, 2: 2, 3: 1, 4: 2}} df = pd.DataFrame
# df.pivot.fillna
df.pivot



 

 

例:

class_ 1 2 3 4 10
timestamp
2019-01-20 13:23:01 1.0 NaN NaN NaN NaN
2019-01-20 13:24:02 NaN NaN 2.0 NaN NaN
2019-01-20 13:25:03 NaN 2.0 NaN NaN NaN
2019-01-20 13:26:02 NaN NaN NaN 1.0 NaN
2019-01-20 13:27:05 NaN NaN NaN NaN 2.0







转为:

 class_ timestamp count
0 10 2019-01-20 13:23:00 1
1 10 2019-01-20 13:24:00 2
2 10 2019-01-20 13:25:00 2
3 10 2019-01-20 13:26:00 1
4 10 2019-01-20 13:27:00 2

解决:

import pandas as pd
from pandas import Timestampinfo = {'class_': {0: 1, 1: 2, 2: 3, 3: 4, 4: 10},
 'timestamp': {0: Timestamp,
 1: Timestamp,
 2: Timestamp,
 3: Timestamp,
 4: Timestamp},
 'count': {0: 1, 1: 2, 2: 2, 3: 1, 4: 2}}
df = pd.DataFrame
#df1 = _df.pivot.dropna
df1 = _df.pivot