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

2024-09-30 10:42:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我用熊猫来写T.T.T文件,由其他C++程序生成。Python或熊猫不承认它们是数字,我真的不知道该怎么办。下面是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()

[![数据帧头部][1][1]

当我试着绘制它时,它就在这里:

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

Plot

检查y轴上的值是否为非正弦值。 这是.txt文件。用“\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

这里是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;

Tags: 文件代码txtdfmyfiletempintstd
1条回答
网友
1楼 · 发布于 2024-09-30 10:42:06
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()

相关问题 更多 >

    热门问题