如何从Python中的原始文本转换熊猫数据框?

jar*_*fly 2 python text list dataframe pandas

我有一个文本文件,其中包含这样的数据,并以列表格式设置,其中第一个元素是一个字符串,其中包含用“;”分隔的列名,而下一个元素是值行:

['Timestamp;T;Pressure [bar];Input line pressure [bar];Speed [rpm];Angular Position [degree];Wheel speed [rpm];Wheel angular position [degree];',
';1;5,281;5,303;219,727;10,283;216,363;45;',
';1;5,273;5,277;219,727;11,602;216,363;45;',
';1;5,288;5,293;205,078;12,832;216,363;45;',
';1;5,316;5,297;219,727;14,15;216,363;45;',
';1;5,314;5,307;219,727;15,469;216,363;45;',
';1;5,288;5,3;219,727;16,787;216,363;45;',
';1;5,318000000000001;5,31;219,727;18,105;216,363;45;',
';1;5,304;5,3;219,727;19,424;216,388;56,25;',
';1;5,291;5,29;219,947;20,742;216,388;56,25;',
';1;5,316;5,297;219,507;22,061;216,388;56,25;']
Run Code Online (Sandbox Code Playgroud)

如何将该文本列表转换为pandas数据框?

Nih*_*hal 6

代码:

df = [
    'Timestamp;T;Pressure [bar];Input line pressure [bar];Speed [rpm];Angular Position [degree];Wheel speed [rpm];Wheel angular position [degree];',
    ';1;5,281;5,303;219,727;10,283;216,363;45;',
    ';1;5,273;5,277;219,727;11,602;216,363;45;',
    ';1;5,288;5,293;205,078;12,832;216,363;45;',
    ';1;5,316;5,297;219,727;14,15;216,363;45;',
    ';1;5,314;5,307;219,727;15,469;216,363;45;',
    ';1;5,288;5,3;219,727;16,787;216,363;45;',
    ';1;5,318000000000001;5,31;219,727;18,105;216,363;45;',
    ';1;5,304;5,3;219,727;19,424;216,388;56,25;',
    ';1;5,291;5,29;219,947;20,742;216,388;56,25;',
    ';1;5,316;5,297;219,507;22,061;216,388;56,25;']

mat = [n.split(';') for n in df]
print(mat)
newdf1 = pd.DataFrame(mat)
newdf1.columns = newdf1.iloc[0]
newdf1 = newdf1.reindex(newdf1.index.drop(0))
# newdf2 = pd.DataFrame.from_dict(df)
print(newdf1)
Run Code Online (Sandbox Code Playgroud)

输出:

0  Timestamp  T     Pressure [bar] Input line pressure [bar] Speed [rpm]  \
1             1              5,281                     5,303     219,727   
2             1              5,273                     5,277     219,727   
3             1              5,288                     5,293     205,078   
4             1              5,316                     5,297     219,727   
5             1              5,314                     5,307     219,727   
6             1              5,288                       5,3     219,727   
7             1  5,318000000000001                      5,31     219,727   
8             1              5,304                       5,3     219,727   
9             1              5,291                      5,29     219,947   
10            1              5,316                     5,297     219,507   

0  Angular Position [degree] Wheel speed [rpm]  \
1                     10,283           216,363   
2                     11,602           216,363   
3                     12,832           216,363   
4                      14,15           216,363   
5                     15,469           216,363   
6                     16,787           216,363   
7                     18,105           216,363   
8                     19,424           216,388   
9                     20,742           216,388   
10                    22,061           216,388   

0  Wheel angular position [degree]    
1                               45    
2                               45    
3                               45    
4                               45    
5                               45    
6                               45    
7                               45    
8                            56,25    
9                            56,25    
10                           56,25 
Run Code Online (Sandbox Code Playgroud)


koP*_*tok 5

使用pd.read_csv,从文本文件读取数据帧,使用和pd.compat.StringIO,从文本生成数据流,例如io.StingIO

pd.read_csv(pd.compat.StringIO("\n".join(lines)), sep=";")
Run Code Online (Sandbox Code Playgroud)

  • 与上面的解决方案相比,我更喜欢这种解决方案,因为它会将肮脏的工作委托给了熊猫。当数据太多(I / O始终是瓶颈)时,请使用此选项。 (3认同)
  • 仅适用于 pandas < 0.25 /sf/ask/3997324761/ (2认同)