从数十个 df.sum() 系列中创建一个新数据框
Create a new dataframe out of dozens of df.sum() series
我有几个 pandas 相同格式的数据帧,有五列。
我想使用 df.sum()
对这些数据帧中的每一个的值求和。这将为每个 Dataframe 创建一个系列,仍然有 5 列。
我的问题是如何获取这些系列,并创建另一个 Dataframe,一列是文件名,另一列是上面来自 df.sum()
的五列
import pandas as pd
import glob
batch_of_dataframes = glob.glob("*.txt")
newdf = []
for filename in batch_of_dataframes:
df = pd.read_csv(filename)
df['filename'] = str(filename)
df = df.sum()
newdf.append(df)
newdf = pd.concat(newdf, ignore_index=True)
不幸的是,这种方法不起作用。 'df['filename'] = str(filename)' 抛出 TypeError,并且创建新数据帧 newdf
无法正确解析。
如何正确地做到这一点?
如何使用多个 pandas.Series
对象并创建一个 DataFrame
?
回答这个具体问题:
@ThomasTu How do I go from a list of Series with 'Filename' as a
column to a dataframe? I think that's the problem---I don't understand
this
这基本上就是您现在所拥有的,但是您不是附加到一个空列表,而是附加到一个空数据框。如果您不想在每次迭代时重新分配 newdf,我认为有一个 inplace 关键字。
import pandas as pd
import glob
batch_of_dataframes = glob.glob("*.txt")
newdf = pd.DataFrame()
for filename in batch_of_dataframes:
df = pd.read_csv(filename)
df['filename'] = str(filename)
df = df.sum()
newdf = newdf.append(df, ignore_index=True)
按此顺序尝试:
创建一个空列表,比如 list_of_series
。
对于每个文件:
加载到数据框中,然后将总和保存在一个序列中s
向s添加一个元素:s['filename'] = your_filename
将 s
附加到 list_of_series
最后,连接(并根据需要转置):
final_df = pd.concat(list_of_series, axis = 1).T
代码
准备:
l_df = [pd.DataFrame(np.random.rand(3,5), columns = list("ABCDE")) for _ in range(5)]
for i, df in enumerate(l_df):
df.to_csv(str(i)+'.txt', index = False)
文件 *.txt 以逗号分隔并包含 headers.
! cat 1.txt
A,B,C,D,E
0.18021800981245173,0.29919271590063656,0.09527248614484807,0.9672038093199938,0.07655003742768962
0.35422759068109766,0.04184770882952815,0.682902924462214,0.9400817219440063,0.8825581077493059
0.3762875793116358,0.4745731412494566,0.6545473610147845,0.7479829630649761,0.15641907539706779
而且,实际上,其余部分与您所做的非常相似(我将文件名附加到一个系列,而不是数据框。否则它们会被 sum()
连接几次):
files = glob.glob('*.txt')
print(files)
['3.txt', '0.txt', '4.txt', '2.txt', '1.txt']
list_of_series = []
for f in files:
df = pd.read_csv(f)
s = df.sum()
s['filename'] = f
list_of_series.append(s)
final_df = pd.concat(list_of_series, axis = 1).T
print(final_df)
A B C D E filename
0 1.0675 2.20957 1.65058 1.80515 2.22058 3.txt
1 0.642805 1.36248 0.0237625 1.87767 1.63317 0.txt
2 1.68678 1.26363 0.835245 2.05305 1.01829 4.txt
3 1.22748 2.09256 0.785089 1.87852 2.05043 2.txt
4 0.910733 0.815614 1.43272 2.65527 1.11553 1.txt
我有几个 pandas 相同格式的数据帧,有五列。
我想使用 df.sum()
对这些数据帧中的每一个的值求和。这将为每个 Dataframe 创建一个系列,仍然有 5 列。
我的问题是如何获取这些系列,并创建另一个 Dataframe,一列是文件名,另一列是上面来自 df.sum()
import pandas as pd
import glob
batch_of_dataframes = glob.glob("*.txt")
newdf = []
for filename in batch_of_dataframes:
df = pd.read_csv(filename)
df['filename'] = str(filename)
df = df.sum()
newdf.append(df)
newdf = pd.concat(newdf, ignore_index=True)
不幸的是,这种方法不起作用。 'df['filename'] = str(filename)' 抛出 TypeError,并且创建新数据帧 newdf
无法正确解析。
如何正确地做到这一点?
如何使用多个 pandas.Series
对象并创建一个 DataFrame
?
回答这个具体问题:
@ThomasTu How do I go from a list of Series with 'Filename' as a column to a dataframe? I think that's the problem---I don't understand this
这基本上就是您现在所拥有的,但是您不是附加到一个空列表,而是附加到一个空数据框。如果您不想在每次迭代时重新分配 newdf,我认为有一个 inplace 关键字。
import pandas as pd
import glob
batch_of_dataframes = glob.glob("*.txt")
newdf = pd.DataFrame()
for filename in batch_of_dataframes:
df = pd.read_csv(filename)
df['filename'] = str(filename)
df = df.sum()
newdf = newdf.append(df, ignore_index=True)
按此顺序尝试:
创建一个空列表,比如
list_of_series
。对于每个文件:
加载到数据框中,然后将总和保存在一个序列中
s
向s添加一个元素:
s['filename'] = your_filename
将
s
附加到list_of_series
最后,连接(并根据需要转置):
final_df = pd.concat(list_of_series, axis = 1).T
代码
准备:
l_df = [pd.DataFrame(np.random.rand(3,5), columns = list("ABCDE")) for _ in range(5)]
for i, df in enumerate(l_df):
df.to_csv(str(i)+'.txt', index = False)
文件 *.txt 以逗号分隔并包含 headers.
! cat 1.txt
A,B,C,D,E
0.18021800981245173,0.29919271590063656,0.09527248614484807,0.9672038093199938,0.07655003742768962
0.35422759068109766,0.04184770882952815,0.682902924462214,0.9400817219440063,0.8825581077493059
0.3762875793116358,0.4745731412494566,0.6545473610147845,0.7479829630649761,0.15641907539706779
而且,实际上,其余部分与您所做的非常相似(我将文件名附加到一个系列,而不是数据框。否则它们会被 sum()
连接几次):
files = glob.glob('*.txt')
print(files)
['3.txt', '0.txt', '4.txt', '2.txt', '1.txt']
list_of_series = []
for f in files:
df = pd.read_csv(f)
s = df.sum()
s['filename'] = f
list_of_series.append(s)
final_df = pd.concat(list_of_series, axis = 1).T
print(final_df)
A B C D E filename
0 1.0675 2.20957 1.65058 1.80515 2.22058 3.txt
1 0.642805 1.36248 0.0237625 1.87767 1.63317 0.txt
2 1.68678 1.26363 0.835245 2.05305 1.01829 4.txt
3 1.22748 2.09256 0.785089 1.87852 2.05043 2.txt
4 0.910733 0.815614 1.43272 2.65527 1.11553 1.txt