我認爲你可以使用DataFrame.from_records
與應用str.strip
:由dt.strftime
有個名字產生
import pandas as pd
import numpy as np
a = [['0L', '0L', '0L', '0L', '0L', '0L', '0L', '0L', '0L', '1L', '1L', '1L', '1L', '1L', '0L', '0L', '0L', '1L', '1L', '1L', '1L', '0L', '0L', '0L'],
['0L', '0L', '0L', '0L', '0L', '0L', '0L', '0L', '0L', '1L', '1L', '1L', '1L', '1L', '0L', '0L', '0L', '1L', '1L', '2L', '2L', '0L', '0L', '0L']]
idx = ['Jan','Feb']
df = pd.DataFrame.from_records(a, index=idx).apply(lambda x: x.str.strip('L').astype(int))
print (df)
0 1 2 3 4 5 6 7 8 9 ... 14 15 16 17 18 19 20 \
Jan 0 0 0 0 0 0 0 0 0 1 ... 0 0 0 1 1 1 1
Feb 0 0 0 0 0 0 0 0 0 1 ... 0 0 0 1 1 2 2
21 22 23
Jan 0 0 0
Feb 0 0 0
[2 rows x 24 columns]
更通用的解決方案:
print (pd.Series(range(1,len(a) + 1)))
0 1
1 2
dtype: int32
idx = pd.to_datetime(pd.Series(range(1,len(a) + 1)), format='%m').dt.strftime('%b')
0 Jan
1 Feb
dtype: object
df = pd.DataFrame.from_records(a, index=idx).apply(lambda x: x.str.strip('L').astype(int))
print (df)
0 1 2 3 4 5 6 7 8 9 ... 14 15 16 17 18 19 20 \
Jan 0 0 0 0 0 0 0 0 0 1 ... 0 0 0 1 1 1 1
Feb 0 0 0 0 0 0 0 0 0 1 ... 0 0 0 1 1 2 2
21 22 23
Jan 0 0 0
Feb 0 0 0
如果需要split
值第一:
b = [['0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 0L, 0L, 0L'],
['0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 1L, 1L, 2L, 2L, 0L, 0L, 0L']]
idx = pd.to_datetime(pd.Series(range(1,len(a) + 1)), format='%m').dt.strftime('%b')
df1 = pd.DataFrame.from_records(b, index=idx)
.iloc[:,0]
.str.split(', ', expand=True)
.replace({'L':''}, regex=True)
.astype(int)
print (df1)
0 1 2 3 4 5 6 7 8 9 ... 14 15 16 17 18 19 20 \
Jan 0 0 0 0 0 0 0 0 0 1 ... 0 0 0 1 1 1 1
Feb 0 0 0 0 0 0 0 0 0 1 ... 0 0 0 1 1 2 2
21 22 23
Jan 0 0 0
Feb 0 0 0
[2 rows x 24 columns]
它是如何工作的? – jezrael
謝謝你的詳細解答。我不能將.str.strip('L')。astype(int)應用於數據框的單元格:AttributeError:'str'對象沒有屬性'str'。怎麼來的? (單元格是str類型) – John12
它不能與樣本或實際數據一起使用? – jezrael