![](/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.