![](/img/trans.png)
[英]Pandas: dropping all columns with nans, 0, and NA from DataFrame
[英]Splitting a column in pandas dataframe not dropping na
我将通过说我不拥有数据在csv中开始的方式来预先提出这个问题。 我也无法直接访问csv,因为我只能从我无法直接访问的SFTP中获取它。 API显示与csv显示的数据格式相同的格式。 以下是与数据帧相关的传入csv的两列。
+-----+-------------------------------+-------------+
| | Sourcing Event ID (DTRM ID) | Site |
+-----+-------------------------------+-------------+
| 0 | 1035 | ,ABC55, |
| 1 | 1067 | ,, |
| 2 | 1181 | ,, |
| 3 | 1183 | ,, |
| 4 | 1184 | ,, |
| 5 | 1264 | ,, |
| 6 | 1307 | ,DEF2, |
| 7 | 1354 | ,, |
| 8 | 1369 | ,HIJ150, |
| 9 | 1372 | ,DEF64, |
| 10 | 1373 | ,KLM9, |
| 11 | 1374 | ,DEF1, |
| 12 | 1381 | ,, |
| 13 | 1385 | ,, |
| 14 | 1391 | ,, |
| 15 | 1394 | ,, |
| 16 | 1395 | ,, |
| 17 | 1402 | ,, |
| 18 | 1404 | ,, |
| 19 | 1405 | ,, |
| 20 | 1406 | ,, |
| 21 | 1408 | ,, |
| 22 | 1410 | ,HIJ116, |
| 23 | 1412 | ,, |
+-----+-------------------------------+-------------+
从那里我做了以下(从以前的SO回答):
df_sourcing_events = pd.read_csv(wf['local_filename'])
sourcing_events_melt_col = 'Sourcing Event ID (DTRM ID)'
sourcing_events_site_col = 'Site'
print(df_sourcing_events[[sourcing_events_melt_col,sourcing_events_site_col]])
df_sourcing_events[sourcing_events_site_col] = df_sourcing_events[sourcing_events_site_col].str.lstrip(',')
df_sourcing_events[sourcing_events_site_col] = df_sourcing_events[sourcing_events_site_col].str.rstrip(',')
df_sourcing_events_sites = pd.concat([df_sourcing_events[sourcing_events_melt_col], df_sourcing_events[sourcing_events_site_col].str.split(',', expand = True)], axis = 1)\
.melt(id_vars=[sourcing_events_melt_col])\
.sort_values(by = sourcing_events_melt_col)\
.rename(columns = {'value' : sourcing_events_site_col})\
.drop(columns = ['variable'])\
.dropna()
现在你问自己为什么要删除前导和尾随逗号?
好吧,因为我有另一个文件与具有相同确切布局的合同有关,我对它做了同样的事情,并用相同的确切代码解决了问题。 我不能为我的生活理解为什么我的代码输出如下:
+-----+-------------------------------+-----------+
| | Sourcing Event ID (DTRM ID) | Site |
+-----+-------------------------------+-----------+
| 0 | 1035 | ABC55 |
| 1 | 1067 | |
| 2 | 1181 | |
| 3 | 1183 | |
| 4 | 1184 | |
| 5 | 1264 | |
| 6 | 1307 | DEF2 |
| 7 | 1354 | |
| 8 | 1369 | HIJ150 |
| 9 | 1372 | DEF64 |
| 10 | 1373 | KLM9 |
| 11 | 1374 | DEF1 |
| 12 | 1381 | |
| 13 | 1385 | |
| 14 | 1391 | |
| 15 | 1394 | |
| 16 | 1395 | |
| 17 | 1402 | |
| 18 | 1404 | |
| 19 | 1405 | |
| 20 | 1406 | |
| 21 | 1408 | |
| 22 | 1410 | HIJ116 |
| 23 | 1412 | |
+-----+-------------------------------+-----------+
就像dropna()根本不工作一样。 我甚至将其他合同csv中的工作代码复制并粘贴到此区域,只需更改代码中的变量以匹配此csv,它仍然无法正常工作。 我重新检查以确保其他代码实际上也正常工作。
我试过.dropna(how='any')
无济于事。 我还该怎么办?
编辑:
回答扎克人:
不,因为在那之后我做了以下事情:
df_sourcing_events_final = df_sourcing_events.drop([sourcing_events_site_col], axis=1)
write_dataframe_to_csv_on_s3(df_sourcing_events_sites, s3_bucket, 'sourcing_events_sites.csv')
write_dataframe_to_csv_on_s3(df_sourcing_events_final, s3_bucket, file_name)
我正在拆分一个列,该列是单个行的列表,并从中创建一个新的csv以加载到单独的表中。
它没有掉线,因为它们是空字符串而不是N / A. 尝试:
df = df_sourcing_events_sites
df = df[df.Site != '']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.