简体   繁体   English

从 .txt 文件读取时,Python Pandas 无法识别数字

[英]Python Pandas does not recognize numbers when read from .txt file

I'm using Pandas to write to table from .txt file generated by other C++ program.我正在使用 Pandas 从其他 C++ 程序生成的 .txt 文件写入表。 Python or Pandas do not recognize them as numbers and I'm really clueless what to do. Python 或 Pandas 不能将它们识别为数字,我真的不知道该怎么做。 Here is Python code:这是Python代码:

df = pd.read_csv(r'C:\Users\romea\Desktop\Inżynierka\ER_etap_5\Metropolis_average_path_6_p_0.010000_nodes_100.txt', sep='\t', engine='python', header=None)
df.columns = df.iloc[0]
df = df.drop([0])
df.head()

[![Head of data frame][1]][1] [![数据帧头][1]][1]

And here it is when i try plot it:这是当我尝试绘制它时:

plt.scatter(df["iteracja"], df["variancja"])
plt.show()

阴谋

Check out that values on y-axis are nonsens.检查 y 轴上的值是无意义的。 Here is the .txt file.这是 .txt 文件。 Separation with "\\t" and new line with std::endl from C++:用 "\\t" 分隔并用 C++ 中的 std::endl 换行:

iteracja    krawedzie   srednia_chwil   variancja
0   10000   10000   0.138686
100 2843.07 2843.07 0.991797
200 16.0296 16.0296 0.263918
300 4.55257 4.55257 0.235237
400 4.5834  4.5834  0.217816
500 4.68072 4.68072 0.167809
600 4.78377 4.78377 0.129301
700 4.83168 4.83168 0.109151
800 4.8534  4.8534  0.0963009
900 5.66296 5.66296 0.0710574
1000    5.96965 5.96965 0.0485687

And here is the part of C++ code that generates it:这是生成它的 C++ 代码部分:

myfile<<"iteracja"<<"\t"<<"krawedzie"<<"\t"<<"srednia_chwil"<<"\t"<<"variancja"<<endl;
        std::vector <float> temp_curent;
        std::vector <float> temp_edges;
        for(int i = 0; i<break_counter; i++)
        {
            temp_curent.push_back(current_average_array[i]);
            temp_edges.push_back(edge_array[i]);
            if (i % 100 == 0)
            {
                double suma = 0;
                double suma_edges = 0;
                for(int j = 0; j<temp_curent.size(); j++)
                {
                    suma_edges += temp_curent[j];
                    suma += temp_curent[j];
                }
                suma /= 100;
                suma_edges /= 100;
                cout<<i<<"\t"<<suma_edges<<"\t"<<suma<<"\t"<<variance_fractorial[(int)((i/100))]<<endl;
                myfile<<i<<"\t"<<suma_edges<<"\t"<<suma<<"\t"<<variance_fractorial[(int)((i/100))]<<endl;
                temp_curent.clear();
                temp_edges.clear();
            }
        }
        myfile.close();
        return break_counter;
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv(r'your_file_here.txt', sep='\t', header=0)
plt.scatter(df["iteracja"], df["variancja"])
plt.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM