如何从数据帧转换为CSV?

时间:2019-05-23 04:43:37

标签: python csv dataframe

我阅读了CSV文件,将其转换为数据框,然后删除了重复的行,然后,当我将数据框转换为csv时,它为我返回了相同的csv文件,但没有重复行。

这是我的代码:

import pandas as pd
import csv
df = pd.read_csv('output_res.csv')
df.loc[~df.NameOfClass.duplicated(keep='last')]
df.to_csv("test.csv")

这是我的CSV文件:

NameOfClass,LazyClass,ComplexClass,LongParameterList,FeatureEnvy,LongMethod,BlobClass,MessageChain,RefusedBequest,SpaghettiCode,SpeculativeGenerality
com.nirhart.shortrain.MainActivity,NaN,NaN,NaN,NaN,NaN,NaN,1,NaN,NaN,NaN
com.nirhart.shortrain.path.PathParser,NaN,1,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN
com.nirhart.shortrain.path.PathParser,NaN,1,NaN,1,NaN,NaN,NaN,NaN,NaN,NaN
com.nirhart.shortrain.path.PathParser,NaN,1,1,1,NaN,NaN,NaN,NaN,NaN,NaN
com.nirhart.shortrain.path.PathParser,NaN,1,2,1,NaN,NaN,NaN,NaN,NaN,NaN
com.nirhart.shortrain.path.PathParser,NaN,1,2,1,1,NaN,NaN,NaN,NaN,NaN
com.nirhart.shortrain.path.PathPoint,1,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN
com.nirhart.shortrain.path.PathPoint,1,NaN,1,NaN,NaN,NaN,NaN,NaN,NaN,NaN
com.nirhart.shortrain.path.TrainPath,NaN,NaN,NaN,1,NaN,NaN,NaN,NaN,NaN,NaN
com.nirhart.shortrain.rail.RailActionActivity,NaN,NaN,NaN,1,NaN,NaN,NaN,NaN,NaN,NaN
com.nirhart.shortrain.rail.RailActionActivity,NaN,NaN,NaN,1,1,NaN,NaN,NaN,NaN,NaN

这就是它的返回结果:

,NameOfClass,LazyClass,ComplexClass,LongParameterList,FeatureEnvy,LongMethod,BlobClass,MessageChain,RefusedBequest,SpaghettiCode,SpeculativeGenerality
0,com.nirhart.shortrain.MainActivity,,,,,,,1.0,,,
1,com.nirhart.shortrain.path.PathParser,,1.0,,,,,,,,
2,com.nirhart.shortrain.path.PathParser,,1.0,,1.0,,,,,,
3,com.nirhart.shortrain.path.PathParser,,1.0,1.0,1.0,,,,,,
4,com.nirhart.shortrain.path.PathParser,,1.0,2.0,1.0,,,,,,
5,com.nirhart.shortrain.path.PathParser,,1.0,2.0,1.0,1.0,,,,,
6,com.nirhart.shortrain.path.PathPoint,1.0,,,,,,,,,
7,com.nirhart.shortrain.path.PathPoint,1.0,,1.0,,,,,,,
8,com.nirhart.shortrain.path.TrainPath,,,,1.0,,,,,,
9,com.nirhart.shortrain.rail.RailActionActivity,,,,1.0,,,,,,
10,com.nirhart.shortrain.rail.RailActionActivity,,,,1.0,1.0,,,,,

0 个答案:

没有答案