簡體   English   中英

如何從 Python 中的原始文本轉換 pandas dataframe?

[英]How can I convert a pandas dataframe from a raw text in Python?

我有一個包含這樣的數據的文本文件,格式為列表,其中第一個元素是一個字符串,其中包含由“;”分隔的列名,下一個元素是值行:

['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;']

如何將此文本列表轉換為 pandas dataframe?

使用pd.read_csv ,即讀取文本文件和數據幀pd.compat.StringIO ,使流從文本,像io.StingIO

pd.read_csv(pd.compat.StringIO("\n".join(lines)), sep=";")

碼:

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)

輸出:

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 

您可以使用from_records()函數拆分輸入列表中的每個字符串項,並注意數據的第一行包含列的標簽這一事實

>>> data = ['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;']

>>> df = pd.DataFrame.from_records([r.split(';') for r in data[1:]], columns=data[0].split(';'))

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

 ... 

@Nihal 解決方案的基礎更短

df = [n.split(';') for n in raw_data_text]
df = pd.DataFrame(df[1:], columns=df[0])

If there are just comma separated values as output to your model - you can use this to convert into a pandas dataframe (content is your output in streamlit app)

out = [line.split(",") for line in content.strip().split("\n")]
df1 = pd.DataFrame(out)
df1.columns = df1.iloc[0]
df1 = df1.reindex(df1.index.drop(0))
st.write(df1)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM