基于以列表为元素的 Dataframe 创建CSV文件

cbwuti44  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(63)

考虑下面的带有列表的数据框架示例

import pandas as pd

# Example DataFrame with lists as elements
data = {
    'ColumnA': [['A', 'B', 'C'], ['D', 'E'], ['F'], ['G', 'H', 'I', 'J']],
    'ColumnB': [['1', '2', '3', '4'], ['5'], ['6', '7'], ['8', '9', '10']]
}

df = pd.DataFrame(data)

----------------------------
     ColumnA        ColumnB
0  [A, B, C]  [1, 2, 3, 4]
1     [D, E]           [5]
2        [F]        [6, 7]
3  [G, H, I, J]  [8, 9, 10]

我想只根据第一行创建一个具有以下格式的csv文件

ColumnA,ColumnB
A,1
B,2
C,3
NaN,4

如何实现?

h9vpoimq

h9vpoimq1#

只处理dfith行的代码(输入数据框)。在这种情况下,0th row

import pandas as pd

data = {
    'ColumnA': [['A', 'B', 'C'], ['D', 'E'], ['F'], ['G', 'H', 'I', 'J']],
    'ColumnB': [['1', '2', '3', '4'], ['5'], ['6', '7'], ['8', '9', '10']]
}

df = pd.DataFrame(data)

row_index = 0 

updated_cola = df.loc[row_index, 'ColumnA']
updated_colb = df.loc[row_index, 'ColumnB']

# Combine the lists using zip and create the new DataFrame
new_data = list(zip_longest(updated_cola, updated_colb))
new_df = pd.DataFrame(new_data, columns=['ColumnA', 'ColumnB'])

# Save the new DataFrame as a CSV file
new_df.to_csv('new_df.csv', index=False)

确保使用zip_longest()而不是zip()来获取输出中包含NaN的最后一行。

输出:

处理df的all rows的代码(输入数据框)

import pandas as pd
from itertools import zip_longest

data = {
    'ColumnA': [['A', 'B', 'C'], ['D', 'E'], ['F'], ['G', 'H', 'I', 'J']],
    'ColumnB': [['1', '2', '3', '4'], ['5'], ['6', '7'], ['8', '9', '10']]
}

df = pd.DataFrame(data)

# Define a function to pair elements of 'ColumnA' with 'ColumnB'
def pair_columns(row):
    updated_cola = row['ColumnA']
    updated_colb = row['ColumnB']
    new_data = list(zip_longest(updated_cola, updated_colb, fillvalue=None))
    return pd.DataFrame(new_data, columns=['ColumnA', 'ColumnB'])

# Apply the function to each row of the DataFrame
new_df = df.apply(pair_columns, axis=1)

# Concatenate the resulting DataFrames into a single DataFrame
new_df = pd.concat(new_df.values, ignore_index=True)

# Save the new DataFrame as a CSV file
new_df.to_csv('new_df.csv', index=False)

输出:

相关问题