回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>我是python开发的新手,我正在尝试基于空值将csv文件分为两个不同的文本文件</p>
<p>我的csv文件有如下数据</p>
<p><a href="https://i.stack.imgur.com/mRlo7.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/mRlo7.png" alt="enter image description here"/></a></p>
<p>以及</p>
<p><a href="https://i.stack.imgur.com/0gnPJ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/0gnPJ.png" alt="enter image description here"/></a></p>
<p>我的csv文件包含四个字段facility、Truck、Driver和license
truck和driver有一些空值
我想为truck value为null的行值创建两个单独的文件,另一个文件将包含driver value为null的信息</p>
<p>我尝试了以下代码,但它并没有消除空值,它在文本文件中显示0或空格</p>
<pre><code> License = pd.read_csv("E:\ActiveCityLicenses.csv")
a=License.isnull().sum()
print(a)
print(License.shape)
m=License[License['TRUCK_ID'].isnull()]
print(m)
n=License.dropna(axis= 0, subset= ['TRUCK_ID'], inplace=True)
print(n)
License.to_csv(r'E:\DriverLicense.txt', header=None, index=None, mode='w', columns=None)
#I had to create two data frames as after doing first dorpna entire frame gets empty
License1 = pd.read_csv("E:\ActiveCityLicenses.csv")
p=License1.dropna(axis= 0, subset= ['EMPLOYEE_ID'], inplace=True)
print(p)
License1.to_csv(r'E:\TruckLicense.txt', header=None, index=None, sep=',', mode='w')
</code></pre>
<p>有没有人能建议一个更好的方法,或者我在这里遗漏了什么?
文本文件中的输出为</p>
<pre><code> A119,BF01,,TOR|MARK|BRAM|MISS|RHILL|VAU
A119,BF03,,TOR|MARK|BRAM|MISS|RHILL|VAU
A119,BF04,,TOR|MARK|BRAM|MISS|RHILL|VAU
A119,BF05,,TOR|MARK|BRAM|MISS|RHILL|VAU
</code></pre>
<p>空间不应该存在</p>