我有一个类似的列表
list1=['wwe', '0.99,', 'aew', '0.80,', 'ufc', '1,', 'tna', '0.45,', 'wwf', '1,', 'ring of honor', '0.6,']
我试图在pandas数据帧中推送列表,使我的数据帧看起来像这样:
shows ratings
wwe 0.99
aew 0.80
ufc 1
tna 0.45
wwf 1
ring of honor 0.6
我尝试了不同的方法来获得这个数据帧,但都没能获得,我该如何实现呢?
将DataFrame
构造函数与reshape
一起使用,此处-1
按numpy计数,然后删除逗号并转换为列ratings
:的浮点值
df = pd.DataFrame(np.reshape(list1, (-1,2)), columns=['shows','ratings'])
df['ratings'] = df['ratings'].str.strip(',').astype(float)
print (df)
shows ratings
0 wwe 0.99
1 aew 0.80
2 ufc 1.00
3 tna 0.45
4 wwf 1.00
5 ring of honor 0.60
import pandas as pd
list1=['wwe', '0.99,', 'aew', '0.80,', 'ufc', '1,', 'tna', '0.45,', 'wwf', '1,', 'ring of honor', '0.6,']
col1 = list1[::2]
col2 = list1[1::2]
# create dataframe from col1 and col2
df = pd.DataFrame({'shows':col1, 'ratings':col2})